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