ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Diff: Engine/Engine.cpp
- Revision:
- 8:d19b30a6cd69
- Parent:
- 7:530ca713d2b2
- Child:
- 9:9830d3a78572
diff -r 530ca713d2b2 -r d19b30a6cd69 Engine/Engine.cpp --- a/Engine/Engine.cpp Tue May 26 01:45:20 2020 +0000 +++ b/Engine/Engine.cpp Tue May 26 22:15:42 2020 +0000 @@ -1,7 +1,8 @@ #include "Engine.h" -Platform maps[5] = {Platform(0, 15, 20, 3), Platform(64, 15, 20, 3), Platform(0, 40, 20, 3), Platform(64, 40, 20, 3), Platform(20, 30, 30, 3)}; +Platform maps[6] = {Platform(0, 40, 20, 3), Platform(0, 14, 20, 3), Platform(64, 14, 20, 3), Platform(64, 40, 20, 3), Platform(5, 30, 30, 3), Platform(50, 30, 30, 3)}; int mapSize = sizeof(maps)/sizeof(*maps); +vector <Enemy> enemies; ////////////////////// DRAW MAP ////////////////////////// @@ -35,14 +36,19 @@ _px = WIDTH / 2; _py = 5; - //init call _p.init(_px, _py); + //ennemy + enemies.push_back(Enemy(0, 0)); + enemies.push_back(Enemy(75, 0)); + + //physics parameters _Ypos = 0; _fall = true; - _collide = false; + _c = false; + } @@ -53,7 +59,8 @@ { _d = pad.get_direction(); _mag = pad.get_mag(); - _jump = pad.A_held(); + _jump = pad.B_held(); + //printf("%s", _jump ? "true\n" : "false\n"); } @@ -65,11 +72,19 @@ void Engine::draw(N5110 &lcd) { - drawMap(lcd); // player _p.draw(lcd); + // map + drawMap(lcd); + + //enemies + for(int i = 0; i < enemies.size(); i ++) { + enemies.at(i).draw(lcd); + } + + } @@ -79,60 +94,114 @@ //provide the player file with necessary Joystick values void Engine::update(Gamepad &pad) { - floorCollide(); - _p.update(_d,_mag, _Ypos, _fall, _jump); -} + efloorCollide(); + spawnEnemy(); - -/* + _p.update(_d,_mag, _Ypos, _fall, _jump); - - if (_collision == true) { + for(int i = 0; i < enemies.size(); i ++) { + enemies.at(i).update(_eYpos, _efall); + //printf("Enemy %i!\n", i); + } - //debug - //printf("collison\n"); - } else { + /* + if (_c == true) { + + //debug + //printf("collison\n"); + } else { - //debug - //printf("no collison\n"); + //debug + //printf("no collison\n"); + } + //enemmyCollide(pad); } - //enemmyCollide(pad); + */ } -*/ - ////////////////////// FLOOR COLLISION ////////////////////////// void Engine::floorCollide() { - //player.x + 8 >= 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 - _collide = false; + int a; + Vector4 coords[mapSize]; Vector2D player = _p.get_pos(); - Vector4 coords[mapSize]; - - for(int i = 0; i<mapSize; i++) { + //coordinates of platforms + for(int i = 0; i < mapSize; i++) { coords[i] = maps[i].get_pos(); } - if(_collide == false) { + if(_c == false) { _fall = true; for(int j=0; j < mapSize; j++) { - if(player.x + 8 >= 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) { + 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; - _collide = true; + _c = true; //printf("%i, %i\n", player.x, player.y ); } } } - - if(_collide == true){ - _Ypos = coords[a].y - 8; - _fall = false; - _collide = false; + + if(_c == true) { + _Ypos = coords[a].y - 8; + _fall = false; + _c = false; + } +} + + +////////////////////// SPAWN ENEMY ///////////////////////////////////// +void Engine::spawnEnemy() +{ + for(int i = 0; i < enemies.size(); i ++) { + //printf("%s", enemies.at(i).fell() ? "true\n" : "false\n"); + if ( enemies.at(i).fell() == true ) { + enemies.erase(enemies.begin() + i); + enemies.push_back(Enemy(1,1)); } + } } +////////////////////// ENNEMY FLOOR COLLISION ////////////////////////// +void Engine::efloorCollide() +{ + vector <Vector2D> epos; + Vector4 coord[mapSize]; + int b; + //coordinates of platforms + for(int i = 0; i < mapSize; i++) { + coord[i] = maps[i].get_pos(); + } + + for(int i = 0; i <enemies.size(); i++) { + epos.push_back(enemies.at(i).get_pos()); + //printf("pos of %i: %f, %f\n", i, epos.at(i).x, epos.at(i).y); + } + + + if(_e == false) { + _efall = true; + for(int i=0; i<mapSize; i++) { + for(int j=0; j < epos.size(); j++) { + 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) { + b = j; + _e = true; + //printf("%f, %f\n", epos.at(j).x,epos.at(j).y ); + } + } + } + } + + if(_e == true) { + _eYpos = coord[b].y - 8; + _efall = false; + _e = false; + //printf("enemy fall"); + } + +} +