ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jkeo

Dependencies:   mbed

SpaceInvaderEngine/SpaceInvaderEngine.cpp

Committer:
josh_ohara
Date:
2020-05-01
Revision:
18:828e9f6ddfdb
Parent:
16:987f72d9bb8f
Child:
20:0b6f1cfc5be6

File content as of revision 18:828e9f6ddfdb:


#include "SpaceInvaderEngine.h"

SpaceInvaderEngine::SpaceInvaderEngine()
{

}

SpaceInvaderEngine::~SpaceInvaderEngine()
{

}

void SpaceInvaderEngine::init(int ship_height, int ship_width, int alien_size, int no_aliens, int armada_column_size, int armada_row_size, int cover_y, int cover1_x, int cover2_x, int cover3_x, int no_rocks) {
    
    S1_height = ship_height;
    S1_width = ship_width;
    A1_size = alien_size;
    
    Vector2D ship_pos = S1.get_position();
    S1x = ship_pos.x;
    S1y = ship_pos.y;
    
    N = no_aliens;
    CS = armada_column_size;
    RS = armada_row_size;
    
    Cy = cover_y;
    C1x = cover1_x;
    C2x = cover2_x;
    C3x = cover3_x;
    NR = no_rocks;
    
    A1.init(N,A1_size,CS,RS);
    S1.init(S1_height,S1_width);
    BS1.init();
    C1.init(C1x,Cy,NR);
    C2.init(C2x,Cy,NR);
    C3.init(C3x,Cy,NR);
 
}

void SpaceInvaderEngine::read_input(Gamepad &pad)
{
    D = pad.get_direction();
    Mag = pad.get_mag();
}
    
void SpaceInvaderEngine::render(N5110 &lcd)
{
    S1.render(lcd);
    A1.render(lcd);
    BS1.render(lcd);
    C1.render(lcd);
    C2.render(lcd);
    C3.render(lcd);
}

void SpaceInvaderEngine::update(Gamepad &pad, N5110 &lcd, int counter)
{   
    get_ship_pos();
    S1.update(D,Mag);
    BS1.update(pad, lcd, S1x+4, S1y, counter);
    A1.update(pad, counter);
    ship_bullet_alien_collision(pad, lcd);
    ship_bullet_cover1_collision(pad, lcd);
    ship_bullet_cover2_collision(pad, lcd);
    ship_bullet_cover3_collision(pad, lcd);
}

void SpaceInvaderEngine::get_ship_pos()
{
    Vector2D ship_pos = S1.get_position();
    S1x = ship_pos.x;
    S1y = ship_pos.y;
}

void SpaceInvaderEngine::ship_bullet_alien_collision(Gamepad &pad, N5110 &lcd){
    vector<Alien> alien_vector = A1.get_vector();
    vector<Bullet> bullet_vector = BS1.get_vector();
    for(int i = 0; i < alien_vector.size(); i++){
        Vector2D alien_position = alien_vector[i].get_position();
        int alien_x = alien_position.x;
        int alien_y = alien_position.y;
        bool alien_life = alien_vector[i].get_life();
        for (int n = 0; n < bullet_vector.size(); n++){
            Vector2D bullet_position = bullet_vector[n].get_position();
            int bullet_x = bullet_position.x;
            int bullet_y = bullet_position.y;
            bool hit = bullet_vector[n].get_hit();
            if((alien_x <= bullet_x) && 
            (bullet_x <= alien_x + A1_size - 1)&&
            (alien_y <= bullet_y) &&
            (bullet_y <= alien_y + A1_size - 1)&&
            (hit==false)&&
            (alien_life == true)) {
                bool T = true;
                bool F = false;
                A1.set_life(i,F);
                BS1.set_hit(n,T);
            }
        }
    }
}

void SpaceInvaderEngine::ship_bullet_cover1_collision(Gamepad &pad, N5110 &lcd){
    vector<Rock> cover_vector_1 = C1.get_vector();
    vector<Bullet> bullet_vector = BS1.get_vector();
    for(int i = 0; i < cover_vector_1.size(); i++){
        Vector2D rock_position = cover_vector_1[i].get_position();
        int rock_x = rock_position.x;
        int rock_y = rock_position.y;
        bool rock_life = cover_vector_1[i].get_life();
        for (int n = 0; n < bullet_vector.size(); n++){
            Vector2D bullet_position = bullet_vector[n].get_position();
            int bullet_x = bullet_position.x;
            int bullet_y = bullet_position.y;
            bool hit = bullet_vector[n].get_hit();
            if((rock_x <= bullet_x) && 
            (bullet_x <= rock_x + 2)&&
            (rock_y <= bullet_y) &&
            (bullet_y <= rock_y + 2)&&
            (hit==false)&&
            (rock_life==true)){
                bool T = true;
                bool F = false;
                C1.set_life(i,F);
                BS1.set_hit(n,T);
            }
        }
    }
}

void SpaceInvaderEngine::ship_bullet_cover2_collision(Gamepad &pad, N5110 &lcd){
    vector<Rock> cover_vector_2 = C2.get_vector();
    vector<Bullet> bullet_vector = BS1.get_vector();
    for(int i = 0; i < cover_vector_2.size(); i++){
        Vector2D rock_position = cover_vector_2[i].get_position();
        int rock_x = rock_position.x;
        int rock_y = rock_position.y;
        bool rock_life = cover_vector_2[i].get_life();
        for (int n = 0; n < bullet_vector.size(); n++){
            Vector2D bullet_position = bullet_vector[n].get_position();
            int bullet_x = bullet_position.x;
            int bullet_y = bullet_position.y;
            bool hit = bullet_vector[n].get_hit();
            if((rock_x <= bullet_x) && 
            (bullet_x <= rock_x + 2)&&
            (rock_y <= bullet_y) &&
            (bullet_y <= rock_y + 2)&&
            (hit==false)&&
            (rock_life==true)){
                bool T = true;
                bool F = false;
                C2.set_life(i,F);
                BS1.set_hit(n,T);
            }
        }
    }
}

void SpaceInvaderEngine::ship_bullet_cover3_collision(Gamepad &pad, N5110 &lcd){
    vector<Rock> cover_vector_3 = C3.get_vector();
    vector<Bullet> bullet_vector = BS1.get_vector();
    for(int i = 0; i < cover_vector_3.size(); i++){
        Vector2D rock_position = cover_vector_3[i].get_position();
        int rock_x = rock_position.x;
        int rock_y = rock_position.y;
        bool rock_life = cover_vector_3[i].get_life();
        for (int n = 0; n < bullet_vector.size(); n++){
            Vector2D bullet_position = bullet_vector[n].get_position();
            int bullet_x = bullet_position.x;
            int bullet_y = bullet_position.y;
            bool hit = bullet_vector[n].get_hit();
            if((rock_x <= bullet_x) && 
            (bullet_x <= rock_x + 2)&&
            (rock_y <= bullet_y) &&
            (bullet_y <= rock_y + 2)&&
            (hit==false)&&
            (rock_life==true)){
                bool T = true;
                bool F = false;
                C3.set_life(i,F);
                BS1.set_hit(n,T);
            }
        }
    }
}