Player Library

Player.cpp

Committer:
ll14c4p
Date:
2017-04-24
Revision:
3:60a09496c839
Parent:
2:108a1fc56c4c
Child:
4:6d5ea29f83b0

File content as of revision 3:60a09496c839:

#include "Player.h"

Player::Player()
{

}

Player::~Player()
{

}

int sprite[3][3] = {
    {1,0,1},
    {1,1,1},
    {1,1,1},
    };
    
void Player::init(int x, int y)
{
    _x = WIDTH/2 - 1;  // Middle of screen
    _y = HEIGHT/2;  // Near bottom of screen
    _speed = 0.3;  // default speed 
    
}


void Player::draw(N5110 &lcd)
{
    lcd.drawSprite(_x,_y,3,3,(int *)sprite);
}





void Player::update(Direction d,float mag)
{
    _speed = int(mag*10.0f);  // scale is arbitrary, could be changed in future


    // update y value depending on direction of movement
    // North is decrement as origin is at the top-left so decreasing moves up
    if (d == N) {
        _y-=_speed;
    }
    if (d == S) {
        _y+=_speed;
    }
    if (d == E) {
        _x+=_speed;
    }
    if (d == W) {
        _x-=_speed;
    }
    if (d == NE) {
        _y-=_speed/2;
        _x+=_speed/2;
    }
    if (d == NW) {
        _y-=_speed/2;
        _x-=_speed/2;
    }
    if (d == SE) {
        _y+=_speed/2;
        _x+=_speed/2;
    }
    if (d == SW) {
        _y+=_speed/2;
        _x-=_speed/2;
    }


    // check the y origin to ensure that the paddle doesn't go off screen
    if (_y <= 0) {
        _y = 0;
    }
    if (_x <= 0) {
        _x = 0;
    }
    if (_x > 81) {
        _x = 81;
    }
    if (_y > 45) {
        _y = 45;
    }

}