test 1 doc

Dependencies:   mbed Gamepad2

Committer:
joebarhouch
Date:
Wed May 27 02:00:08 2020 +0000
Revision:
10:9317a62bd4d0
Parent:
9:9830d3a78572
Child:
11:b3024ab59fa5
individual collision was with enemy and platform was too hard, new game system implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joebarhouch 3:e4e1cbf750b6 1 #include "Engine.h"
joebarhouch 3:e4e1cbf750b6 2
joebarhouch 9:9830d3a78572 3 Platform maps[6] = {Platform(0, 40, 20, 3), Platform(0, 14, 20, 3), Platform(64, 14, 10, 3), Platform(70, 40, 20, 3), Platform(5, 20, 30, 3), Platform(55, 30, 25, 3)};
joebarhouch 7:530ca713d2b2 4 int mapSize = sizeof(maps)/sizeof(*maps);
joebarhouch 7:530ca713d2b2 5
joebarhouch 7:530ca713d2b2 6
joebarhouch 7:530ca713d2b2 7 ////////////////////// DRAW MAP //////////////////////////
joebarhouch 7:530ca713d2b2 8 void drawMap(N5110 &lcd)
joebarhouch 7:530ca713d2b2 9 {
joebarhouch 7:530ca713d2b2 10 for (int i = 0; i < mapSize; i++) {
joebarhouch 7:530ca713d2b2 11 maps[i].draw(lcd);
joebarhouch 7:530ca713d2b2 12
joebarhouch 7:530ca713d2b2 13 //debugs
joebarhouch 7:530ca713d2b2 14 //coords = maps[i].get_pos();
joebarhouch 7:530ca713d2b2 15 //printf("x: %i, y: %i,w: %i,h: %i \n",coords.x, coords.y, coords.width, coords.height);
joebarhouch 7:530ca713d2b2 16 }
joebarhouch 7:530ca713d2b2 17 //debugs
joebarhouch 7:530ca713d2b2 18 //printf("-----------------------------------------\n");
joebarhouch 7:530ca713d2b2 19 }
joebarhouch 7:530ca713d2b2 20
joebarhouch 7:530ca713d2b2 21
joebarhouch 7:530ca713d2b2 22
joebarhouch 3:e4e1cbf750b6 23 Engine::Engine()
joebarhouch 3:e4e1cbf750b6 24 {
joebarhouch 3:e4e1cbf750b6 25 }
joebarhouch 3:e4e1cbf750b6 26 Engine::~Engine()
joebarhouch 3:e4e1cbf750b6 27 {
joebarhouch 3:e4e1cbf750b6 28 }
joebarhouch 3:e4e1cbf750b6 29
joebarhouch 6:00d20886e4f8 30
joebarhouch 6:00d20886e4f8 31 ////////////////////// INIT ////////////////////////////
joebarhouch 3:e4e1cbf750b6 32 void Engine::init()
joebarhouch 3:e4e1cbf750b6 33 {
joebarhouch 3:e4e1cbf750b6 34 //init coord
joebarhouch 3:e4e1cbf750b6 35 _px = WIDTH / 2;
joebarhouch 7:530ca713d2b2 36 _py = 5;
joebarhouch 6:00d20886e4f8 37
joebarhouch 3:e4e1cbf750b6 38 //init call
joebarhouch 3:e4e1cbf750b6 39 _p.init(_px, _py);
joebarhouch 7:530ca713d2b2 40
joebarhouch 8:d19b30a6cd69 41 //ennemy
joebarhouch 10:9317a62bd4d0 42 enemies.push_back(Enemy(0,0, 10));
joebarhouch 10:9317a62bd4d0 43 enemies.push_back(Enemy(0,75, 20));
joebarhouch 10:9317a62bd4d0 44 enemies.push_back(Enemy(1,20, 0));
joebarhouch 10:9317a62bd4d0 45 enemies.push_back(Enemy(1,60, 30));
joebarhouch 8:d19b30a6cd69 46
joebarhouch 8:d19b30a6cd69 47
joebarhouch 7:530ca713d2b2 48 //physics parameters
joebarhouch 7:530ca713d2b2 49 _Ypos = 0;
joebarhouch 7:530ca713d2b2 50 _fall = true;
joebarhouch 8:d19b30a6cd69 51 _c = false;
joebarhouch 8:d19b30a6cd69 52
joebarhouch 9:9830d3a78572 53 //rands
joebarhouch 9:9830d3a78572 54 srand(time(NULL));
joebarhouch 3:e4e1cbf750b6 55 }
joebarhouch 3:e4e1cbf750b6 56
joebarhouch 6:00d20886e4f8 57
joebarhouch 6:00d20886e4f8 58
joebarhouch 6:00d20886e4f8 59 ////////////////////// INPUT //////////////////////////
joebarhouch 5:928c2eee4109 60 //reads direction and magnitude from the JOYSTICK to control the player
joebarhouch 3:e4e1cbf750b6 61 void Engine::read_input(Gamepad &pad)
joebarhouch 3:e4e1cbf750b6 62 {
joebarhouch 3:e4e1cbf750b6 63 _d = pad.get_direction();
joebarhouch 3:e4e1cbf750b6 64 _mag = pad.get_mag();
joebarhouch 8:d19b30a6cd69 65 _jump = pad.B_held();
joebarhouch 8:d19b30a6cd69 66 //printf("%s", _jump ? "true\n" : "false\n");
joebarhouch 3:e4e1cbf750b6 67 }
joebarhouch 3:e4e1cbf750b6 68
joebarhouch 7:530ca713d2b2 69
joebarhouch 7:530ca713d2b2 70
joebarhouch 7:530ca713d2b2 71
joebarhouch 7:530ca713d2b2 72
joebarhouch 6:00d20886e4f8 73 ////////////////////// DRAW ///////////////////////////
joebarhouch 6:00d20886e4f8 74 //draw both player and map
joebarhouch 3:e4e1cbf750b6 75 void Engine::draw(N5110 &lcd)
joebarhouch 3:e4e1cbf750b6 76 {
joebarhouch 7:530ca713d2b2 77
joebarhouch 7:530ca713d2b2 78
joebarhouch 3:e4e1cbf750b6 79 // player
joebarhouch 3:e4e1cbf750b6 80 _p.draw(lcd);
joebarhouch 7:530ca713d2b2 81
joebarhouch 8:d19b30a6cd69 82 // map
joebarhouch 8:d19b30a6cd69 83 drawMap(lcd);
joebarhouch 8:d19b30a6cd69 84
joebarhouch 8:d19b30a6cd69 85 //enemies
joebarhouch 8:d19b30a6cd69 86 for(int i = 0; i < enemies.size(); i ++) {
joebarhouch 8:d19b30a6cd69 87 enemies.at(i).draw(lcd);
joebarhouch 8:d19b30a6cd69 88 }
joebarhouch 8:d19b30a6cd69 89
joebarhouch 8:d19b30a6cd69 90
joebarhouch 3:e4e1cbf750b6 91 }
joebarhouch 3:e4e1cbf750b6 92
joebarhouch 7:530ca713d2b2 93
joebarhouch 7:530ca713d2b2 94
joebarhouch 7:530ca713d2b2 95
joebarhouch 6:00d20886e4f8 96 ////////////////////// UPDATE //////////////////////////
joebarhouch 5:928c2eee4109 97 //provide the player file with necessary Joystick values
joebarhouch 10:9317a62bd4d0 98 //updates enemy file
joebarhouch 3:e4e1cbf750b6 99 void Engine::update(Gamepad &pad)
joebarhouch 3:e4e1cbf750b6 100 {
joebarhouch 7:530ca713d2b2 101 floorCollide();
joebarhouch 10:9317a62bd4d0 102 //spawnEnemy();
joebarhouch 8:d19b30a6cd69 103 _p.update(_d,_mag, _Ypos, _fall, _jump);
joebarhouch 7:530ca713d2b2 104
joebarhouch 8:d19b30a6cd69 105 for(int i = 0; i < enemies.size(); i ++) {
joebarhouch 10:9317a62bd4d0 106 enemies.at(i).update(2,2);
joebarhouch 8:d19b30a6cd69 107 }
joebarhouch 6:00d20886e4f8 108
joebarhouch 10:9317a62bd4d0 109 }
joebarhouch 10:9317a62bd4d0 110
joebarhouch 8:d19b30a6cd69 111
joebarhouch 10:9317a62bd4d0 112 /*
joebarhouch 10:9317a62bd4d0 113 if (_c == true) {
joebarhouch 6:00d20886e4f8 114
joebarhouch 10:9317a62bd4d0 115 //debug
joebarhouch 10:9317a62bd4d0 116 //printf("collison\n");
joebarhouch 10:9317a62bd4d0 117 } else {
joebarhouch 10:9317a62bd4d0 118
joebarhouch 10:9317a62bd4d0 119 //debug
joebarhouch 10:9317a62bd4d0 120 //printf("no collison\n");
joebarhouch 6:00d20886e4f8 121 }
joebarhouch 10:9317a62bd4d0 122 //enemmyCollide(pad);
joebarhouch 3:e4e1cbf750b6 123 }
joebarhouch 10:9317a62bd4d0 124 */
joebarhouch 10:9317a62bd4d0 125
joebarhouch 7:530ca713d2b2 126
joebarhouch 6:00d20886e4f8 127 ////////////////////// FLOOR COLLISION //////////////////////////
joebarhouch 7:530ca713d2b2 128 void Engine::floorCollide()
joebarhouch 6:00d20886e4f8 129 {
joebarhouch 8:d19b30a6cd69 130
joebarhouch 7:530ca713d2b2 131 int a;
joebarhouch 8:d19b30a6cd69 132 Vector4 coords[mapSize];
joebarhouch 6:00d20886e4f8 133 Vector2D player = _p.get_pos();
joebarhouch 8:d19b30a6cd69 134 //coordinates of platforms
joebarhouch 8:d19b30a6cd69 135 for(int i = 0; i < mapSize; i++) {
joebarhouch 7:530ca713d2b2 136 coords[i] = maps[i].get_pos();
joebarhouch 7:530ca713d2b2 137 }
joebarhouch 7:530ca713d2b2 138
joebarhouch 6:00d20886e4f8 139
joebarhouch 8:d19b30a6cd69 140 if(_c == false) {
joebarhouch 7:530ca713d2b2 141 _fall = true;
joebarhouch 7:530ca713d2b2 142 for(int j=0; j < mapSize; j++) {
joebarhouch 8:d19b30a6cd69 143 if(player.x+5 >= coords[j].x && player.x+1 <= coords[j].x + coords[j].width && player.y+9 >= coords[j].y && player.y+9 <= coords[j].y + coords[j].height) {
joebarhouch 7:530ca713d2b2 144 a = j;
joebarhouch 8:d19b30a6cd69 145 _c = true;
joebarhouch 7:530ca713d2b2 146 //printf("%i, %i\n", player.x, player.y );
joebarhouch 7:530ca713d2b2 147 }
joebarhouch 7:530ca713d2b2 148 }
joebarhouch 6:00d20886e4f8 149 }
joebarhouch 8:d19b30a6cd69 150
joebarhouch 8:d19b30a6cd69 151 if(_c == true) {
joebarhouch 8:d19b30a6cd69 152 _Ypos = coords[a].y - 8;
joebarhouch 8:d19b30a6cd69 153 _fall = false;
joebarhouch 8:d19b30a6cd69 154 _c = false;
joebarhouch 8:d19b30a6cd69 155 }
joebarhouch 8:d19b30a6cd69 156 }
joebarhouch 8:d19b30a6cd69 157
joebarhouch 8:d19b30a6cd69 158
joebarhouch 8:d19b30a6cd69 159 ////////////////////// SPAWN ENEMY /////////////////////////////////////
joebarhouch 8:d19b30a6cd69 160 void Engine::spawnEnemy()
joebarhouch 8:d19b30a6cd69 161 {
joebarhouch 9:9830d3a78572 162
joebarhouch 6:00d20886e4f8 163 }
joebarhouch 3:e4e1cbf750b6 164
joebarhouch 5:928c2eee4109 165
joebarhouch 8:d19b30a6cd69 166