Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Player/Player.cpp

Committer:
joshdavy
Date:
2019-04-19
Revision:
8:21b6d4dbce44
Parent:
7:68e06dda79f7
Child:
9:96969b1c6bde

File content as of revision 8:21b6d4dbce44:

#include "Player.h"



Player::Player()
{

}

Player::~Player()
{

}

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


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


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




}