Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Player/Player.cpp

Committer:
joshdavy
Date:
2019-04-24
Revision:
9:96969b1c6bde
Parent:
8:21b6d4dbce44
Child:
10:58cf89dd878c

File content as of revision 9:96969b1c6bde:

#include "Player.h"



Player::Player()
{

}

Player::~Player()
{

}

void Player::init(int height,int width,Vector2D pos)
{
    _height = height;
    _width = width;
    _pos = pos;
    _initial_pos = pos;
    _bitmap = (int *) player_bitmap_left; // Default
    _orientation = 1;
    _direction = -1;
}

void  Player::check_out_of_range()
{
    if (_pos.x < -5  || _pos.x > 89 || _pos.y < -5 || _pos.y > 53) {
        _pos = _initial_pos;
        _orientation = 1;
    }
}

bool Player::check_goal_reached(Vector2D goal) 
{
    bool goal_reached = false;
    int x_distance = abs(goal.x - _pos.x);
    int y_distance = abs(goal.y - _pos.y);
    
    if (x_distance < 2 && y_distance < 6) {
        goal_reached = true;
    }
    return goal_reached;
}

bool Player::can_move_down(Block blocks [],int number_of_blocks)
{
    bool can_move_down = true;

    for (int i  = 0; i < number_of_blocks; i++) {
        if (_pos.x + _width > blocks[i].first.x &&
                _pos.x  < blocks[i].second.x) {

            if ( (_pos.y + _height) == blocks[i].first.y ) {
                can_move_down = false;
            }
        }
    }
    return can_move_down;
}

bool Player::can_move_up(Block blocks [],int number_of_blocks)
{
    bool can_move_up = true;
    for (int i  = 0; i < number_of_blocks; i++) {
        if (_pos.x + _width > blocks[i].first.x &&
                _pos.x  < blocks[i].second.x) {

            if ( (_pos.y) == blocks[i].second.y ) {
                can_move_up = false;
            }
        }
    }
    return can_move_up;
}

bool Player::can_move_left(Block blocks [],int number_of_blocks)
{
    bool can_move_left = true;
    for (int i  = 0; i < number_of_blocks; i++) {
        if (_pos.y  + _height > blocks[i].first.y &&
                _pos.y < blocks[i].second.y) {

            if ( (_pos.x) == blocks[i].second.x ) {
                can_move_left = false;
            }
        }
    }
    return can_move_left;
}

bool Player::can_move_right(Block blocks [],int number_of_blocks)
{
    bool can_move_right = true;

    for (int i  = 0; i < number_of_blocks; i++) {
        if (_pos.y + _height > blocks[i].first.y &&
                _pos.y  < blocks[i].second.y) {

            if ( (_pos.x + _width) == blocks[i].first.x ) {
                can_move_right = false;
            }
        }
    }
    return can_move_right;
}

void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
{
    if (_direction == -1 && _orientation == 1) {
        _bitmap = (int *) player_bitmap_left;
    }
    if (_direction == 1 && _orientation == 1) {
        _bitmap = (int *) player_bitmap_right;
    }
    if (_direction == -1 && _orientation == -1) {
        _bitmap = (int *) player_bitmap_left_flipped;
    }
    if (_direction == 1 && _orientation == -1) {
        _bitmap = (int *) player_bitmap_right_flipped;
    }
    
    check_out_of_range();


    if (pad.check_event(Gamepad::A_PRESSED) &&
            (!can_move_down(blocks,number_of_blocks) || !can_move_up(blocks,number_of_blocks))
       ) {

        if (_orientation == 1) {
            _orientation = -1;
        } else {
            _orientation = 1;
        }
    }

    if (_orientation == 1 && can_move_down( blocks,
                                            number_of_blocks)) {
        _pos.y += GRAVITY;
    }
    if (_orientation == -1 && can_move_up(blocks,
                                          number_of_blocks)) {
        _pos.y -= GRAVITY;
    }


    if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) {
        _pos.x -= 2;
        _direction = -1;
    }
    if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) {
        _pos.x += 2;
        _direction = 1;
    }




}