![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test 1 doc
Engine/Engine.cpp@9:9830d3a78572, 2020-05-27 (annotated)
- Committer:
- joebarhouch
- Date:
- Wed May 27 00:49:39 2020 +0000
- Revision:
- 9:9830d3a78572
- Parent:
- 8:d19b30a6cd69
- Child:
- 10:9317a62bd4d0
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 | 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 | 8:d19b30a6cd69 | 5 | vector <Enemy> enemies; |
joebarhouch | 7:530ca713d2b2 | 6 | |
joebarhouch | 7:530ca713d2b2 | 7 | |
joebarhouch | 7:530ca713d2b2 | 8 | ////////////////////// DRAW MAP ////////////////////////// |
joebarhouch | 7:530ca713d2b2 | 9 | void drawMap(N5110 &lcd) |
joebarhouch | 7:530ca713d2b2 | 10 | { |
joebarhouch | 7:530ca713d2b2 | 11 | for (int i = 0; i < mapSize; i++) { |
joebarhouch | 7:530ca713d2b2 | 12 | maps[i].draw(lcd); |
joebarhouch | 7:530ca713d2b2 | 13 | |
joebarhouch | 7:530ca713d2b2 | 14 | //debugs |
joebarhouch | 7:530ca713d2b2 | 15 | //coords = maps[i].get_pos(); |
joebarhouch | 7:530ca713d2b2 | 16 | //printf("x: %i, y: %i,w: %i,h: %i \n",coords.x, coords.y, coords.width, coords.height); |
joebarhouch | 7:530ca713d2b2 | 17 | } |
joebarhouch | 7:530ca713d2b2 | 18 | //debugs |
joebarhouch | 7:530ca713d2b2 | 19 | //printf("-----------------------------------------\n"); |
joebarhouch | 7:530ca713d2b2 | 20 | } |
joebarhouch | 7:530ca713d2b2 | 21 | |
joebarhouch | 7:530ca713d2b2 | 22 | |
joebarhouch | 7:530ca713d2b2 | 23 | |
joebarhouch | 3:e4e1cbf750b6 | 24 | Engine::Engine() |
joebarhouch | 3:e4e1cbf750b6 | 25 | { |
joebarhouch | 3:e4e1cbf750b6 | 26 | } |
joebarhouch | 3:e4e1cbf750b6 | 27 | Engine::~Engine() |
joebarhouch | 3:e4e1cbf750b6 | 28 | { |
joebarhouch | 3:e4e1cbf750b6 | 29 | } |
joebarhouch | 3:e4e1cbf750b6 | 30 | |
joebarhouch | 6:00d20886e4f8 | 31 | |
joebarhouch | 6:00d20886e4f8 | 32 | ////////////////////// INIT //////////////////////////// |
joebarhouch | 3:e4e1cbf750b6 | 33 | void Engine::init() |
joebarhouch | 3:e4e1cbf750b6 | 34 | { |
joebarhouch | 3:e4e1cbf750b6 | 35 | //init coord |
joebarhouch | 3:e4e1cbf750b6 | 36 | _px = WIDTH / 2; |
joebarhouch | 7:530ca713d2b2 | 37 | _py = 5; |
joebarhouch | 6:00d20886e4f8 | 38 | |
joebarhouch | 3:e4e1cbf750b6 | 39 | //init call |
joebarhouch | 3:e4e1cbf750b6 | 40 | _p.init(_px, _py); |
joebarhouch | 7:530ca713d2b2 | 41 | |
joebarhouch | 8:d19b30a6cd69 | 42 | //ennemy |
joebarhouch | 8:d19b30a6cd69 | 43 | enemies.push_back(Enemy(0, 0)); |
joebarhouch | 8:d19b30a6cd69 | 44 | enemies.push_back(Enemy(75, 0)); |
joebarhouch | 8:d19b30a6cd69 | 45 | |
joebarhouch | 8:d19b30a6cd69 | 46 | |
joebarhouch | 7:530ca713d2b2 | 47 | //physics parameters |
joebarhouch | 7:530ca713d2b2 | 48 | _Ypos = 0; |
joebarhouch | 7:530ca713d2b2 | 49 | _fall = true; |
joebarhouch | 8:d19b30a6cd69 | 50 | _c = false; |
joebarhouch | 8:d19b30a6cd69 | 51 | |
joebarhouch | 9:9830d3a78572 | 52 | //rands |
joebarhouch | 9:9830d3a78572 | 53 | srand(time(NULL)); |
joebarhouch | 3:e4e1cbf750b6 | 54 | } |
joebarhouch | 3:e4e1cbf750b6 | 55 | |
joebarhouch | 6:00d20886e4f8 | 56 | |
joebarhouch | 6:00d20886e4f8 | 57 | |
joebarhouch | 6:00d20886e4f8 | 58 | ////////////////////// INPUT ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 59 | //reads direction and magnitude from the JOYSTICK to control the player |
joebarhouch | 3:e4e1cbf750b6 | 60 | void Engine::read_input(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 61 | { |
joebarhouch | 3:e4e1cbf750b6 | 62 | _d = pad.get_direction(); |
joebarhouch | 3:e4e1cbf750b6 | 63 | _mag = pad.get_mag(); |
joebarhouch | 8:d19b30a6cd69 | 64 | _jump = pad.B_held(); |
joebarhouch | 8:d19b30a6cd69 | 65 | //printf("%s", _jump ? "true\n" : "false\n"); |
joebarhouch | 3:e4e1cbf750b6 | 66 | } |
joebarhouch | 3:e4e1cbf750b6 | 67 | |
joebarhouch | 7:530ca713d2b2 | 68 | |
joebarhouch | 7:530ca713d2b2 | 69 | |
joebarhouch | 7:530ca713d2b2 | 70 | |
joebarhouch | 7:530ca713d2b2 | 71 | |
joebarhouch | 6:00d20886e4f8 | 72 | ////////////////////// DRAW /////////////////////////// |
joebarhouch | 6:00d20886e4f8 | 73 | //draw both player and map |
joebarhouch | 3:e4e1cbf750b6 | 74 | void Engine::draw(N5110 &lcd) |
joebarhouch | 3:e4e1cbf750b6 | 75 | { |
joebarhouch | 7:530ca713d2b2 | 76 | |
joebarhouch | 7:530ca713d2b2 | 77 | |
joebarhouch | 3:e4e1cbf750b6 | 78 | // player |
joebarhouch | 3:e4e1cbf750b6 | 79 | _p.draw(lcd); |
joebarhouch | 7:530ca713d2b2 | 80 | |
joebarhouch | 8:d19b30a6cd69 | 81 | // map |
joebarhouch | 8:d19b30a6cd69 | 82 | drawMap(lcd); |
joebarhouch | 8:d19b30a6cd69 | 83 | |
joebarhouch | 8:d19b30a6cd69 | 84 | //enemies |
joebarhouch | 8:d19b30a6cd69 | 85 | for(int i = 0; i < enemies.size(); i ++) { |
joebarhouch | 8:d19b30a6cd69 | 86 | enemies.at(i).draw(lcd); |
joebarhouch | 8:d19b30a6cd69 | 87 | } |
joebarhouch | 8:d19b30a6cd69 | 88 | |
joebarhouch | 8:d19b30a6cd69 | 89 | |
joebarhouch | 3:e4e1cbf750b6 | 90 | } |
joebarhouch | 3:e4e1cbf750b6 | 91 | |
joebarhouch | 7:530ca713d2b2 | 92 | |
joebarhouch | 7:530ca713d2b2 | 93 | |
joebarhouch | 7:530ca713d2b2 | 94 | |
joebarhouch | 6:00d20886e4f8 | 95 | ////////////////////// UPDATE ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 96 | //provide the player file with necessary Joystick values |
joebarhouch | 3:e4e1cbf750b6 | 97 | void Engine::update(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 98 | { |
joebarhouch | 7:530ca713d2b2 | 99 | floorCollide(); |
joebarhouch | 8:d19b30a6cd69 | 100 | efloorCollide(); |
joebarhouch | 8:d19b30a6cd69 | 101 | spawnEnemy(); |
joebarhouch | 3:e4e1cbf750b6 | 102 | |
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 | 9:9830d3a78572 | 106 | enemies.at(i).update(_eYpos.at(i), _efall.at(i)); |
joebarhouch | 8:d19b30a6cd69 | 107 | //printf("Enemy %i!\n", i); |
joebarhouch | 8:d19b30a6cd69 | 108 | } |
joebarhouch | 6:00d20886e4f8 | 109 | |
joebarhouch | 8:d19b30a6cd69 | 110 | /* |
joebarhouch | 8:d19b30a6cd69 | 111 | if (_c == true) { |
joebarhouch | 8:d19b30a6cd69 | 112 | |
joebarhouch | 8:d19b30a6cd69 | 113 | //debug |
joebarhouch | 8:d19b30a6cd69 | 114 | //printf("collison\n"); |
joebarhouch | 8:d19b30a6cd69 | 115 | } else { |
joebarhouch | 6:00d20886e4f8 | 116 | |
joebarhouch | 8:d19b30a6cd69 | 117 | //debug |
joebarhouch | 8:d19b30a6cd69 | 118 | //printf("no collison\n"); |
joebarhouch | 8:d19b30a6cd69 | 119 | } |
joebarhouch | 8:d19b30a6cd69 | 120 | //enemmyCollide(pad); |
joebarhouch | 6:00d20886e4f8 | 121 | } |
joebarhouch | 8:d19b30a6cd69 | 122 | */ |
joebarhouch | 3:e4e1cbf750b6 | 123 | } |
joebarhouch | 7:530ca713d2b2 | 124 | |
joebarhouch | 6:00d20886e4f8 | 125 | ////////////////////// FLOOR COLLISION ////////////////////////// |
joebarhouch | 7:530ca713d2b2 | 126 | void Engine::floorCollide() |
joebarhouch | 6:00d20886e4f8 | 127 | { |
joebarhouch | 8:d19b30a6cd69 | 128 | |
joebarhouch | 7:530ca713d2b2 | 129 | int a; |
joebarhouch | 8:d19b30a6cd69 | 130 | Vector4 coords[mapSize]; |
joebarhouch | 6:00d20886e4f8 | 131 | Vector2D player = _p.get_pos(); |
joebarhouch | 8:d19b30a6cd69 | 132 | //coordinates of platforms |
joebarhouch | 8:d19b30a6cd69 | 133 | for(int i = 0; i < mapSize; i++) { |
joebarhouch | 7:530ca713d2b2 | 134 | coords[i] = maps[i].get_pos(); |
joebarhouch | 7:530ca713d2b2 | 135 | } |
joebarhouch | 7:530ca713d2b2 | 136 | |
joebarhouch | 6:00d20886e4f8 | 137 | |
joebarhouch | 8:d19b30a6cd69 | 138 | if(_c == false) { |
joebarhouch | 7:530ca713d2b2 | 139 | _fall = true; |
joebarhouch | 7:530ca713d2b2 | 140 | for(int j=0; j < mapSize; j++) { |
joebarhouch | 8:d19b30a6cd69 | 141 | 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 | 142 | a = j; |
joebarhouch | 8:d19b30a6cd69 | 143 | _c = true; |
joebarhouch | 7:530ca713d2b2 | 144 | //printf("%i, %i\n", player.x, player.y ); |
joebarhouch | 7:530ca713d2b2 | 145 | } |
joebarhouch | 7:530ca713d2b2 | 146 | } |
joebarhouch | 6:00d20886e4f8 | 147 | } |
joebarhouch | 8:d19b30a6cd69 | 148 | |
joebarhouch | 8:d19b30a6cd69 | 149 | if(_c == true) { |
joebarhouch | 8:d19b30a6cd69 | 150 | _Ypos = coords[a].y - 8; |
joebarhouch | 8:d19b30a6cd69 | 151 | _fall = false; |
joebarhouch | 8:d19b30a6cd69 | 152 | _c = false; |
joebarhouch | 8:d19b30a6cd69 | 153 | } |
joebarhouch | 8:d19b30a6cd69 | 154 | } |
joebarhouch | 8:d19b30a6cd69 | 155 | |
joebarhouch | 8:d19b30a6cd69 | 156 | |
joebarhouch | 8:d19b30a6cd69 | 157 | ////////////////////// SPAWN ENEMY ///////////////////////////////////// |
joebarhouch | 8:d19b30a6cd69 | 158 | void Engine::spawnEnemy() |
joebarhouch | 8:d19b30a6cd69 | 159 | { |
joebarhouch | 9:9830d3a78572 | 160 | |
joebarhouch | 9:9830d3a78572 | 161 | |
joebarhouch | 8:d19b30a6cd69 | 162 | for(int i = 0; i < enemies.size(); i ++) { |
joebarhouch | 8:d19b30a6cd69 | 163 | //printf("%s", enemies.at(i).fell() ? "true\n" : "false\n"); |
joebarhouch | 8:d19b30a6cd69 | 164 | if ( enemies.at(i).fell() == true ) { |
joebarhouch | 9:9830d3a78572 | 165 | int f = rand()%2; |
joebarhouch | 9:9830d3a78572 | 166 | if ( f == 0) { |
joebarhouch | 9:9830d3a78572 | 167 | place =1; |
joebarhouch | 9:9830d3a78572 | 168 | } else if ( f ==1) { |
joebarhouch | 9:9830d3a78572 | 169 | place = 0; |
joebarhouch | 9:9830d3a78572 | 170 | } |
joebarhouch | 9:9830d3a78572 | 171 | printf("%s\n", f ? "true" : "false"); |
joebarhouch | 9:9830d3a78572 | 172 | //enemies.erase(enemies.begin() + i); |
joebarhouch | 9:9830d3a78572 | 173 | switch(place) { |
joebarhouch | 9:9830d3a78572 | 174 | case 1: |
joebarhouch | 9:9830d3a78572 | 175 | enemies.at(i).set_pos(0,0); |
joebarhouch | 9:9830d3a78572 | 176 | //enemies.push_back(Enemy(0,0)); |
joebarhouch | 9:9830d3a78572 | 177 | break; |
joebarhouch | 9:9830d3a78572 | 178 | case 0: |
joebarhouch | 9:9830d3a78572 | 179 | enemies.at(i).set_pos(0,75); |
joebarhouch | 9:9830d3a78572 | 180 | //enemies.push_back(Enemy(0,75)); |
joebarhouch | 9:9830d3a78572 | 181 | break; |
joebarhouch | 9:9830d3a78572 | 182 | } |
joebarhouch | 9:9830d3a78572 | 183 | |
joebarhouch | 7:530ca713d2b2 | 184 | } |
joebarhouch | 8:d19b30a6cd69 | 185 | } |
joebarhouch | 7:530ca713d2b2 | 186 | |
joebarhouch | 6:00d20886e4f8 | 187 | } |
joebarhouch | 3:e4e1cbf750b6 | 188 | |
joebarhouch | 5:928c2eee4109 | 189 | |
joebarhouch | 8:d19b30a6cd69 | 190 | ////////////////////// ENNEMY FLOOR COLLISION ////////////////////////// |
joebarhouch | 8:d19b30a6cd69 | 191 | void Engine::efloorCollide() |
joebarhouch | 8:d19b30a6cd69 | 192 | { |
joebarhouch | 8:d19b30a6cd69 | 193 | vector <Vector2D> epos; |
joebarhouch | 8:d19b30a6cd69 | 194 | Vector4 coord[mapSize]; |
joebarhouch | 8:d19b30a6cd69 | 195 | int b; |
joebarhouch | 8:d19b30a6cd69 | 196 | //coordinates of platforms |
joebarhouch | 8:d19b30a6cd69 | 197 | for(int i = 0; i < mapSize; i++) { |
joebarhouch | 8:d19b30a6cd69 | 198 | coord[i] = maps[i].get_pos(); |
joebarhouch | 8:d19b30a6cd69 | 199 | } |
joebarhouch | 8:d19b30a6cd69 | 200 | |
joebarhouch | 8:d19b30a6cd69 | 201 | for(int i = 0; i <enemies.size(); i++) { |
joebarhouch | 8:d19b30a6cd69 | 202 | epos.push_back(enemies.at(i).get_pos()); |
joebarhouch | 9:9830d3a78572 | 203 | _eYpos.push_back(1); |
joebarhouch | 9:9830d3a78572 | 204 | _efall.push_back(true); |
joebarhouch | 9:9830d3a78572 | 205 | |
joebarhouch | 8:d19b30a6cd69 | 206 | //printf("pos of %i: %f, %f\n", i, epos.at(i).x, epos.at(i).y); |
joebarhouch | 8:d19b30a6cd69 | 207 | } |
joebarhouch | 8:d19b30a6cd69 | 208 | |
joebarhouch | 9:9830d3a78572 | 209 | /* |
joebarhouch | 8:d19b30a6cd69 | 210 | if(_e == false) { |
joebarhouch | 9:9830d3a78572 | 211 | //_efall = true; |
joebarhouch | 8:d19b30a6cd69 | 212 | for(int i=0; i<mapSize; i++) { |
joebarhouch | 8:d19b30a6cd69 | 213 | for(int j=0; j < epos.size(); j++) { |
joebarhouch | 8:d19b30a6cd69 | 214 | if(epos.at(j).x >= coord[i].x && epos.at(j).x+1 <= coord[i].x + coord[i].width && epos.at(j).y+6 >= coord[i].y && epos.at(j).y+6 <= coord[i].y + coord[i].height) { |
joebarhouch | 8:d19b30a6cd69 | 215 | b = j; |
joebarhouch | 8:d19b30a6cd69 | 216 | _e = true; |
joebarhouch | 8:d19b30a6cd69 | 217 | //printf("%f, %f\n", epos.at(j).x,epos.at(j).y ); |
joebarhouch | 8:d19b30a6cd69 | 218 | } |
joebarhouch | 8:d19b30a6cd69 | 219 | } |
joebarhouch | 8:d19b30a6cd69 | 220 | } |
joebarhouch | 8:d19b30a6cd69 | 221 | } |
joebarhouch | 8:d19b30a6cd69 | 222 | |
joebarhouch | 8:d19b30a6cd69 | 223 | if(_e == true) { |
joebarhouch | 9:9830d3a78572 | 224 | _eYpos.at(i) = coord[b].y - 8; |
joebarhouch | 9:9830d3a78572 | 225 | _efall.at(i) = false; |
joebarhouch | 8:d19b30a6cd69 | 226 | _e = false; |
joebarhouch | 8:d19b30a6cd69 | 227 | //printf("enemy fall"); |
joebarhouch | 8:d19b30a6cd69 | 228 | } |
joebarhouch | 9:9830d3a78572 | 229 | */ |
joebarhouch | 9:9830d3a78572 | 230 | for(int i=0; i < mapSize; i++){ |
joebarhouch | 9:9830d3a78572 | 231 | for(int j=0; j< epos.size(); j++){ |
joebarhouch | 9:9830d3a78572 | 232 | if(epos.at(j).x >= coord[i].x && epos.at(j).x+1 <= coord[i].x + coord[i].width && epos.at(j).y+6 >= coord[i].y && epos.at(j).y+6 <= coord[i].y + coord[i].height){ |
joebarhouch | 9:9830d3a78572 | 233 | _eYpos.at(i) = coord[i].y - 8; |
joebarhouch | 9:9830d3a78572 | 234 | _efall.at(i) = false; |
joebarhouch | 9:9830d3a78572 | 235 | } |
joebarhouch | 9:9830d3a78572 | 236 | else{ |
joebarhouch | 9:9830d3a78572 | 237 | _efall.at(i) = true; |
joebarhouch | 9:9830d3a78572 | 238 | } |
joebarhouch | 9:9830d3a78572 | 239 | } |
joebarhouch | 9:9830d3a78572 | 240 | } |
joebarhouch | 8:d19b30a6cd69 | 241 | |
joebarhouch | 8:d19b30a6cd69 | 242 | } |
joebarhouch | 8:d19b30a6cd69 | 243 |