ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

RosenEngine/RosenEngine.cpp

Committer:
ikenna1
Date:
2019-04-18
Revision:
28:6319e928f0aa
Parent:
27:f99249e727fd
Child:
29:4c7b16b5b6df

File content as of revision 28:6319e928f0aa:

#include "RosenEngine.h"
DigitalIn A(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(48,0);
    _menu.init(16);
    _health.init(_shipno);

}
void RosenEngine::reset()
{
    _health.init(_shipno);
}

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,_shipno);
    _health.draw_shields(lcd);
    _enemy.draw_seeker(lcd);
    if(_shipno == 0) {
        _ship.set_dimensions(9,6);
        _ship.draw_ship(lcd,_shipno);
        _weapons.draw(lcd,pad,_shipno);
    }
    if(_shipno == 1) {
        _ship.set_dimensions(7,10);
        _ship.draw_ship(lcd,_shipno);
        _weapons.draw(lcd,pad,_shipno);
    }
    if(_shipno == 2) {
        _ship.set_dimensions(7,10);
        _ship.draw_ship(lcd,_shipno);
        _weapons.draw(lcd,pad,_shipno);
    }
}

void RosenEngine::update(Gamepad &pad)
{
    _enemy.update_seeker(ship_xpos, ship_ypos);
    if(_shipno == 0) {
        _ship.update_ship(_xjoystick,_yjoystick);
        _weapons.update();
    }
    if(_shipno == 1 && A == false) {
        _ship.update_ship(_xjoystick,_yjoystick);
        _weapons.update();
    }
    if(_shipno == 2) {
        _ship.update_ship(_xjoystick,_yjoystick);
        _weapons.update();
    }
    // _menu.update(_d);

    if(enemy_ship_collision() == true) {
        _health.update(1,pad);
        _enemy.reset_seeker();
        pad.tone(500,0.05);
        wait(0.05);
    }
    if(enemy_projectile_collision() == true) {
        _enemy.reset_seeker();
        pad.tone(1000,0.05);
        wait(0.05);
        pad.tone(1500,0.05);
        wait(0.05);
    }
    // printf("collision check complete");
    
    // warning sound
    Vector2D hp = _health.get_hp();
    int health = hp.x;
    if(_shipno == 0 || 1) {
        if(health <= 2 ) {
            pad.tone(1300,0.1);
            wait(0.1);
        }
    }
    if(_shipno == 2) {
        if(health <= 1 ) {
            pad.tone(1300,0.1);
            wait(0.1);
        }
    }
}
void RosenEngine::get_pos()
{
    Vector2D ship_pos = _ship.get_pos();
    ship_xpos = ship_pos.x;
    ship_ypos = ship_pos.y;
    _weapons.init(ship_xpos, ship_ypos, ship_width);
    _ycursor = _menu.get_ycursor();

    if(_shipno == 0) {
        ship_width = 9;
        ship_height = 6;
    }
    if(_shipno == 1) {
        ship_width = 7;
        ship_height = 10;
    }
}
void RosenEngine::title(N5110 &lcd)
{
    _menu.title(lcd);
    _menu.update(_d);
}
int RosenEngine::get_ycursor()
{
    _ycursor = _menu.get_ycursor();
    return _ycursor;
}
int RosenEngine::get_shipno()
{
    _shipno = _menu.get_xcursor();
    return _shipno;
}
void RosenEngine::ship_select(N5110 &lcd)
{
    _menu.update(_d);
    _menu.disp_ships(lcd);
}
bool RosenEngine::enemy_ship_collision()
{
    // Vector2D ship_pos = _ship.get_pos();
    Vector2D seeker_pos = _enemy.get_seekerpos();
    int seeker_xpos = seeker_pos.x, seeker_ypos = seeker_pos.y;
    int sxcol = 0;
    int sycol = 0;
    int ship_x[9],ship_y[6],seeker_x[9],seeker_y[6];
    // printf("shipx = %d, shipy = %d\n",ship_xpos,ship_ypos);
    // create an array of all x positions for the ship sprite i.e along its width (ship_x)
    for(int cx = 0; cx<=9; cx=cx+1) {
        ship_x[cx] = ship_xpos + cx;
        seeker_x[cx] = seeker_xpos + cx;
        // printf("ship_x = %d, seeker_x = %d\n", ship_x[cx], seeker_x[cx]);
    }

    // create an array of all y positions for the ship sprite i.e along its height (ship_y)
    for(int cy = 0; cy<=6; cy=cy+1) {
        ship_y[cy] = (ship_ypos ) + cy ;
        seeker_y[cy] = seeker_ypos + cy;
        // printf("ship_y = %d, seeker_y = %d\n", ship_y[cy], seeker_y[cy]);
    }
    // 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]) {
                sxcol = 1;
            }
        }
    }
    for(int ny = 0; ny<=6; ny=ny+1) {
        for(int my = 0; my<=6; my=my+1) {
            if(ship_y[ny] == seeker_y[my]) {
                sycol = 1;
            }
        }
    }

    printf("sxcol = %d, sycol = %d\n", sxcol, sycol);
    if(sxcol == 1 && sycol == 1) {
        return true;
    } else {
        return false;
    }
}
bool RosenEngine::enemy_projectile_collision()
{
    Vector2D seeker_pos = _enemy.get_seekerpos();
    int lazer_x[3],seeker_x[9],seeker_y[38];
    int wxcol = 0;
    int wycol = 0;

    // get seekers x any y values and put into arrays
    for(int cx = 0; cx<=9; cx=cx+1) {
        seeker_x[cx] = seeker_pos.x + cx;
    }
    for(int cy = 0; cy<=6; cy=cy+1) {
        seeker_y[cy] = seeker_pos.y + cy;
    }

    // kestrel artemis missle
    if(_shipno == 0) {
        Vector2D missle_pos = _weapons.get_pos(_shipno);

        for(int cx = 0; cx<=9; cx=cx+1) {
            seeker_x[cx] = seeker_pos.x + cx;
            if(seeker_x[cx] == missle_pos.x) {
                printf("seeker_xpos = %f, missle_xpos = %f\n",seeker_pos.x,missle_pos.x);
                wxcol = 1;
            }
        }
        for(int cy = 0; cy<=7; cy=cy+1) {
            seeker_y[cy] = seeker_pos.y + cy;
            if(seeker_y[cy] == missle_pos.y + 4) {
                printf("seeker_ypos = %f, missle_ypos = %f\n",seeker_pos.y,missle_pos.y);
                wycol = 1;
            }
        }
        if(wxcol == 1 && wycol == 1) {
            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
            return true;
        }
    }


// imperion lazer
    if(_shipno == 1) {
        for(int cx = 0; cx<=3; cx=cx+1) {
            lazer_x[cx] = (ship_xpos + 2) + cx;
        }
        // ony register collision if seeker is above ship
        if(seeker_pos.y + 6 < ship_ypos) {
            // check all values of ship position against all values of seekers x position
            for(int nx = 0; nx<=3; nx=nx+1) {
                for(int mx = 0; mx<=9; mx=mx+1) {
                    if(lazer_x[nx] == seeker_x[mx]) {
                        wxcol = 1;
                        printf("lazer_x = %d,seeker_x = %f\n");
                    }
                }
            }
        }
        if(wxcol == 1 && A == true) {
            return true;
        } else {
            return false;
        }
    }
    if(_shipno == 2) {
        return false;
    }
}