test 1 doc

Dependencies:   mbed Gamepad2

Engine/Engine.cpp

Committer:
joebarhouch
Date:
2020-05-27
Revision:
10:9317a62bd4d0
Parent:
9:9830d3a78572
Child:
11:b3024ab59fa5

File content as of revision 10:9317a62bd4d0:

#include "Engine.h"

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


    //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();
    //spawnEnemy();
    _p.update(_d,_mag, _Ypos, _fall, _jump);

    for(int i = 0; i < enemies.size(); i ++) {
        enemies.at(i).update(2,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()
{

}