ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

People/People.cpp

Committer:
el19zf
Date:
2020-04-29
Revision:
8:8287d2ef965d
Parent:
4:b12a49f0b730

File content as of revision 8:8287d2ef965d:

#include "People.h"
#define INIT_x 40
#define INIT_y 43


const int people_sprite[4][4] = {
        {0,1,1,0},
        {1,1,1,1},
        {0,1,1,0},
        {0,1,1,0},
};//try to draw a people, but for the operability of game, seems not good..

People::People() {
    
}

People::~People() {
    
}
    
void People::init() {
    
    _x = INIT_x;
    _y = INIT_y;//Set initial postion of people
}
void People::draw(N5110 &lcd) {
    
    //Draw sprite to represent a people
    lcd.drawSprite(_x,_y,4,4,(int*)people_sprite);
    // printf("drawSprite");
}

void People::set_pos(Vector2D p)
{
    _x = p.x;
    _y = p.y;
}

void People::set_velocity(Direction d,float mag)
{
    _d = d;
    _mag = mag;
}

Vector2D People::get_pos()
{
    Vector2D p = {_x,_y};
    //printf("Coord = %f,%f\n",p.x,p.y);
    return p;
}

void People::update()
{
    if (_d == S) {
        if(_mag < 0.25f)        {   _x += 0;    _y += 1;} else
        if(0.25f < _mag < 0.5f) {   _x += 0;    _y += 2;} else
        if(0.5f < _mag < 0.75f) {   _x += 0;    _y += 3;} else 
                                {   _x += 0;    _y += 4;}}
    if (_d == SE) {
        if(_mag < 0.25f)        {   _x += 1;    _y += 1;} else
        if(0.25f < _mag < 0.5f) {   _x += 2;    _y += 2;} else
        if(0.5f < _mag < 0.75f) {   _x += 3;    _y += 3;} else 
                                {   _x += 4;    _y += 4;}}
    if (_d == E) {              
        if(_mag < 0.25f)        {   _x += 1;    _y += 0;} else
        if(0.25f < _mag < 0.5f) {   _x += 2;    _y += 0;} else
        if(0.5f < _mag < 0.75f) {   _x += 3;    _y += 0;} else 
                                {   _x += 4;    _y += 0;}}  
    if (_d == NE) {
        if(_mag < 0.25f)        {   _x += 1;    _y -= 1;} else
        if(0.25f < _mag < 0.5f) {   _x += 2;    _y -= 2;} else
        if(0.5f < _mag < 0.75f) {   _x += 3;    _y -= 3;} else 
                                {   _x += 4;    _y -= 4;}}
    if (_d == N) {
        if(_mag < 0.25f)        {   _x += 0;    _y -= 1;} else
        if(0.25f < _mag < 0.5f) {   _x += 0;    _y -= 2;} else
        if(0.5f < _mag < 0.75f) {   _x += 0;    _y -= 3;} else 
                                {   _x += 0;    _y -= 4;}}
    if (_d == NW) {
        if(_mag < 0.25f)        {   _x -= 1;    _y -= 1;} else
        if(0.25f < _mag < 0.5f) {   _x -= 2;    _y -= 2;} else
        if(0.5f < _mag < 0.75f) {   _x -= 3;    _y -= 3;} else 
                                {   _x -= 4;    _y -= 4;}}
    if (_d == W) {
        if(_mag < 0.25f)        {   _x -= 1;    _y -= 0;} else
        if(0.25f < _mag < 0.5f) {   _x -= 2;    _y -= 0;} else
        if(0.5f < _mag < 0.75f) {   _x -= 3;    _y -= 0;} else 
                                {   _x -= 4;    _y -= 0;}}
    if (_d == NW) {
        if(_mag < 0.25f)        {   _x -= 1;    _y += 1;} else
        if(0.25f < _mag < 0.5f) {   _x -= 2;    _y += 2;} else
        if(0.5f < _mag < 0.75f) {   _x -= 3;    _y += 3;} else 
                                {   _x -= 4;    _y += 4;}}
    //without going off screen
    if (_x < 1) { _x = 1;} else
    if (_x > 79) { _x = 79;} else
    if (_y < 1) {_y = 1;} else
    if (_y > 43) {_y = 43;} 
}