Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

https://os.mbed.com/media/uploads/evanso/84bc1a30759fd6a1e3f1fd1fae3e97c2.png

Hello, soldier, you have been specially selected as the defender of planet earth.

Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.

But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.

As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.

GameEngine/GameEngine.cpp

Committer:
evanso
Date:
2020-05-14
Revision:
32:c006a9882778
Parent:
31:6015e8ed859c
Child:
33:7fedd8029473

File content as of revision 32:c006a9882778:

#include "GameEngine.h"

GameEngine::GameEngine() {
    
}
 
GameEngine::~GameEngine() {
    
}

void GameEngine::init() {
    srand(pad.read_adc()*64000);
    pad.init();
    lcd.init();
    spaceship.init();
    map.init(pad);
    
    //spawn 5 aliens at start of game
    for(int i = 0; i < 6; i++){
        create_alien();
    }
    spawn_alien_counter = 250;
    spawn_alien_counter_two = 0;
    spaceship_lives = 3;
    spaceship_destroyed = false;
    reset_map_counter = 0;
    smart_bomb_counter = 5;
}

void GameEngine::gameplay_loop() {
    // clear screen and set contrast
    lcd.setContrast(pad.read_pot1());          
    lcd.clear();
    
    // Gets movements  
    read_joystick_direction();
    spaceship.movement(d_);
    create_weapons();
    spawn_aliens();

    // Draws 
    if (!spaceship_destroyed){
        spaceship.draw(lcd);
    }
    map.draw_map(lcd, d_);
    draw_aliens();
    draw_bullets();
    draw_explosions(); 
    
    // Resets map after set frames so spaceship explosion animation showes
    if(spaceship_destroyed){
        reset_map_counter++; 
        if(reset_map_counter == 50){
            reset_map();
        }
    }
    
    // refresh's screen
    lcd.refresh(); 
}

void GameEngine::read_joystick_direction(){
    d_ = pad.get_direction();
}

void GameEngine::spawn_aliens(){
    int x = floor(spawn_alien_counter);
    if(spawn_alien_counter_two%x ==  0){
        create_alien();  
    }
    
    if(spawn_alien_counter > 125){
        spawn_alien_counter --;
    }
    spawn_alien_counter_two++;
}

void GameEngine::create_weapons(){
    //Spaceship bullets
    if (pad.A_pressed()){
        // Bullet object
        Weapons new_bullet;
        
        new_bullet.init(spaceship.get_pos(), 
        spaceship.get_spaceship_sprite_direction(), true);
        
        // Stores bullet object in vector 
        bullet_vector.push_back(new_bullet);
    }
    // Smart bomb
    if (pad.B_pressed() && smart_bomb_counter > 0){
        Weapons weapons;
        
        weapons.smart_bomb(lcd);
        smart_bomb_counter--;
        
        //Deletes alien object if on screen
        for (int i = 0; i < alien_vector.size(); i++){
            Vector2D alien_pos = alien_vector[i].get_pos(); 
            
            if(alien_pos.x <= 84 && alien_pos.x >= -6){
                create_explosion(alien_pos); 
                alien_vector.erase(alien_vector.begin()+ i);  
            }
        }
        
    }
}

void GameEngine::create_alien(){
    int position_x_start = 0;
    
    // Alien object
    Alien new_alien;
    
    //Creats randon number for x pos for x > 84 and x <0
    if(rand() % 2){
    position_x_start = rand() % 84 + 84;
    }else{
    position_x_start =rand() % 83 - 84;
    }
    
    new_alien.init(pad, position_x_start, (rand() % 45));
    
    // Stores alien object in vector 
    alien_vector.push_back(new_alien);
}

void GameEngine::create_explosion(Vector2D destroyed_position){
    // explosion object
    Explosion new_explosion;
    
    new_explosion.init(destroyed_position);
    
    // Stores explosion object in vector 
    explosion_vector.push_back(new_explosion);
}

void GameEngine::draw_bullets(){
    // interates over bullet vectors, moves and draws bullet object
    
    // Alien bullets
    for (int i = 0; i < alien_bullet_vector.size(); i++){
        alien_bullet_vector[i].draw_alien_bullet(lcd, d_);
        
        // deletes alien bullet object after bullet has moved set distance   
        if(alien_bullet_vector[i].get_bullet_delete_counter() >> 7){
             alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
        }
        
        // Deletes bullet and shpaceship if collision detected
        if (spaceship.check_collision(alien_bullet_vector[i]) && 
        !spaceship_destroyed ){
            create_explosion(spaceship.get_pos());
            
            spaceship_destroyed = true;
            alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);     
        }
    }
    
    // Spaceship bullets 
    for (int i = 0; i < bullet_vector.size(); i++){
        bullet_vector[i].draw_bullet(lcd);
         
        // deletes bullet object after bullet has moved set distance   
        if(bullet_vector[i].get_bullet_delete_counter() >> 5){
             bullet_vector.erase(bullet_vector.begin()+ i);
        }
    }
}

void GameEngine::draw_aliens(){
    // interates over alien vector and draws each new_alien object 
    for (int i = 0; i < alien_vector.size(); i++){
        alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, 
        map.get_length_map(), map.get_position_x_map());
        
        // deleted spaceship if alien ship touches spaceship
        if (spaceship.check_alien_collision(alien_vector[i]) && 
        !spaceship_destroyed){
            create_explosion(spaceship.get_pos());
            spaceship_destroyed = true;  
        }
        
        // alien sprites fire bullet randomley
        if (alien_vector[i].get_alien_fire_counter()%60 == 0){
            Weapons alien_bullet;
            
            // fires bullet towards direction of spaceship
            bool alien_bullet_direction = false;
            Vector2D spaceship_pos = spaceship.get_pos();
            Vector2D alien_pos = alien_vector[i].get_pos();
            if(spaceship_pos.x > alien_pos.x){
                alien_bullet_direction = true;
            }
            
            alien_bullet.init(alien_vector[i].get_pos(), 
            alien_bullet_direction , false);
        
            // Stores bullet object in vector 
            alien_bullet_vector.push_back(alien_bullet); 
        }
        
        // Deletes bullet and alien if collision detected and draws explosion
        for (int x = 0; x < bullet_vector.size(); x++){
            if (alien_vector[i].check_collision(bullet_vector[x])){
                create_explosion(alien_vector[i].get_pos());
            
                bullet_vector.erase(bullet_vector.begin()+ i);    
                alien_vector.erase(alien_vector.begin()+ i);  
            }
        }
    }
}

void GameEngine::draw_explosions(){
    // interates over expoltion vector and draws each explosion object then 
    // deleted object after set size
    for (int i = 0; i < explosion_vector.size(); i++){
        explosion_vector[i].draw_explosion(lcd);
        
        // delete explosion after reaches set size
        if(explosion_vector[i].get_explosion_radius() == 8){
             explosion_vector.erase(explosion_vector.begin()+ i);    
        }
    }
}

void GameEngine::reset_map(){
    spaceship.init();
    spaceship_lives--;
    spaceship_destroyed = false;
    reset_map_counter = 0;
}