ELEC2645 (2016/17) / Mbed 2 deprecated 2645_Project_el15as

Dependencies:   mbed

Player/Player.cpp

Committer:
el15as
Date:
2017-04-30
Revision:
5:158e2951654b
Parent:
4:1f7f32f3e017
Child:
7:b54323241435

File content as of revision 5:158e2951654b:

#include "Player.h"

Player::Player()
{

}

Player::~Player()
{

}

void Player::init(intVector2D XY)
{
    _x = XY.x;
    _y = XY.y;

    _isDead = false;

    Vector2D velocity = {0.0f, 0.0f};
    _velocity = velocity;

    intVector2D points[8] = {
        {0,0}, {2,0}, // top
        {0,2}, {2,2}, // bottom
        {0,0}, {0,2}, // left
        {2,0}, {2,2}, // right
    };

    for (int i = 0; i < 8; i++) {
		collisionPoint[i] = points[i];
    };

    _isJumping = true;
}

void Player::draw(N5110 &lcd)
{
    // draw the player
    lcd.drawRect(_x, _y, 3, 3, FILL_TRANSPARENT);
}

void Player::update(float jx, Serial &pc, int lvl, bool theSwitch)
{
    if (jx < 0) {
        _velocity.x = int(jx*3.2f); // to make up for mechanical imbalance
    } else {
        _velocity.x = int(jx*3.0f);
    }

    // Jump if not already jumping and the jump key was pressed
    if (_didReleaseJumpKey && !_isJumping && _didPressJump)
    {
        apply_jump(lvl, theSwitch);
    }

    // Jump key released
    if (!_didPressJump) {
        _didReleaseJumpKey = true;
    }
    apply_gravity(lvl, theSwitch);

    // Assume player is in the air to prevent jumping
    // If that's not true it will be set to false in collsion detection
    _isJumping = true;

    //if (_velocity.y > 6) _velocity.y = 6; // Terminal velocity
}

intVector2D Player::get_pos() {
    intVector2D p = {_x, _y};
    return p;
}

void Player::set_pos(intVector2D p) {
    _x = p.x;
    _y = p.y;
}

Vector2D Player::get_velocity() {
    Vector2D velocity = {_velocity.x, _velocity.y};
    return velocity;
}

void Player::set_velocity(Vector2D v) {
    _velocity = v;
}

void Player::reset_velocity()
{
    _velocity.x = 0;
    _velocity.y = 0;
}

void Player::apply_gravity(int lvl, bool theSwitch)
{
    // Opening the door on level 2 inverts gravity
    if ((lvl == 2 || lvl == 3) && theSwitch == true) {
        _velocity.y -= GRAVITY;
    }
    else {
        _velocity.y += GRAVITY;
    }
}

void Player::apply_jump(int lvl, bool theSwitch)
{
    _isJumping = true;
    _didReleaseJumpKey = false;

    // Opening the door on level 2 inverts gravity
    if ((lvl == 2 || lvl == 3) && theSwitch == true) {
        _velocity.y = -JUMPSPEED;
    }
    else {
        _velocity.y = JUMPSPEED;
    }
}