ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

RosenEngine/RosenEngine.cpp

Committer:
ikenna1
Date:
2019-04-13
Revision:
19:3316dba9787e
Parent:
18:2cc6898de6b2
Child:
20:5b4b3bf5795c

File content as of revision 19:3316dba9787e:

#include "RosenEngine.h"

DigitalIn A_button(PTB9);
// Constructor
RosenEngine::RosenEngine()
{

}
// Destructor
RosenEngine::~RosenEngine()
{

}


void RosenEngine::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos)
{
    // initialise the game parameters
    _ship.init(ship_width,ship_height,ship_speed,ship_xpos,ship_ypos);
    // place seeker above the ship
    _enemy.init(ship_xpos, 0);
    _menu.init(16);
    _health.init(_xcursor);

}

void RosenEngine::read_input(Gamepad &pad)
{
    Vector2D mapped_coord = pad.get_coord();
    _xjoystick = mapped_coord.x;
    _yjoystick = mapped_coord.y;
    _d = pad.get_direction();
    // printf("_xjoystick ,_yjoystick = %f , %f\n",_xjoystick, _yjoystick);
}

void RosenEngine::draw(N5110 &lcd, Gamepad &pad)
{
    _health.draw_health(lcd);
    _health.draw_shields(lcd);
    _enemy.draw_seeker(lcd);
    if(_xcursor == 0) {
        _ship.set_dimensions(9,6);
        _ship.draw_ship(lcd);
        _weapons.draw(lcd);
    }
    if(_xcursor == 1) {
        _ship.set_dimensions(7,10);
        _ship.draw_imperion(lcd);

        if((A_button) == true) {
            pad.tone(500,0.5);
            wait(0.5);
            pad.tone(1000,0.3);
            wait(0.3);
            pad.tone(1500,0.2);
            wait(0.2);
            pad.tone(1700,0.15);
            wait(0.15);
            while((A_button) == true) {
                _weapons.draw_i(lcd);
                pad.tone(2000,0.1);
                wait(0.1);
            }
        }
    }
}

void RosenEngine::update(Gamepad &pad)
{
    _enemy.update_seeker(ship_xpos, ship_ypos);
    if(_xcursor == 0) {
        _ship.update_ship(_xjoystick,_yjoystick);
        _weapons.update();
    }
    if(_xcursor == 1 && A_button == false) {
        _ship.update_ship(_xjoystick,_yjoystick);
        _weapons.update();
    }
    // _menu.update(_d);
    check_enemy_ship_collision();
}
void RosenEngine::get_pos()
{
    Vector2D ship_pos = _ship.get_pos();
    ship_xpos = ship_pos.x;
    ship_ypos = ship_pos.y;
    ship_width = 9;
    _weapons.init(ship_xpos, ship_ypos, ship_width);
    _ycursor = _menu.get_ycursor();
}
void RosenEngine::title(N5110 &lcd)
{
    _menu.title(lcd);
    _menu.update(_d);
}
int RosenEngine::get_ycursor()
{
    _ycursor = _menu.get_ycursor();
    return _ycursor;
}
int RosenEngine::get_xcursor()
{
    _xcursor = _menu.get_xcursor();
    return _xcursor;
}
void RosenEngine::ship_select(N5110 &lcd)
{
    _menu.update(_d);
    _menu.disp_ships(lcd);
}
void RosenEngine::check_enemy_ship_collision()
{
    Vector2D ship_pos = _ship.get_pos();
    Vector2D seeker_pos = _enemy.get_seekerpos();
    int ship_x[9];
    int ship_y[6];
    int seeker_x[9];
    int seeker_y[6];
    bool xcol = false;
    bool ycol = false;

    // create an array of all x positions for the ship sprite i.e along its width (ship_x)
    for(int countx = 0; countx>=9; countx=countx+1) {
        ship_x[countx] = ship_pos.x + countx;
        seeker_x[countx] = seeker_pos.x + countx;
    }

    // create an array of all y positions for the ship sprite i.e along its height (ship_y)
    for(int county = 0; county>=6; county=county+1) {
        ship_y[county] = ship_pos.y + county;
        seeker_y[county] = seeker_pos.y + county;
    }
    // check all values of ship position against all values of seekers x position
    for(int nx = 0; nx>=9; nx=nx+1) {
        for(int mx = 0; mx>=9; mx=mx+1) {
            if(ship_x[nx] == seeker_x[mx]) {
                xcol = true;
            }
        }
    }
    for(int ny = 0; ny>=6; ny=ny+1) {
        for(int my = 0; my>=9; my=my+1) {
            if(ship_y[ny] == seeker_y[my]) {
                ycol = true;
            }
        }
    }
    if(xcol == true && ycol == true) {
        _health.update(1);
        _enemy.reset_seeker();
    }
}