test 1 doc

Dependencies:   mbed Gamepad2

Engine/Engine.cpp

Committer:
joebarhouch
Date:
2020-05-27
Revision:
13:cb5ed2f0cbd5
Parent:
12:eb8d30593e95
Child:
14:58887d7e1072

File content as of revision 13:cb5ed2f0cbd5:

#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));
    ko1 = 0;
    ko2 = 0;

    //physics parameters
    _Ypos = 0;
    _fall = true;
    _c = false;
    //rands
    srand(time(NULL));

    //coins
    coin.init();
}



////////////////////// 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);
    }

    //coin
    coin.draw(lcd);

}




////////////////////// UPDATE //////////////////////////
//provide the player file with necessary Joystick values
//updates enemy file
void Engine::update(Gamepad &pad)
{
    //fellDown();
    coinTaken();
    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];
    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;
    }
}


////////////////////// GOT COIN /////////////////////////////////////////
void Engine::coinTaken(){
        coinPos = coin.get_pos();
        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){
                coin.set_pos(10, 10);
            }
    }



////////////////////// ENNEMY COLLIDE ///////////////////////////////////
bool Engine::enemyCollide()
{
    vector <Vector2D> epos;


    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) {
            ko1 = 1;
            //printf("KO\n");
        }
    }
    return ko1;
}

///////////////////// FELL DOWN /////////////////////////////////////////////
bool Engine::fellDown()
{
    if(player.y > HEIGHT - 2) {
        ko2 = 1;
    }  else {
        ko2 = 0;
    }
    return ko2;
}


/////////////////////// KO ///////////////////////////////////////////////////
bool Engine::koed()
{
    
    if( ko1 == 1 || ko2 == 1) {
        return true;
    } else {
        return false;
    }
}


////////////////////// 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, false);
    _p.draw(lcd);
    //printf("KO RUN\n");
}