Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

game/enemies.h

Committer:
Noximilien
Date:
2019-04-16
Revision:
30:d454d0cb72bc
Parent:
29:579e00b7f118
Child:
31:becb8f6bf7b7

File content as of revision 30:d454d0cb72bc:

#ifndef ENEMIES_H
#define ENEMIES_H

#include "constants.h"
#include "geometry.h"
#include "game.h"

/** A bool that changes states between whehter to go up or down in y-direction.*/
bool switch_enemy_y_dir = false;
/** Enemy speed movement - increasing the value will increase the speed.*/
extern int enemy_speed = 1;
/** Enemy blast speed - increasing the value will increase the speed.*/
extern int enemy_blast_speed = 2;
/** Maximum enemies allowed on the screen */
const int max_enemies = 4;
/** Maximum enemies' blasts allowed on the screen*/
const int max_enemy_blasts = max_enemies*2;

/** Enemy Class
  * @brief A class to describe the states of one enemy ship.
  * @author Dmitrijs Griskovs
  * @date 15/04/2019
  */
class Enemy : public GameObject {
public:
/** @brief Enemy spawn function.
  * @details This function spawns an enemy on the right side of the screen at the 
  * x-direction LCD limit(84) and at random position in the y-direction.
  */
    void spawn() {
        // giving the enemy the spawning positions 
        Point spawn_pos(screen_width, game_area_y + rand() % (game_area_height - enemy2_height));
        GameObject::spawn(spawn_pos);
        dead = false;
        dead_counter = 0;
        blast_countdown = 0;
    }
/** @brief enemy death sound and status reset.
  * @details A function that resets the status of death of the enemy to be True,
  * and plays a death sound when an enemy was hit.
  */
    void die() {
        gamepad.tone(123,0.3);
        dead = true;
        dead_counter = 3;
    }
/** @brief Draws and updates enemy sprites on the screen and substracts the score
  * if the ship leaves the screen limits.
  *
  * @details The function draws each individual enemy ship on the screen, and 
  * changes their x and y positions each time the function is called. It
  * contains "if" statment for the the y-direction movement border limits so
  * that when they are reached, the ship would move the opposite way in y-direction.
  * Also, this function monitors the death status of the enemy ships, if 
  * TRUE, then it calls for the enemy explosion animation to be executed.
  * To conclude, it sets the "active" status of the ship to FALSE when it leaves
  * the screen limits and substracts the in game score by -50.
  */    
    void updateAndDraw() {
        pos.x -= enemy_speed;
        if (game_score >= 200){
            if (switch_enemy_y_dir == false){pos.y -= 1;}
            else if(switch_enemy_y_dir == true){pos.y += 1;}
            if (pos.y > (game_area_height - enemy2_height)){
                switch_enemy_y_dir = false;
            }
            else if (pos.y < game_area_y){
                switch_enemy_y_dir = true;   
            }
        }
        if (!dead) { drawSprite(pos, enemy2_sprite);} 
        else { updateAndDrawDeathExplosion();}
        
        if (pos.x < 0) {
            game_score -= 50;
            score_count_for_difficulty -= 50; 
            active = false;
        }
    }
/** @brief The function draws enemy explosion sprite
  * @details This is an explosion function that draws the enemy ships explosion 
  * sprites when the player's shot hits them or when the player collides with them.
  * the animation consists of two sprites "Exploded" and "Half exploded".
  */        
    void updateAndDrawDeathExplosion() {
        if (dead_counter > 0) {
            dead_counter--;
            if (dead_counter == 2){    
                drawSprite(pos, enemy2_half_exploded_sprite);
            } else if (dead_counter == 1){
                drawSprite(pos, enemy2_exploded_sprite);
            }
        } else {
            active = false;
        }
    }
    
    bool dead;
    int dead_counter;
    int blast_countdown;
};

    
/** Enemies Class
  * @brief The class that describes the states and actions of several enemy ships.
  * @details It includes random position generation (y - position),enemy ship 
  * shoots drawing and control of time intervals between enemy shots.
  * @author Dmitrijs Griskovs
  * @date 15/04/2019
  */  
class Enemies {
public:
    Enemy enemy_blasts[max_enemy_blasts];
    Enemy enemies[max_enemies];
    CircleBounds enemy_bounds;
    CircleBounds enemy_blast_bounds;
    
/** A constructor for the enemy's sprite body circle area and the blast circle 
  * area. It sets the circle radius for collsion callculations.
  */
    Enemies () {
        enemy_bounds.center.x = 5;
        enemy_bounds.center.y = 3;
        enemy_bounds.radius = 6;

        enemy_blast_bounds.center.x = 0;
        enemy_blast_bounds.center.y = 1;
        enemy_blast_bounds.radius = 1;
    }
/** @brief This function spawns a new enemy on the screen.
  * @details It spawns a new enemy on the screen when there is a free space
  * in the enemy aray.
  */  
    void spawnNewEnemy() {
        int found = -1;
        for (int i = 0; i < max_enemies; ++i) {
            if (!enemies[i].active) {
                found = i;
                break;
            }
        }
        
        if (found != -1) {
            enemies[found].spawn();
        }
    }
/** @brief Draws the ships and the blasts.
  * This function draws the enemy ships and enemy blasts whenever they are 
  * avaialbe to move. Also, it limits the fire rate of the enemy ships.
  */  
    void updateAndDrawEnemies() {
        for (int i = 0; i < max_enemies; ++i) {
            if (enemies[i].active){
                enemies[i].updateAndDraw();
                
                // Spawn blast on enemy if counter is ready
                enemies[i].blast_countdown -= 1;
                if (enemies[i].blast_countdown <= 0) {
                    bool fired = fireEnemyBlast(enemies[i]);
                    if (fired) {
                        enemies[i].blast_countdown = 20;
                    }
                }
            }
        }
    }
/** @brief activates a blast and gives the start positions
  * @details This function fires a blast whenever it is free in the blast array.
  * If the blast is free to be shot, it will become active and will get the.
  * positions of x and y in front of the enemy ship
  * @param enemy ~~~~~~~~~~~~~~~~~~~~~~~~~~(const Enemy&)
  */
    bool fireEnemyBlast(const Enemy& enemy) {
        int found = -1;
        for (int i = 0; i < max_enemy_blasts; ++i) {
            if (!enemy_blasts[i].active) {
                found = i;
                break;
            }
        }
        if (found != -1) {
            enemy_blasts[found].active = true;
            enemy_blasts[found].pos.x = enemy.pos.x;
            enemy_blasts[found].pos.y = enemy.pos.y + enemy2_height / 2;
            gamepad.tone(500,0.1);
            return true;
        }
        return false;
    }
/** @brief draws the blasts on the screen.
  * @details Whenever the blast is active, this function draws the blast accross
  * the x-direction of the screen until it reaches the left screen limit and then
  * becomes inactive, therefore freeing a space in the blast array. 
  */
    void updateAndDrawEnemyBlasts() {
        for (int i = 0; i < max_enemy_blasts; ++i) {
            if (enemy_blasts[i].active) {
                enemy_blasts[i].pos.x -= enemy_blast_speed;
                if (enemy_blasts[i].pos.x <= -1){
                    enemy_blasts[i].active = false;
                }
                // Blast is a line 3 pixels wide
                lcd.setPixel(enemy_blasts[i].pos.x,   enemy_blasts[i].pos.y, 1);
                lcd.setPixel(enemy_blasts[i].pos.x-1, enemy_blasts[i].pos.y, 1);
                lcd.setPixel(enemy_blasts[i].pos.x-2, enemy_blasts[i].pos.y, 1);
            }
        }
    }  
};

#endif