ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Engine/Engine.cpp@13:cb5ed2f0cbd5, 2020-05-27 (annotated)
- Committer:
- joebarhouch
- Date:
- Wed May 27 04:36:22 2020 +0000
- Revision:
- 13:cb5ed2f0cbd5
- Parent:
- 12:eb8d30593e95
- Child:
- 14:58887d7e1072
Coin reset
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 | 12:eb8d30593e95 | 46 | ko1 = 0; |
joebarhouch | 12:eb8d30593e95 | 47 | ko2 = 0; |
joebarhouch | 8:d19b30a6cd69 | 48 | |
joebarhouch | 7:530ca713d2b2 | 49 | //physics parameters |
joebarhouch | 7:530ca713d2b2 | 50 | _Ypos = 0; |
joebarhouch | 7:530ca713d2b2 | 51 | _fall = true; |
joebarhouch | 8:d19b30a6cd69 | 52 | _c = false; |
joebarhouch | 9:9830d3a78572 | 53 | //rands |
joebarhouch | 9:9830d3a78572 | 54 | srand(time(NULL)); |
joebarhouch | 12:eb8d30593e95 | 55 | |
joebarhouch | 12:eb8d30593e95 | 56 | //coins |
joebarhouch | 12:eb8d30593e95 | 57 | coin.init(); |
joebarhouch | 3:e4e1cbf750b6 | 58 | } |
joebarhouch | 3:e4e1cbf750b6 | 59 | |
joebarhouch | 6:00d20886e4f8 | 60 | |
joebarhouch | 6:00d20886e4f8 | 61 | |
joebarhouch | 6:00d20886e4f8 | 62 | ////////////////////// INPUT ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 63 | //reads direction and magnitude from the JOYSTICK to control the player |
joebarhouch | 3:e4e1cbf750b6 | 64 | void Engine::read_input(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 65 | { |
joebarhouch | 3:e4e1cbf750b6 | 66 | _d = pad.get_direction(); |
joebarhouch | 3:e4e1cbf750b6 | 67 | _mag = pad.get_mag(); |
joebarhouch | 8:d19b30a6cd69 | 68 | _jump = pad.B_held(); |
joebarhouch | 8:d19b30a6cd69 | 69 | //printf("%s", _jump ? "true\n" : "false\n"); |
joebarhouch | 3:e4e1cbf750b6 | 70 | } |
joebarhouch | 3:e4e1cbf750b6 | 71 | |
joebarhouch | 7:530ca713d2b2 | 72 | |
joebarhouch | 7:530ca713d2b2 | 73 | |
joebarhouch | 7:530ca713d2b2 | 74 | |
joebarhouch | 7:530ca713d2b2 | 75 | |
joebarhouch | 6:00d20886e4f8 | 76 | ////////////////////// DRAW /////////////////////////// |
joebarhouch | 6:00d20886e4f8 | 77 | //draw both player and map |
joebarhouch | 3:e4e1cbf750b6 | 78 | void Engine::draw(N5110 &lcd) |
joebarhouch | 3:e4e1cbf750b6 | 79 | { |
joebarhouch | 7:530ca713d2b2 | 80 | |
joebarhouch | 7:530ca713d2b2 | 81 | |
joebarhouch | 3:e4e1cbf750b6 | 82 | // player |
joebarhouch | 3:e4e1cbf750b6 | 83 | _p.draw(lcd); |
joebarhouch | 7:530ca713d2b2 | 84 | |
joebarhouch | 8:d19b30a6cd69 | 85 | // map |
joebarhouch | 8:d19b30a6cd69 | 86 | drawMap(lcd); |
joebarhouch | 8:d19b30a6cd69 | 87 | |
joebarhouch | 8:d19b30a6cd69 | 88 | //enemies |
joebarhouch | 8:d19b30a6cd69 | 89 | for(int i = 0; i < enemies.size(); i ++) { |
joebarhouch | 8:d19b30a6cd69 | 90 | enemies.at(i).draw(lcd); |
joebarhouch | 8:d19b30a6cd69 | 91 | } |
joebarhouch | 12:eb8d30593e95 | 92 | |
joebarhouch | 12:eb8d30593e95 | 93 | //coin |
joebarhouch | 12:eb8d30593e95 | 94 | coin.draw(lcd); |
joebarhouch | 12:eb8d30593e95 | 95 | |
joebarhouch | 3:e4e1cbf750b6 | 96 | } |
joebarhouch | 3:e4e1cbf750b6 | 97 | |
joebarhouch | 7:530ca713d2b2 | 98 | |
joebarhouch | 7:530ca713d2b2 | 99 | |
joebarhouch | 7:530ca713d2b2 | 100 | |
joebarhouch | 6:00d20886e4f8 | 101 | ////////////////////// UPDATE ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 102 | //provide the player file with necessary Joystick values |
joebarhouch | 10:9317a62bd4d0 | 103 | //updates enemy file |
joebarhouch | 3:e4e1cbf750b6 | 104 | void Engine::update(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 105 | { |
joebarhouch | 13:cb5ed2f0cbd5 | 106 | //fellDown(); |
joebarhouch | 13:cb5ed2f0cbd5 | 107 | coinTaken(); |
joebarhouch | 7:530ca713d2b2 | 108 | floorCollide(); |
joebarhouch | 12:eb8d30593e95 | 109 | //enemyCollide(); |
joebarhouch | 10:9317a62bd4d0 | 110 | //spawnEnemy(); |
joebarhouch | 8:d19b30a6cd69 | 111 | _p.update(_d,_mag, _Ypos, _fall, _jump); |
joebarhouch | 7:530ca713d2b2 | 112 | |
joebarhouch | 8:d19b30a6cd69 | 113 | for(int i = 0; i < enemies.size(); i ++) { |
joebarhouch | 11:b3024ab59fa5 | 114 | enemies.at(i).update(2); |
joebarhouch | 8:d19b30a6cd69 | 115 | } |
joebarhouch | 6:00d20886e4f8 | 116 | |
joebarhouch | 10:9317a62bd4d0 | 117 | } |
joebarhouch | 10:9317a62bd4d0 | 118 | |
joebarhouch | 8:d19b30a6cd69 | 119 | |
joebarhouch | 10:9317a62bd4d0 | 120 | /* |
joebarhouch | 10:9317a62bd4d0 | 121 | if (_c == true) { |
joebarhouch | 6:00d20886e4f8 | 122 | |
joebarhouch | 10:9317a62bd4d0 | 123 | //debug |
joebarhouch | 10:9317a62bd4d0 | 124 | //printf("collison\n"); |
joebarhouch | 10:9317a62bd4d0 | 125 | } else { |
joebarhouch | 10:9317a62bd4d0 | 126 | |
joebarhouch | 10:9317a62bd4d0 | 127 | //debug |
joebarhouch | 10:9317a62bd4d0 | 128 | //printf("no collison\n"); |
joebarhouch | 6:00d20886e4f8 | 129 | } |
joebarhouch | 10:9317a62bd4d0 | 130 | //enemmyCollide(pad); |
joebarhouch | 3:e4e1cbf750b6 | 131 | } |
joebarhouch | 10:9317a62bd4d0 | 132 | */ |
joebarhouch | 10:9317a62bd4d0 | 133 | |
joebarhouch | 7:530ca713d2b2 | 134 | |
joebarhouch | 6:00d20886e4f8 | 135 | ////////////////////// FLOOR COLLISION ////////////////////////// |
joebarhouch | 7:530ca713d2b2 | 136 | void Engine::floorCollide() |
joebarhouch | 6:00d20886e4f8 | 137 | { |
joebarhouch | 8:d19b30a6cd69 | 138 | |
joebarhouch | 7:530ca713d2b2 | 139 | int a; |
joebarhouch | 8:d19b30a6cd69 | 140 | Vector4 coords[mapSize]; |
joebarhouch | 12:eb8d30593e95 | 141 | player = _p.get_pos(); |
joebarhouch | 8:d19b30a6cd69 | 142 | //coordinates of platforms |
joebarhouch | 8:d19b30a6cd69 | 143 | for(int i = 0; i < mapSize; i++) { |
joebarhouch | 7:530ca713d2b2 | 144 | coords[i] = maps[i].get_pos(); |
joebarhouch | 7:530ca713d2b2 | 145 | } |
joebarhouch | 7:530ca713d2b2 | 146 | |
joebarhouch | 6:00d20886e4f8 | 147 | |
joebarhouch | 8:d19b30a6cd69 | 148 | if(_c == false) { |
joebarhouch | 7:530ca713d2b2 | 149 | _fall = true; |
joebarhouch | 7:530ca713d2b2 | 150 | for(int j=0; j < mapSize; j++) { |
joebarhouch | 8:d19b30a6cd69 | 151 | 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 | 152 | a = j; |
joebarhouch | 8:d19b30a6cd69 | 153 | _c = true; |
joebarhouch | 7:530ca713d2b2 | 154 | //printf("%i, %i\n", player.x, player.y ); |
joebarhouch | 7:530ca713d2b2 | 155 | } |
joebarhouch | 7:530ca713d2b2 | 156 | } |
joebarhouch | 6:00d20886e4f8 | 157 | } |
joebarhouch | 8:d19b30a6cd69 | 158 | |
joebarhouch | 8:d19b30a6cd69 | 159 | if(_c == true) { |
joebarhouch | 8:d19b30a6cd69 | 160 | _Ypos = coords[a].y - 8; |
joebarhouch | 8:d19b30a6cd69 | 161 | _fall = false; |
joebarhouch | 8:d19b30a6cd69 | 162 | _c = false; |
joebarhouch | 8:d19b30a6cd69 | 163 | } |
joebarhouch | 8:d19b30a6cd69 | 164 | } |
joebarhouch | 8:d19b30a6cd69 | 165 | |
joebarhouch | 8:d19b30a6cd69 | 166 | |
joebarhouch | 13:cb5ed2f0cbd5 | 167 | ////////////////////// GOT COIN ///////////////////////////////////////// |
joebarhouch | 13:cb5ed2f0cbd5 | 168 | void Engine::coinTaken(){ |
joebarhouch | 13:cb5ed2f0cbd5 | 169 | coinPos = coin.get_pos(); |
joebarhouch | 13:cb5ed2f0cbd5 | 170 | if(player.x+4 >= coinPos.x-2 & player.y+4 >= coinPos.y-2 & player.x+4 <= coinPos.x+2 & player.y+4 <= coinPos.x+2){ |
joebarhouch | 13:cb5ed2f0cbd5 | 171 | coin.set_pos(10, 10); |
joebarhouch | 13:cb5ed2f0cbd5 | 172 | } |
joebarhouch | 13:cb5ed2f0cbd5 | 173 | } |
joebarhouch | 13:cb5ed2f0cbd5 | 174 | |
joebarhouch | 13:cb5ed2f0cbd5 | 175 | |
joebarhouch | 3:e4e1cbf750b6 | 176 | |
joebarhouch | 11:b3024ab59fa5 | 177 | ////////////////////// ENNEMY COLLIDE /////////////////////////////////// |
joebarhouch | 11:b3024ab59fa5 | 178 | bool Engine::enemyCollide() |
joebarhouch | 11:b3024ab59fa5 | 179 | { |
joebarhouch | 11:b3024ab59fa5 | 180 | vector <Vector2D> epos; |
joebarhouch | 12:eb8d30593e95 | 181 | |
joebarhouch | 11:b3024ab59fa5 | 182 | |
joebarhouch | 11:b3024ab59fa5 | 183 | for (int i = 0; i < enemies.size(); i++) { |
joebarhouch | 11:b3024ab59fa5 | 184 | epos.push_back(enemies.at(i).get_pos()); |
joebarhouch | 11:b3024ab59fa5 | 185 | //printf("coord %i, at %f, %f\n", i, epos.at(i).x, epos.at(i).y); |
joebarhouch | 11:b3024ab59fa5 | 186 | } |
joebarhouch | 11:b3024ab59fa5 | 187 | for (int i=0; i<epos.size(); i++) { |
joebarhouch | 11:b3024ab59fa5 | 188 | 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 | 12:eb8d30593e95 | 189 | ko1 = 1; |
joebarhouch | 11:b3024ab59fa5 | 190 | //printf("KO\n"); |
joebarhouch | 11:b3024ab59fa5 | 191 | } |
joebarhouch | 11:b3024ab59fa5 | 192 | } |
joebarhouch | 12:eb8d30593e95 | 193 | return ko1; |
joebarhouch | 12:eb8d30593e95 | 194 | } |
joebarhouch | 12:eb8d30593e95 | 195 | |
joebarhouch | 12:eb8d30593e95 | 196 | ///////////////////// FELL DOWN ///////////////////////////////////////////// |
joebarhouch | 12:eb8d30593e95 | 197 | bool Engine::fellDown() |
joebarhouch | 12:eb8d30593e95 | 198 | { |
joebarhouch | 12:eb8d30593e95 | 199 | if(player.y > HEIGHT - 2) { |
joebarhouch | 12:eb8d30593e95 | 200 | ko2 = 1; |
joebarhouch | 12:eb8d30593e95 | 201 | } else { |
joebarhouch | 12:eb8d30593e95 | 202 | ko2 = 0; |
joebarhouch | 12:eb8d30593e95 | 203 | } |
joebarhouch | 12:eb8d30593e95 | 204 | return ko2; |
joebarhouch | 11:b3024ab59fa5 | 205 | } |
joebarhouch | 11:b3024ab59fa5 | 206 | |
joebarhouch | 12:eb8d30593e95 | 207 | |
joebarhouch | 12:eb8d30593e95 | 208 | /////////////////////// KO /////////////////////////////////////////////////// |
joebarhouch | 12:eb8d30593e95 | 209 | bool Engine::koed() |
joebarhouch | 12:eb8d30593e95 | 210 | { |
joebarhouch | 12:eb8d30593e95 | 211 | |
joebarhouch | 12:eb8d30593e95 | 212 | if( ko1 == 1 || ko2 == 1) { |
joebarhouch | 12:eb8d30593e95 | 213 | return true; |
joebarhouch | 12:eb8d30593e95 | 214 | } else { |
joebarhouch | 12:eb8d30593e95 | 215 | return false; |
joebarhouch | 12:eb8d30593e95 | 216 | } |
joebarhouch | 12:eb8d30593e95 | 217 | } |
joebarhouch | 12:eb8d30593e95 | 218 | |
joebarhouch | 12:eb8d30593e95 | 219 | |
joebarhouch | 11:b3024ab59fa5 | 220 | ////////////////////// GAME OVER /////////////////////////////////////////// |
joebarhouch | 12:eb8d30593e95 | 221 | void Engine::gameOver(N5110 &lcd) |
joebarhouch | 12:eb8d30593e95 | 222 | { |
joebarhouch | 12:eb8d30593e95 | 223 | lcd.inverseMode(); |
joebarhouch | 12:eb8d30593e95 | 224 | wait(0.5); |
joebarhouch | 12:eb8d30593e95 | 225 | lcd.normalMode(); |
joebarhouch | 12:eb8d30593e95 | 226 | wait(0.5); |
joebarhouch | 12:eb8d30593e95 | 227 | lcd.inverseMode(); |
joebarhouch | 12:eb8d30593e95 | 228 | wait(0.5); |
joebarhouch | 12:eb8d30593e95 | 229 | _p.update(_d, 0, _Ypos, true, false); |
joebarhouch | 12:eb8d30593e95 | 230 | _p.draw(lcd); |
joebarhouch | 12:eb8d30593e95 | 231 | //printf("KO RUN\n"); |
joebarhouch | 12:eb8d30593e95 | 232 | } |
joebarhouch | 5:928c2eee4109 | 233 | |
joebarhouch | 8:d19b30a6cd69 | 234 |