Musallam Bseiso / Friendly

Friendly.cpp

Committer:
musallambseiso
Date:
2017-04-03
Revision:
6:378b5d29e9cf
Parent:
5:0cd7f779418a
Child:
7:ac97bd2f5b1a

File content as of revision 6:378b5d29e9cf:

#include "Friendly.h"

//  Class for friendly ship (the one you control)

Friendly::Friendly()
{
}

Friendly::~Friendly()
{
}

void Friendly::init(int x,int height,int width)     // initialization
{
    _x = 2;     // starting x position (fixed)
    _y = HEIGHT/2 - height/2;  // starting y position (centre)
}

void Friendly::draw(N5110 &lcd)     // draws friendly ship in screen buffer
{
    lcd.drawLine(_x,_y,_x,_y+6,1);
    lcd.drawLine(_x+1,_y,_x+3,_y,1);
    lcd.drawLine(_x+1,_y+6,_x+3,_y+6,1);
    lcd.drawLine(_x+4,_y+1,_x+5,_y+1,1);
    lcd.drawLine(_x+4,_y+5,_x+5,_y+5,1);
    lcd.drawLine(_x+6,_y+2,_x+6,_y+4,1);
}

void Friendly::update(Direction d,float mag)
{
    _speed = int(mag*4.0f); 

    // four-directional movement
    if (d == N) {
        _y-=_speed;
    } else if (d == S) {
        _y+=_speed;
    } else if (d == W) {
        _x-=_speed;
    } else if (d == E) {
        _x+=_speed;
    } else if (d == NW) {
        _y-=_speed;
        _x-=_speed;
    } else if (d == NE) {
        _y-=_speed;
        _x+=_speed;
    } else if (d == SW) {
        _y+=_speed;
        _x-=_speed;
    } else if (d == SE) {
        _y+=_speed;
        _x+=_speed;
    }

    // position check so the ship doesn't go out of bounds
    if (_y < 1) {
        _y = 1;
    }
    
    if (_y > 41) {
        _y = 41;
    }
    
    if (_x < 1) {
        _x = 1;
    }
    
    if (_x > 78) {
        _x = 78;
    }
}

Vector2D Friendly::get_pos() {
    Vector2D p = {_x,_y};
    return p;    
}