ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

RosenEngine/RosenEngine.cpp

Committer:
ikenna1
Date:
2019-04-20
Revision:
33:7a814c874c57
Parent:
32:098fbc1222cd
Child:
34:6d0786582d81

File content as of revision 33:7a814c874c57:

#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,2);
    _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)
{
    lcd.drawRect(0,0,78,48,FILL_TRANSPARENT);
    _health.draw_health(lcd,_shipno);
    _health.draw_shields(lcd);
    _enemy.draw_seeker(lcd);
    _enemy.draw_shooter(lcd,2);
    _enemy.draw_shw(lcd,pad,2);
    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);
    _enemy.update_shooter(ship_xpos, ship_ypos);
    _enemy.update_shw();
    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();
    }
    if(shooter_weapon_collision(3) == true) {
        _health.update(1,pad);
        pad.tone(500,0.05);
        wait(0.05);
    }
    if(seeker_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 || _shipno == 1) {
        if(health <= 2 ) {
            pad.tone(1300,0.05);
            wait(0.05);
        }
    } else if(_shipno == 2) {
        if(health <= 1 ) {
            pad.tone(1300,0.05);
            wait(0.05);
        }
    }
    printf("health = %d||shipno = %d",health,_shipno);
}
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,_shipno);
    _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::seeker_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;
    }
}
Vector2D RosenEngine::shooter_projectile_collision()
{
    Vector2D shooter1_pos = _enemy.get_sh1pos();
    Vector2D shooter2_pos = _enemy.get_sh2pos();
    Vector2D shooter3_pos = _enemy.get_sh3pos();
    int shooter1_x[11],shooter1_y[10],shooter2_x[11],shooter2_y[10],shooter3_x[11],shooter3_y[10];
    int wx1col = 0;
    int wx2col = 0;
    int wx3col = 0;
    int wy1col = 0;
    int wy2col = 0;
    int wy3col = 0;
    

    // get shooters x any y values and put into arrays
    for(int cx = 0; cx<=11; cx=cx+1) {
        shooter1_x[cx]= shooter1_pos.x + cx;
    }
    for(int cy = 0; cy<=10; cy=cy+1) {
        shooter1_y[cy]= shooter1_pos.y + cy;
    }
    for(int cx = 0; cx<=11; cx=cx+1) {
        shooter2_x[cx]= shooter2_pos.x + cx;
    }
    for(int cy = 0; cy<=10; cy=cy+1) {
        shooter2_y[cy]= shooter2_pos.y + cy;
    }
    for(int cx = 0; cx<=11; cx=cx+1) {
        shooter3_x[cx]= shooter3_pos.x + cx;
    }
    for(int cy = 0; cy<=10; cy=cy+1) {
        shooter3_y[cy]= shooter3_pos.y + cy;
    }

    if(_shipno == 0) {
        Vector2D missle_pos = _weapons.get_pos(_shipno);

        for(int cx = 0; cx<=11; cx=cx+1) {
            if(shooter1_x[cx] == missle_pos.x) {
                wx1col = 1;
            }
            if(shooter2_x[cx] == missle_pos.x) {
                wx2col = 1;
            }
            if(shooter3_x[cx] == missle_pos.x) {
                wx3col = 1;
            }
        }

        for(int cy = 0; cy<=10; cy=cy+1) {
            if(shooter1_y[cy] == missle_pos.y + 4) {
                wy1col = 1;
            }
            if(shooter2_y[cy] == missle_pos.y + 4) {
                wy2col = 1;
            }
            if(shooter3_y[cy] == missle_pos.y + 4) {
                wy3col = 1;
            }
        }
        if(wx1col == 1 && wy1col == 1) {
            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
            return {1,1};
        } else if(wx2col == 1 && wy2col == 1) {
            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
            return {1,2};
        } else if(wx3col == 1 && wy3col == 1) {
            _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
            return {1,3};
        } else {
            return {0,0};
        }
    }


    // 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(shooter1_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<=11; mx=mx+1) {
                    if(lazer_x[nx] == shooter1_x[mx]) {
                        wx1col = 1;
                        printf("lazer_x = %f,seeker_x = %d\n",lazer_x[nx],seeker_x[mx]);
                    }
                }
            }
        }
        if(shooter2_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<=11; mx=mx+1) {
                    if(lazer_x[nx] == shooter2_x[mx]) {
                        wx2col = 1;
                        printf("lazer_x = %f,seeker_x = %d\n",lazer_x[nx],seeker_x[mx]);
                    }
                }
            }
        }
        if(shooter3_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<=11; mx=mx+1) {
                    if(lazer_x[nx] == shooter3_x[mx]) {
                        wx3col = 1;
                        printf("lazer_x = %f,seeker_x = %d\n",lazer_x[nx],seeker_x[mx]);
                    }
                }
            }
        }
        if(wx1col == 1 && A == true) {
            return {1,1};
        else if(wx2col == 1 && A == true){
            return {1,2};
            }
        else if(wx2col == 1 && A == true){
            return {1,3};
            }
        else{
            return {0,0};
            }
    }
    if(_shipno == 2) {
        return {0,0};
    }

}
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",lazer_x[nx],seeker_x[mx]);
                    }
                }
            }
        }
        if(wxcol == 1 && A == true) {
            return true;
        } else {
            return false;
        }
    }
    if(_shipno == 2) {
        return false;
    }
}
bool RosenEngine::shooter_weapon_collision(int sh_no)
{
    Vector2D shw1_pos = _enemy.get_sh1pos();
    Vector2D shw2_pos = _enemy.get_sh2pos();
    Vector2D shw3_pos = _enemy.get_sh3pos();

    bool shw1xcol = false;
    bool shw1ycol = false;
    bool shw2xcol = false;
    bool shw2ycol = false;
    bool shw3xcol = false;
    bool shw3ycol = false;

    int ship_x[9];
    int ship_y[6];

    for(int cx = 0; cx<=9; cx=cx+1) {
        ship_x[cx] = ship_xpos + cx;
        if(ship_x[cx] == shw1_pos.x) {
            shw1xcol = true;
        }
        if(ship_x[cx] == shw2_pos.x) {
            shw2xcol = true;
        }
        if(ship_x[cx] == shw3_pos.x) {
            shw3xcol = true;
        }
    }
    for(int cy = 0; cy<=6; cy=cy+1) {
        ship_y[cy] = (ship_ypos ) + cy ;
        if(ship_y[cy] == shw1_pos.y) {
            shw1ycol = true;
        }
        if(ship_y[cy] == shw2_pos.y) {
            shw2ycol = true;
        }
        if(ship_y[cy] == shw3_pos.y) {
            shw3ycol = true;
        }
    }
    if(sh_no == 1) {
        shw2xcol = false;
        shw2ycol = false;
        shw3xcol = false;
        shw3ycol = false;
    }
    if(sh_no == 2) {
        shw3xcol = false;
        shw3ycol = false;
    }

    if(shw1xcol && shw1ycol == true || shw2xcol && shw2ycol == true || shw3xcol && shw3ycol == true) {
        return true;
    } else {
        return false;
    }
}
void RosenEngine::score(int points)
{
    _score = _score + points;
}