Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Player/Player.cpp
- Committer:
- joshdavy
- Date:
- 2019-05-06
- Revision:
- 10:58cf89dd878c
- Parent:
- 9:96969b1c6bde
- Child:
- 11:db27d3838514
File content as of revision 10:58cf89dd878c:
#include "Player.h"
Player::Player()
{
}
Player::~Player()
{
}
void Player::init(Vector2D pos)
{
    _height = PLAYER_HEIGHT;
    _width = PLAYER_WIDTH;
    _pos = pos;       
    _initial_pos = pos; 
    _bitmap = (int *) player_bitmap_left; // Default
    _orientation = 1; // Upright
    _direction = -1;  // Left
}
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_sprite(int orientation, int direction)
{
    // If poiting left and  upright
    if (direction == -1 && orientation == 1) {
        _bitmap = (int *) player_bitmap_left;
    }
    // If poiting right and upright
    if (direction == 1 && orientation == 1) {
        _bitmap = (int *) player_bitmap_right;
    }
    // If poiting left and upside down
    if (direction == -1 && orientation == -1) {
        _bitmap = (int *) player_bitmap_left_flipped;
    }
    // If poiting right and upside down
    if (direction == 1 && orientation == -1) {
        _bitmap = (int *) player_bitmap_right_flipped;
    }
}
void Player::process_inputs(Gamepad &pad,Block blocks [], int number_of_blocks)
{
    // If A pressed and touching a block vertically
    if (pad.check_event(Gamepad::A_PRESSED) &&
            (!can_move_down(blocks,number_of_blocks) ||
             !can_move_up(blocks,number_of_blocks))
       ) {
        // then flip the orientation
        if (_orientation == 1) {
            _orientation = -1;
        } else {
            _orientation = 1;
        }
    }
    // If  joystick is held left and not touching a block on the left
    if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) {
        // then move left and set the direction facing to -1 (left)
        _pos.x -= SPEED;
        _direction = -1;
    }
    // If joystick is held right and not touching a block on the right
    if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) {
        // then move right and set the direction facing to 1 (right)
        _pos.x += SPEED;
        _direction = 1;
    }
}
void Player::gravity(Block blocks [], int number_of_blocks)
{
    // If upright and can move down gravity increases the position
    if (_orientation == 1 && can_move_down( blocks,
                                            number_of_blocks)) {
        _pos.y += GRAVITY;
    }
    // If upside down and can move up gravity decreases the position
    if (_orientation == -1 && can_move_up(blocks,
                                          number_of_blocks)) {
        _pos.y -= GRAVITY;
    }
}
void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks)
{
    process_inputs(pad,blocks,number_of_blocks);
    gravity(blocks,number_of_blocks);
    check_out_of_range();
    update_sprite(_orientation,_direction);
    
    
}