ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Ship/Ship.cpp

Committer:
ikenna1
Date:
2019-04-18
Revision:
26:a53d41adf40b
Parent:
14:88ca5b1a111a
Child:
31:c7bd3ed16840

File content as of revision 26:a53d41adf40b:

#include "Ship.h"
Ship::Ship()
{

}

Ship::~Ship()
{

}
// data needed for first ship: kestrel
const int kestrel[6][9] =   {
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,1,1,0,1,1,0,0 },
    { 0,0,1,1,0,1,1,0,0 },
    { 1,0,1,1,0,1,1,0,1 },
    { 0,1,1,1,1,1,1,1,0 },
};
const int imperion[10][7] = {
    {0,1,0,0,0,1,0},
    {0,1,0,0,0,1,0},
    {1,1,0,0,0,1,1},
    {0,1,0,0,0,1,0},
    {0,1,0,0,0,1,0},
    {0,1,0,0,0,1,0},
    {0,1,1,0,1,1,0},
    {1,0,0,1,0,0,1},
    {0,0,1,0,1,0,0},
    {0,0,0,1,0,0,0},
};
const int thor[10][7] = {
    {0,0,0,1,0,0,0},
    {0,0,1,0,1,0,0},
    {0,1,0,0,0,1,0},
    {1,0,0,1,0,0,1},
    {0,1,0,0,0,1,0},
    {0,0,1,0,1,0,0},
    {1,0,0,1,0,0,1},
    {1,0,1,0,1,0,1},
    {1,1,0,0,0,1,1},
    {1,0,0,0,0,0,1},

};
/*
int kestrel_width = 9;
int kestrel_height = 6;
int kestrel_speed = 5;
*/

// Set kestrel ship to be default
void Ship::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos)
{
    _ship_width = ship_width;
    _ship_height = ship_height;
    _ship_speed = ship_speed;
    _ship_xpos = ship_xpos;
    _ship_xpos = ship_ypos;
//    _ship_shape[_ship_height][_ship_width] = ship_shape;
}
// Draw the ship ***Note: figure out how to change ship type e.g from kestrel to devotion
void Ship::draw_ship(N5110 &lcd, int shipno)
{
    if(shipno == 0) {
        lcd.drawSprite(_ship_xpos,_ship_ypos,6,9,(int *)kestrel);
    }
    if(shipno == 1) {
        lcd.drawSprite(_ship_xpos,_ship_ypos,10,7,(int *)imperion);
    }
    if(shipno == 2) {
        lcd.drawSprite(_ship_xpos,_ship_ypos,10,7,(int *)thor);
    }
}

void Ship::update_ship(float x_joystick,float y_joystick)
{
    // Only change position if joystick is reasonably moved
    if(-0.25 > x_joystick || x_joystick > 0.25 || -0.25 > y_joystick || y_joystick > 0.25) {
        // Update positions using joystick and the intended ships speed
        _ship_xpos = _ship_xpos + (x_joystick*_ship_speed);
        _ship_ypos = _ship_ypos - (y_joystick*_ship_speed);

        // Dont let sprite move out of screen
        if(_ship_xpos < 0) {
            _ship_xpos = 0;
        }
        if(_ship_xpos > (84 - (_ship_width))) {
            _ship_xpos = 84 - _ship_width;
        }
        if(_ship_ypos < 0) {
            _ship_ypos = 0;
        }
        if(_ship_ypos > (48 - (_ship_height))) {
            _ship_ypos = 48 - _ship_height;
        }
    }
    // printf("y_joysticzk = %f\n",y_joystick);
    // printf("x_joystick = %f\n",x_joystick);
}
// returns the current ship position on screen as a vector
Vector2D Ship::get_pos()
{
    Vector2D ship_pos = {_ship_xpos,_ship_ypos};
    return ship_pos;
}

void Ship::set_dimensions(int ship_width, int ship_height)
{
    _ship_width = ship_width;
    _ship_height = ship_height;
}

/****Note: if you make all the ships the same size, you could then creare a enum for them ship.[ enum class Ship {kestrel, devotion}; ]
then, you can declare a new variable belonging to the enum Ship, ship. then to set a ships sprite simply use the variable to test if
the ship is correct and create an if function to draw the appropriate ship
*/