test 1 doc

Dependencies:   mbed Gamepad2

Committer:
joebarhouch
Date:
Wed May 27 04:22:47 2020 +0000
Revision:
12:eb8d30593e95
Parent:
11:b3024ab59fa5
Child:
13:cb5ed2f0cbd5
dying scenarios

Who changed what in which revision?

UserRevisionLine numberNew 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 12:eb8d30593e95 106 fellDown();
joebarhouch 7:530ca713d2b2 107 floorCollide();
joebarhouch 12:eb8d30593e95 108 //enemyCollide();
joebarhouch 10:9317a62bd4d0 109 //spawnEnemy();
joebarhouch 8:d19b30a6cd69 110 _p.update(_d,_mag, _Ypos, _fall, _jump);
joebarhouch 7:530ca713d2b2 111
joebarhouch 8:d19b30a6cd69 112 for(int i = 0; i < enemies.size(); i ++) {
joebarhouch 11:b3024ab59fa5 113 enemies.at(i).update(2);
joebarhouch 8:d19b30a6cd69 114 }
joebarhouch 6:00d20886e4f8 115
joebarhouch 10:9317a62bd4d0 116 }
joebarhouch 10:9317a62bd4d0 117
joebarhouch 8:d19b30a6cd69 118
joebarhouch 10:9317a62bd4d0 119 /*
joebarhouch 10:9317a62bd4d0 120 if (_c == true) {
joebarhouch 6:00d20886e4f8 121
joebarhouch 10:9317a62bd4d0 122 //debug
joebarhouch 10:9317a62bd4d0 123 //printf("collison\n");
joebarhouch 10:9317a62bd4d0 124 } else {
joebarhouch 10:9317a62bd4d0 125
joebarhouch 10:9317a62bd4d0 126 //debug
joebarhouch 10:9317a62bd4d0 127 //printf("no collison\n");
joebarhouch 6:00d20886e4f8 128 }
joebarhouch 10:9317a62bd4d0 129 //enemmyCollide(pad);
joebarhouch 3:e4e1cbf750b6 130 }
joebarhouch 10:9317a62bd4d0 131 */
joebarhouch 10:9317a62bd4d0 132
joebarhouch 7:530ca713d2b2 133
joebarhouch 6:00d20886e4f8 134 ////////////////////// FLOOR COLLISION //////////////////////////
joebarhouch 7:530ca713d2b2 135 void Engine::floorCollide()
joebarhouch 6:00d20886e4f8 136 {
joebarhouch 8:d19b30a6cd69 137
joebarhouch 7:530ca713d2b2 138 int a;
joebarhouch 8:d19b30a6cd69 139 Vector4 coords[mapSize];
joebarhouch 12:eb8d30593e95 140 player = _p.get_pos();
joebarhouch 8:d19b30a6cd69 141 //coordinates of platforms
joebarhouch 8:d19b30a6cd69 142 for(int i = 0; i < mapSize; i++) {
joebarhouch 7:530ca713d2b2 143 coords[i] = maps[i].get_pos();
joebarhouch 7:530ca713d2b2 144 }
joebarhouch 7:530ca713d2b2 145
joebarhouch 6:00d20886e4f8 146
joebarhouch 8:d19b30a6cd69 147 if(_c == false) {
joebarhouch 7:530ca713d2b2 148 _fall = true;
joebarhouch 7:530ca713d2b2 149 for(int j=0; j < mapSize; j++) {
joebarhouch 8:d19b30a6cd69 150 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 151 a = j;
joebarhouch 8:d19b30a6cd69 152 _c = true;
joebarhouch 7:530ca713d2b2 153 //printf("%i, %i\n", player.x, player.y );
joebarhouch 7:530ca713d2b2 154 }
joebarhouch 7:530ca713d2b2 155 }
joebarhouch 6:00d20886e4f8 156 }
joebarhouch 8:d19b30a6cd69 157
joebarhouch 8:d19b30a6cd69 158 if(_c == true) {
joebarhouch 8:d19b30a6cd69 159 _Ypos = coords[a].y - 8;
joebarhouch 8:d19b30a6cd69 160 _fall = false;
joebarhouch 8:d19b30a6cd69 161 _c = false;
joebarhouch 8:d19b30a6cd69 162 }
joebarhouch 8:d19b30a6cd69 163 }
joebarhouch 8:d19b30a6cd69 164
joebarhouch 8:d19b30a6cd69 165
joebarhouch 3:e4e1cbf750b6 166
joebarhouch 11:b3024ab59fa5 167 ////////////////////// ENNEMY COLLIDE ///////////////////////////////////
joebarhouch 11:b3024ab59fa5 168 bool Engine::enemyCollide()
joebarhouch 11:b3024ab59fa5 169 {
joebarhouch 11:b3024ab59fa5 170 vector <Vector2D> epos;
joebarhouch 12:eb8d30593e95 171
joebarhouch 11:b3024ab59fa5 172
joebarhouch 11:b3024ab59fa5 173 for (int i = 0; i < enemies.size(); i++) {
joebarhouch 11:b3024ab59fa5 174 epos.push_back(enemies.at(i).get_pos());
joebarhouch 11:b3024ab59fa5 175 //printf("coord %i, at %f, %f\n", i, epos.at(i).x, epos.at(i).y);
joebarhouch 11:b3024ab59fa5 176 }
joebarhouch 11:b3024ab59fa5 177 for (int i=0; i<epos.size(); i++) {
joebarhouch 11:b3024ab59fa5 178 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 179 ko1 = 1;
joebarhouch 11:b3024ab59fa5 180 //printf("KO\n");
joebarhouch 11:b3024ab59fa5 181 }
joebarhouch 11:b3024ab59fa5 182 }
joebarhouch 12:eb8d30593e95 183 return ko1;
joebarhouch 12:eb8d30593e95 184 }
joebarhouch 12:eb8d30593e95 185
joebarhouch 12:eb8d30593e95 186 ///////////////////// FELL DOWN /////////////////////////////////////////////
joebarhouch 12:eb8d30593e95 187 bool Engine::fellDown()
joebarhouch 12:eb8d30593e95 188 {
joebarhouch 12:eb8d30593e95 189 if(player.y > HEIGHT - 2) {
joebarhouch 12:eb8d30593e95 190 ko2 = 1;
joebarhouch 12:eb8d30593e95 191 } else {
joebarhouch 12:eb8d30593e95 192 ko2 = 0;
joebarhouch 12:eb8d30593e95 193 }
joebarhouch 12:eb8d30593e95 194 return ko2;
joebarhouch 11:b3024ab59fa5 195 }
joebarhouch 11:b3024ab59fa5 196
joebarhouch 12:eb8d30593e95 197
joebarhouch 12:eb8d30593e95 198 /////////////////////// KO ///////////////////////////////////////////////////
joebarhouch 12:eb8d30593e95 199 bool Engine::koed()
joebarhouch 12:eb8d30593e95 200 {
joebarhouch 12:eb8d30593e95 201
joebarhouch 12:eb8d30593e95 202 if( ko1 == 1 || ko2 == 1) {
joebarhouch 12:eb8d30593e95 203 return true;
joebarhouch 12:eb8d30593e95 204 } else {
joebarhouch 12:eb8d30593e95 205 return false;
joebarhouch 12:eb8d30593e95 206 }
joebarhouch 12:eb8d30593e95 207 }
joebarhouch 12:eb8d30593e95 208
joebarhouch 12:eb8d30593e95 209
joebarhouch 11:b3024ab59fa5 210 ////////////////////// GAME OVER ///////////////////////////////////////////
joebarhouch 12:eb8d30593e95 211 void Engine::gameOver(N5110 &lcd)
joebarhouch 12:eb8d30593e95 212 {
joebarhouch 12:eb8d30593e95 213 lcd.inverseMode();
joebarhouch 12:eb8d30593e95 214 wait(0.5);
joebarhouch 12:eb8d30593e95 215 lcd.normalMode();
joebarhouch 12:eb8d30593e95 216 wait(0.5);
joebarhouch 12:eb8d30593e95 217 lcd.inverseMode();
joebarhouch 12:eb8d30593e95 218 wait(0.5);
joebarhouch 12:eb8d30593e95 219 _p.update(_d, 0, _Ypos, true, false);
joebarhouch 12:eb8d30593e95 220 _p.draw(lcd);
joebarhouch 12:eb8d30593e95 221 //printf("KO RUN\n");
joebarhouch 12:eb8d30593e95 222 }
joebarhouch 5:928c2eee4109 223
joebarhouch 8:d19b30a6cd69 224