Musallam Bseiso / Engine

Engine.cpp

Committer:
musallambseiso
Date:
2017-05-01
Revision:
11:10c01766f774
Parent:
10:b2dd5f484f98
Child:
12:d68c757d829a

File content as of revision 11:10c01766f774:

#include "Engine.h"

#define LEVEL_ONE 2
#define LEVEL_TWO 3
#define LEVEL_THREE 4
#define LEVEL_FOUR 5
#define LEVEL_FIVE 6

Engine::Engine()
{
    collisions = 0;
    wave_counter = 0;
    ammo = 3;
}

Engine::~Engine()
{
}


// Initialization function:

void Engine::init(int speed, N5110 &lcd, Gamepad &pad)
{
    _speed = speed;

    _friendly.init();       // friendly ship initialization
    _ship1.init(_speed);    // enemy ships initialization
    _ship2.init(_speed);
    _ship3.init(_speed);
    _ship4.init(_speed);
    _ship5.init(_speed);
    _ship6.init(_speed);
}

void Engine::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}


// ::: DRAWING FUNCTIONS :::

void Engine::draw(N5110 &lcd)
{
    _stats.grid_draw(lcd);                  // initial grid and border generation
    _stats.draw_health(lcd);                // health counter generation (initial state)
    _stats.check_health(lcd, collisions);   // health counter generation (later states)
    _stats.check_rocket(lcd, ammo);         // rocket and ammo generation
    _stats.wave_draw(lcd, wave_counter);    // wave counter generation 
    _friendly.draw(lcd);                    // friendly ship generation
    ships_draw(lcd);                        // enemy ships generation
}


// Enemy ships generation:

void Engine::ships_draw(N5110 &lcd)
{
    _ship1.draw(lcd);
    _ship2.draw(lcd);
    _ship3.draw(lcd);
    _ship4.draw(lcd);
    _ship5.draw(lcd);
    _ship6.draw(lcd);
}


// State change checker:

void Engine::checker(N5110 &lcd, Gamepad &pad)
{
    check_pass(pad);            // checks if enemy passed screen border and generates another
    check_death_all(lcd, pad);  // checks for collisions and eventual friendly death
    level_two(lcd, pad);        // checks if level two has been reached, same for below
    level_three(lcd, pad);
    level_four(lcd, pad);
    level_five(lcd, pad);
}


// Dynamic object position/velocity updater:

void Engine::update(N5110 &lcd, Gamepad &pad)
{
    _friendly.update(_d,_mag);
    _ship1.update();
    _ship2.update();
    _ship3.update();
    _ship4.update();
    _ship5.update();
    _ship6.update();
}


// Function to enable shooting and eliminating enemy ships:

void Engine::shoot(N5110 &lcd, Gamepad &pad)
{
    Vector2D friendly_pos = _friendly.get_pos();
    _x = friendly_pos.x+6;
    _y = friendly_pos.y+3;
    _bullet.init(_x, _y);
    
    if ((pad.check_event(Gamepad::B_PRESSED) == true) &&
        (ammo > 0))
    {    
        trigger = true;
    }
    
    if (trigger == true) 
    {   
        _bullet.draw(lcd);
        check_enemy_death(pad);
        pad.tone(1500.0,0.1);
        ammo = ammo - 1;
        trigger = false;
    }
}


// Checks if (each) enemy ship passed off screen and re-initializes ones that have: (Wave counter associated with ship1)

void Engine::check_pass(Gamepad &pad)
{
    Vector2D ship1_pos = _ship1.get_pos();
    
    if (ship1_pos.x < 0) 
    {
        _ship1.init(_speed);
        wave_counter = wave_counter + 1;
    }
    
    Vector2D ship2_pos = _ship2.get_pos();
    
    if (ship2_pos.x < 0) 
    {
        _ship2.init(_speed);
    }
    
    Vector2D ship3_pos = _ship3.get_pos();
    
    if (ship3_pos.x < 0) 
    {
        _ship3.init(_speed);
    }
    
    Vector2D ship4_pos = _ship4.get_pos();
    
    if (ship4_pos.x < 0) 
    {
        _ship4.init(_speed);
    }
    
    Vector2D ship5_pos = _ship5.get_pos();
    
    if (ship5_pos.x < 0) 
    {
        _ship5.init(_speed);
    }
    
    Vector2D ship6_pos = _ship6.get_pos();
    
    if (ship6_pos.x < 0) 
    {
        _ship6.init(_speed);
    }
}


// Checks if enemy ship has been destroyed:

