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-15
Revision:
34:85ccc16f24d2
Parent:
33:7fedd8029473
Child:
36:27aa597db3d2

File content as of revision 34:85ccc16f24d2:

#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_alien_rate = 250;
    alien_number_counter = 3;
    spawn_alien_counter = 0;
    spaceship_lives = 3;
    spaceship_destroyed = false;
    reset_map_counter = 0;
    smart_bomb_counter = 5;
    create_people();
}

void GameEngine::gameplay_loop() {
    // clear screen and set contrast
    lcd.setContrast(pad.read_pot1());          
    lcd.clear();
    
    // creat objects 
    create_weapons();
    spawn_aliens();
    spawn_people();

    //If spaceship is destroyed can't move
    if (!spaceship_destroyed){
        read_joystick_direction();
        spaceship.movement(d_);
        spaceship.draw(lcd);
    }
    
    // Draws objects
    map.draw_map(lcd, d_);
    draw_aliens();
    draw_bullets();
    draw_explosions();
    draw_people(); 
    
    // Resets map after set frames so spaceship explosion animation showes
    if(spaceship_destroyed){
        reset_map_counter++; 
        d_ = CENTRE;
        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(){
    if(alien_vector.size() <= alien_number_counter){
        create_alien();
    }
    
    //slowley incresing the alien counter as game goes on to make harder
    //int rounded_spawn_rate = floor(spawn_alien_rate);
    if(spawn_alien_counter%750 ==  0){
      //alien_number_counter++;      
    }
    
    spawn_alien_counter++;
}

void GameEngine::spawn_people(){
    if(people_vector.size() <= 5){
        create_people();
    }
}

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.smart_bomb(lcd);
        smart_bomb_counter--;
        
        //Deletes alien object if on screen
        for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){
            Vector2D alien_pos = alien_vector[i].get_pos(); 
            
            if(alien_pos.x <= 84 && alien_pos.x >= -6){
                create_explosion(alien_pos); 
                //delete person object if was carried by destroyed alien
                if(alien_vector[i].get_collision_people_element() > -1){
                    people_vector.erase(people_vector.begin() + 
                    alien_vector[i].get_collision_people_element()); 
                } 
                alien_vector.erase(alien_vector.begin()+ i); 
            }
        }
        
    }
}

void GameEngine::create_people(){
    // People object
    People people;
    
    people.init(pad, (rand() % 168 - 84));
    
    // Stores alien object in vector 
    people_vector.push_back(people);
}

void GameEngine::draw_people(){
    for (int i = 0; i < people_vector.size(); i++){
        people_vector[i].draw_people(lcd, d_, 
        map.get_length_map(), map.get_position_x_map());  
        
        //errase person if at top of screen as captured by alien and alien is 
        //set to track mode
        Vector2D people_pos =  people_vector[i].get_pos();
        if(people_pos.y <= 0){
            for (int x = 0; x < alien_vector.size(); x++){
                if(alien_vector[x].get_collision_people_element() == i){
                   alien_vector[x].set_track_flag(true);
                }  
            }
            people_vector.erase(people_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() >> 5){
             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++){
        // Checks Alien and person collision 
        for (int x = 0; x < people_vector.size(); x++){
            if (!people_vector[x].get_alien_collision_flag() &&
            people_vector[x].check_alien_collision(alien_vector[i])){  
                alien_vector[i].set_collision_people_element(x); 
            }
        }
        //draws collision if detected
        if(alien_vector[i].get_collision_people_element() > -1){
            alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, 
            map.get_length_map(), map.get_position_x_map(),true);
        }else{
            alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, 
            map.get_length_map(), map.get_position_x_map(),false);
        }
        
        // 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());
                
                //delete person object if was carried by destroyed alien
                if(alien_vector[i].get_collision_people_element() > -1){
                    people_vector.erase(people_vector.begin() + 
                    alien_vector[i].get_collision_people_element()); 
                }
            
                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;
    
    // erase aliens so redrawn in random positions when respawning spaceship
    for (int i = (alien_vector.size() - 1); i >= 0 ; i--){
        alien_vector.erase(alien_vector.begin()+ i);  
    }
    // erase all people so redrawn at bottom of map
    for (int i = (people_vector.size() - 1); i >= 0 ; i--){
         people_vector.erase(people_vector.begin()+ i);      
    }
    // erase all palien bullets
    for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--){
         alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);      
    }
}