ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Engine/Engine.cpp
- Committer:
- joebarhouch
- Date:
- 2020-05-27
- Revision:
- 11:b3024ab59fa5
- Parent:
- 10:9317a62bd4d0
- Child:
- 12:eb8d30593e95
File content as of revision 11:b3024ab59fa5:
#include "Engine.h" 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)}; int mapSize = sizeof(maps)/sizeof(*maps); ////////////////////// DRAW MAP ////////////////////////// void drawMap(N5110 &lcd) { for (int i = 0; i < mapSize; i++) { maps[i].draw(lcd); //debugs //coords = maps[i].get_pos(); //printf("x: %i, y: %i,w: %i,h: %i \n",coords.x, coords.y, coords.width, coords.height); } //debugs //printf("-----------------------------------------\n"); } Engine::Engine() { } Engine::~Engine() { } ////////////////////// INIT //////////////////////////// void Engine::init() { //init coord _px = WIDTH / 2; _py = 5; //init call _p.init(_px, _py); //ennemy enemies.push_back(Enemy(0,0, 10)); enemies.push_back(Enemy(0,75, 20)); enemies.push_back(Enemy(1,20, 0)); enemies.push_back(Enemy(1,60, 30)); ko = 0; //physics parameters _Ypos = 0; _fall = true; _c = false; //rands srand(time(NULL)); } ////////////////////// INPUT ////////////////////////// //reads direction and magnitude from the JOYSTICK to control the player void Engine::read_input(Gamepad &pad) { _d = pad.get_direction(); _mag = pad.get_mag(); _jump = pad.B_held(); //printf("%s", _jump ? "true\n" : "false\n"); } ////////////////////// DRAW /////////////////////////// //draw both player and map void Engine::draw(N5110 &lcd) { // player _p.draw(lcd); // map drawMap(lcd); //enemies for(int i = 0; i < enemies.size(); i ++) { enemies.at(i).draw(lcd); } } ////////////////////// UPDATE ////////////////////////// //provide the player file with necessary Joystick values //updates enemy file void Engine::update(Gamepad &pad) { floorCollide(); enemyCollide(); //spawnEnemy(); _p.update(_d,_mag, _Ypos, _fall, _jump); for(int i = 0; i < enemies.size(); i ++) { enemies.at(i).update(2); } } /* if (_c == true) { //debug //printf("collison\n"); } else { //debug //printf("no collison\n"); } //enemmyCollide(pad); } */ ////////////////////// FLOOR COLLISION ////////////////////////// void Engine::floorCollide() { int a; Vector4 coords[mapSize]; Vector2D player = _p.get_pos(); //coordinates of platforms for(int i = 0; i < mapSize; i++) { coords[i] = maps[i].get_pos(); } if(_c == false) { _fall = true; for(int j=0; j < mapSize; j++) { 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) { a = j; _c = true; //printf("%i, %i\n", player.x, player.y ); } } } if(_c == true) { _Ypos = coords[a].y - 8; _fall = false; _c = false; } } ////////////////////// SPAWN ENEMY ///////////////////////////////////// void Engine::spawnEnemy() { } ////////////////////// ENNEMY COLLIDE /////////////////////////////////// bool Engine::enemyCollide() { vector <Vector2D> epos; Vector2D player = _p.get_pos(); for (int i = 0; i < enemies.size(); i++) { epos.push_back(enemies.at(i).get_pos()); //printf("coord %i, at %f, %f\n", i, epos.at(i).x, epos.at(i).y); } for (int i=0; i<epos.size(); i++) { 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) { ko = 1; //printf("KO\n"); } } return ko; } ////////////////////// GAME OVER /////////////////////////////////////////// void Engine::gameOver(N5110 &lcd){ lcd.inverseMode(); wait(0.5); lcd.normalMode(); wait(0.5); lcd.inverseMode(); wait(0.5); _p.update(_d, 0, _Ypos, true, _jump); _p.draw(lcd); printf("KO RUN\n"); }