ELEC2645 (2018/19) / Mbed 2 deprecated EL17MCD

Dependencies:   mbed

Tank/Tank.cpp

Committer:
el17mcd
Date:
2019-04-19
Revision:
15:fa5282fcd134
Parent:
13:feadff02d3f7
Child:
16:a2c945279b79

File content as of revision 15:fa5282fcd134:

/* Tank.cpp
Produces tank on the left side of the
lcd and dictates it's movement
1.4.19
*/
#include "Tank.h"

Tank::Tank()
{
    _speed = 1;
}

Tank::~Tank()
{

}
// Accessors
int Tank::get_position_x()
{
    return _position_x;
}

int Tank::get_position_y()
{
    return _position_y;
}

int Tank::get_hitbox(int i)
{
    return _hitbox[i];
}

int Tank::get_health()
{
    return _health;
}
// Mutators
void Tank::set_position(int x, int y)
{
    _position_x = x;
    _position_y = y;
}    

void Tank::set_movement_limits(int left, int right)
{
    _left_lim = left; 
    _right_lim = right;   
}

void Tank::set_health(int h)
{
    _health = h;   
}

void Tank::set_speed(int s)
{
    _speed = s;
}
// Movement
void Tank::_limit_movement()
{
    if (_position_x == _left_lim - 1) {
        _position_x  = _left_lim;
    } else if (_position_x == _right_lim + 1) { 
        _position_x = _right_lim;
    }
}

void Tank::move_position(int d)
{
    int slowness = 9 - _speed;
    int i = _move_counter % slowness;
    if (d > 0) {
        if (i == 0) {_position_x++;}
    _move_counter++;
    }  
    else if (d < 0) {
        if (i == 0) {_position_x--;}
    _move_counter--;
    }
    _limit_movement(); 
}
// Other Methods
void Tank::lose_health()
{
    _health--;
}

void Tank::generate_hitbox()
{
    int i = 0;
    for (int i0 = 0; i0 < 4; i0++) {    
        for (int i1 = 1; i1 < 11; i1++) {
            _hitbox[i] = (i0 + _position_y) * 84 + _position_x + i1;
            i++;
        }
    }
}