void Engine::check_enemy_death(Gamepad &pad)
{
    Vector2D friendly_pos = _friendly.get_pos();
    
    if (friendly_pos.y < 5) 
    {
        _ship1.init(_speed);
    } 
    else if ((friendly_pos.y < 11) &&
            (friendly_pos.y > 4))
    {
        _ship2.init(_speed);
    }
    else if ((friendly_pos.y < 17) &&
            (friendly_pos.y > 10))
    {
        _ship3.init(_speed);
    }
    else if ((friendly_pos.y < 25) &&
            (friendly_pos.y > 16))
    {
        _ship4.init(_speed);
    }
    else if ((friendly_pos.y < 31) &&
            (friendly_pos.y > 24))
    {
        _ship5.init(_speed);
    }
    else
    {
        _ship6.init(_speed);
    }    
}


// Checks for collisions between friendly and enemy ship: (Associates collisions counter)

void Engine::check_death(Gamepad &pad, Vector2D ship_pos)
{
    Vector2D friendly_pos = _friendly.get_pos();
    
    if ((friendly_pos.y >= ship_pos.y-5) && // change 5 to friendly size and 6 to ship size
        (friendly_pos.y <= ship_pos.y+5) && 
        (friendly_pos.x+6 >= ship_pos.x) && 
        (friendly_pos.x+6 <= ship_pos.x+5)) 
    {
        pad.tone(800.0,0.1);
        collisions = collisions + 1;
    }
}


// Checks for collisions between friendly and each enemy ship:

void Engine::check_death_all(N5110 &lcd, Gamepad &pad)
{
    Vector2D ship1_pos = _ship1.get_pos();
    Vector2D ship2_pos = _ship2.get_pos();
    Vector2D ship3_pos = _ship3.get_pos();
    Vector2D ship4_pos = _ship4.get_pos();
    Vector2D ship5_pos = _ship5.get_pos();
    Vector2D ship6_pos = _ship6.get_pos();
    
    check_death(pad, ship1_pos);
    check_death(pad, ship2_pos);
    check_death(pad, ship3_pos);
    check_death(pad, ship4_pos);
    check_death(pad, ship5_pos);
    check_death(pad, ship6_pos);
    game_over(lcd, pad);    // game over sequence, if health subzero
}


// Level two sequence, occurs if wave 5 is reached:

void Engine::level_two(N5110 &lcd, Gamepad &pad)
{
    if (wave_counter == 5) 
    {
        lcd.drawRect(0,0,84,48,FILL_WHITE);
        lcd.printString("Nice! Level 2",0,1);
        lcd.printString("  Press Back!  ",0,4);
        lcd.refresh();
        wait(1);
        
        while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
            wait(0.1);
        }
        
        init(LEVEL_TWO, lcd, pad);
        wave_counter = 6;
    }
}


// Level three sequence, occurs if wave 25 is reached:

void Engine::level_three(N5110 &lcd, Gamepad &pad)
{
    if (wave_counter == 25) 
    {
        lcd.drawRect(0,0,84,48,FILL_WHITE);
        lcd.printString("Nice! Level 3",0,1);
        lcd.printString("  Press Back!  ",0,4);
        lcd.refresh();
        wait(1);
        
        while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
            wait(0.1);
        }
        
        init(LEVEL_THREE, lcd, pad);
        wave_counter = 26;
    }
}


// Level three sequence, occurs if wave 50 is reached:

void Engine::level_four(N5110 &lcd, Gamepad &pad)
{
    if (wave_counter == 50) 
    {
        lcd.drawRect(0,0,84,48,FILL_WHITE);
        lcd.printString("Nice! Level 4",0,1);
        lcd.printString("  Press Back!  ",0,4);
        lcd.refresh();
        wait(1);
        
        while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
            wait(0.1);
        }
        
        init(LEVEL_FOUR, lcd, pad);
        wave_counter = 51;
    }
}


// Level five sequence, occurs if wave 100 is reached:

void Engine::level_five(N5110 &lcd, Gamepad &pad)
{
    if (wave_counter == 100) 
    {
        lcd.drawRect(0,0,84,48,FILL_WHITE);
        lcd.printString("5. Good Luck!",0,1);
        lcd.printString("  Press Back!  ",0,4);
        lcd.refresh();
        wait(1);
        
        while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
            wait(0.1);
        }
        
        init(LEVEL_FIVE, lcd, pad);
        wave_counter = 101;
    }
}


// Game over sequence, ends game if health is below zero:

void Engine::game_over(N5110 &lcd, Gamepad &pad)
{
    if (collisions >= 6) 
    {
        lcd.drawRect(0,0,84,48,FILL_WHITE);
        lcd.printString("  You Lose! ",0,1);
        lcd.printString("  Press Back! ",0,4);
        lcd.refresh();
        wait(1);
        
        while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
            wait(0.1);
        }
        
        init(LEVEL_ONE, lcd, pad);
        collisions = 0;
        wave_counter = 0;
        ammo = 3;
    }
}