ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jb

Dependencies:   mbed Gamepad2

Player/Player.cpp

Committer:
joebarhouch
Date:
2020-05-22
Revision:
5:928c2eee4109
Parent:
4:cf5088ace087
Child:
7:530ca713d2b2

File content as of revision 5:928c2eee4109:

#include "Player.h"
#include "Sprites.h"

Player::Player()
{

}

Player::~Player()
{

}

//initialize
void Player::init(int x,int y)
{
    _playerX = x;
    _playerY = y;
}



//Player jumps if A is pressed
bool Player::jump(Gamepad &pad)
{
    
}



//Draw the sprites depending on the orientation
void Player::draw(N5110 &lcd)
{
    if(_dir == E) {
        Bitmap bit_player(s_player, 8, 9);
        bit_player.render(lcd, _playerX, _playerY);
    }

    if(_dir == W) {
        Bitmap bit_player_inv(s_player_inv, 8, 9);
        bit_player_inv.render(lcd, _playerX, _playerY);
    } else {
        Bitmap bit_player(s_player, 8, 9);
        bit_player.render(lcd, _playerX, _playerY);
    }
}

//Update player movement and set screen restrictions
void Player::update(Direction d, float mag)
{
    int _vx = int(mag*4.0f);
    if(d == E) {
        _playerX = _playerX + _vx;
        _dir = E;
    }

    if(d == W) {
        _playerX = _playerX - _vx;
        _dir = W;
    }
    if (_playerX > WIDTH - 9) {
        _playerX = WIDTH - 9;
    }
    if(_playerX < 0) {
        _playerX = 0;
    }
    
    //debug
    //printf("x: %i, y: %i \n", _playerX, _playerY);
}

//get the position of the player in a 2D vector
Vector2D Player::get_pos()
{
    Vector2D pos = {_playerX, _playerY};
    return pos;
}