ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Engine/Engine.cpp@11:b3024ab59fa5, 2020-05-27 (annotated)
- Committer:
- joebarhouch
- Date:
- Wed May 27 03:52:11 2020 +0000
- Revision:
- 11:b3024ab59fa5
- Parent:
- 10:9317a62bd4d0
- Child:
- 12:eb8d30593e95
Coin class and enemy collision;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joebarhouch | 3:e4e1cbf750b6 | 1 | #include "Engine.h" |
joebarhouch | 3:e4e1cbf750b6 | 2 | |
joebarhouch | 11:b3024ab59fa5 | 3 | Platform maps[7] = {Platform(0, 10, 20, 3), Platform(70, 14, 14, 3), Platform(40, 25, 30, 3), Platform(65, 44, 15, 3), Platform(5, 35, 20, 3), Platform(25, 17, 5, 3),Platform(40, 40, 10, 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 | 11:b3024ab59fa5 | 46 | ko = 0; |
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 | 3:e4e1cbf750b6 | 89 | } |
joebarhouch | 3:e4e1cbf750b6 | 90 | |
joebarhouch | 7:530ca713d2b2 | 91 | |
joebarhouch | 7:530ca713d2b2 | 92 | |
joebarhouch | 7:530ca713d2b2 | 93 | |
joebarhouch | 6:00d20886e4f8 | 94 | ////////////////////// UPDATE ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 95 | //provide the player file with necessary Joystick values |
joebarhouch | 10:9317a62bd4d0 | 96 | //updates enemy file |
joebarhouch | 3:e4e1cbf750b6 | 97 | void Engine::update(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 98 | { |
joebarhouch | 7:530ca713d2b2 | 99 | floorCollide(); |
joebarhouch | 11:b3024ab59fa5 | 100 | enemyCollide(); |
joebarhouch | 10:9317a62bd4d0 | 101 | //spawnEnemy(); |
joebarhouch | 8:d19b30a6cd69 | 102 | _p.update(_d,_mag, _Ypos, _fall, _jump); |
joebarhouch | 7:530ca713d2b2 | 103 | |
joebarhouch | 8:d19b30a6cd69 | 104 | for(int i = 0; i < enemies.size(); i ++) { |
joebarhouch | 11:b3024ab59fa5 | 105 | enemies.at(i).update(2); |
joebarhouch | 8:d19b30a6cd69 | 106 | } |
joebarhouch | 6:00d20886e4f8 | 107 | |
joebarhouch | 10:9317a62bd4d0 | 108 | } |
joebarhouch | 10:9317a62bd4d0 | 109 | |
joebarhouch | 8:d19b30a6cd69 | 110 | |
joebarhouch | 10:9317a62bd4d0 | 111 | /* |
joebarhouch | 10:9317a62bd4d0 | 112 | if (_c == true) { |
joebarhouch | 6:00d20886e4f8 | 113 | |
joebarhouch | 10:9317a62bd4d0 | 114 | //debug |
joebarhouch | 10:9317a62bd4d0 | 115 | //printf("collison\n"); |
joebarhouch | 10:9317a62bd4d0 | 116 | } else { |
joebarhouch | 10:9317a62bd4d0 | 117 | |
joebarhouch | 10:9317a62bd4d0 | 118 | //debug |
joebarhouch | 10:9317a62bd4d0 | 119 | //printf("no collison\n"); |
joebarhouch | 6:00d20886e4f8 | 120 | } |
joebarhouch | 10:9317a62bd4d0 | 121 | //enemmyCollide(pad); |
joebarhouch | 3:e4e1cbf750b6 | 122 | } |
joebarhouch | 10:9317a62bd4d0 | 123 | */ |
joebarhouch | 10:9317a62bd4d0 | 124 | |
joebarhouch | 7:530ca713d2b2 | 125 | |
joebarhouch | 6:00d20886e4f8 | 126 | ////////////////////// FLOOR COLLISION ////////////////////////// |
joebarhouch | 7:530ca713d2b2 | 127 | void Engine::floorCollide() |
joebarhouch | 6:00d20886e4f8 | 128 | { |
joebarhouch | 8:d19b30a6cd69 | 129 | |
joebarhouch | 7:530ca713d2b2 | 130 | int a; |
joebarhouch | 8:d19b30a6cd69 | 131 | Vector4 coords[mapSize]; |
joebarhouch | 6:00d20886e4f8 | 132 | Vector2D player = _p.get_pos(); |
joebarhouch | 8:d19b30a6cd69 | 133 | //coordinates of platforms |
joebarhouch | 8:d19b30a6cd69 | 134 | for(int i = 0; i < mapSize; i++) { |
joebarhouch | 7:530ca713d2b2 | 135 | coords[i] = maps[i].get_pos(); |
joebarhouch | 7:530ca713d2b2 | 136 | } |
joebarhouch | 7:530ca713d2b2 | 137 | |
joebarhouch | 6:00d20886e4f8 | 138 | |
joebarhouch | 8:d19b30a6cd69 | 139 | if(_c == false) { |
joebarhouch | 7:530ca713d2b2 | 140 | _fall = true; |
joebarhouch | 7:530ca713d2b2 | 141 | for(int j=0; j < mapSize; j++) { |
joebarhouch | 8:d19b30a6cd69 | 142 | 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 | 143 | a = j; |
joebarhouch | 8:d19b30a6cd69 | 144 | _c = true; |
joebarhouch | 7:530ca713d2b2 | 145 | //printf("%i, %i\n", player.x, player.y ); |
joebarhouch | 7:530ca713d2b2 | 146 | } |
joebarhouch | 7:530ca713d2b2 | 147 | } |
joebarhouch | 6:00d20886e4f8 | 148 | } |
joebarhouch | 8:d19b30a6cd69 | 149 | |
joebarhouch | 8:d19b30a6cd69 | 150 | if(_c == true) { |
joebarhouch | 8:d19b30a6cd69 | 151 | _Ypos = coords[a].y - 8; |
joebarhouch | 8:d19b30a6cd69 | 152 | _fall = false; |
joebarhouch | 8:d19b30a6cd69 | 153 | _c = false; |
joebarhouch | 8:d19b30a6cd69 | 154 | } |
joebarhouch | 8:d19b30a6cd69 | 155 | } |
joebarhouch | 8:d19b30a6cd69 | 156 | |
joebarhouch | 8:d19b30a6cd69 | 157 | |
joebarhouch | 8:d19b30a6cd69 | 158 | ////////////////////// SPAWN ENEMY ///////////////////////////////////// |
joebarhouch | 8:d19b30a6cd69 | 159 | void Engine::spawnEnemy() |
joebarhouch | 8:d19b30a6cd69 | 160 | { |
joebarhouch | 9:9830d3a78572 | 161 | |
joebarhouch | 6:00d20886e4f8 | 162 | } |
joebarhouch | 3:e4e1cbf750b6 | 163 | |
joebarhouch | 11:b3024ab59fa5 | 164 | ////////////////////// ENNEMY COLLIDE /////////////////////////////////// |
joebarhouch | 11:b3024ab59fa5 | 165 | bool Engine::enemyCollide() |
joebarhouch | 11:b3024ab59fa5 | 166 | { |
joebarhouch | 11:b3024ab59fa5 | 167 | vector <Vector2D> epos; |
joebarhouch | 11:b3024ab59fa5 | 168 | Vector2D player = _p.get_pos(); |
joebarhouch | 11:b3024ab59fa5 | 169 | |
joebarhouch | 11:b3024ab59fa5 | 170 | for (int i = 0; i < enemies.size(); i++) { |
joebarhouch | 11:b3024ab59fa5 | 171 | epos.push_back(enemies.at(i).get_pos()); |
joebarhouch | 11:b3024ab59fa5 | 172 | //printf("coord %i, at %f, %f\n", i, epos.at(i).x, epos.at(i).y); |
joebarhouch | 11:b3024ab59fa5 | 173 | } |
joebarhouch | 11:b3024ab59fa5 | 174 | for (int i=0; i<epos.size(); i++) { |
joebarhouch | 11:b3024ab59fa5 | 175 | if(player.x < epos.at(i).x + 7 && player.y < epos.at(i).y + 5 && player.x+9 > epos.at(i).x && player.y> epos.at(i).y) { |
joebarhouch | 11:b3024ab59fa5 | 176 | ko = 1; |
joebarhouch | 11:b3024ab59fa5 | 177 | //printf("KO\n"); |
joebarhouch | 11:b3024ab59fa5 | 178 | } |
joebarhouch | 11:b3024ab59fa5 | 179 | } |
joebarhouch | 11:b3024ab59fa5 | 180 | return ko; |
joebarhouch | 11:b3024ab59fa5 | 181 | } |
joebarhouch | 11:b3024ab59fa5 | 182 | |
joebarhouch | 11:b3024ab59fa5 | 183 | ////////////////////// GAME OVER /////////////////////////////////////////// |
joebarhouch | 11:b3024ab59fa5 | 184 | void Engine::gameOver(N5110 &lcd){ |
joebarhouch | 11:b3024ab59fa5 | 185 | lcd.inverseMode(); |
joebarhouch | 11:b3024ab59fa5 | 186 | wait(0.5); |
joebarhouch | 11:b3024ab59fa5 | 187 | lcd.normalMode(); |
joebarhouch | 11:b3024ab59fa5 | 188 | wait(0.5); |
joebarhouch | 11:b3024ab59fa5 | 189 | lcd.inverseMode(); |
joebarhouch | 11:b3024ab59fa5 | 190 | wait(0.5); |
joebarhouch | 11:b3024ab59fa5 | 191 | _p.update(_d, 0, _Ypos, true, _jump); |
joebarhouch | 11:b3024ab59fa5 | 192 | _p.draw(lcd); |
joebarhouch | 11:b3024ab59fa5 | 193 | printf("KO RUN\n"); |
joebarhouch | 11:b3024ab59fa5 | 194 | } |
joebarhouch | 5:928c2eee4109 | 195 | |
joebarhouch | 8:d19b30a6cd69 | 196 |