Dmitrijs Griskovs / Mbed 2 deprecated el17dg

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers boss.h Source File

boss.h

00001 #ifndef BOSS_H
00002 #define BOSS_H
00003 
00004 #include "constants.h"
00005 
00006 /** 
00007 * Boss Class
00008 * @brief A class to describe the states of the boss ship.
00009 * @author Dmitrijs Griskovs
00010 * @date 30/04/2019
00011 */
00012 class Boss : public GameObject {
00013 public:
00014     /**
00015      * @var static const int max_boss_blasts variable;
00016      * @brief Sets the limit of maximum boss blasts on the screen.
00017      */
00018     static const int max_boss_blasts = 5;
00019 
00020 
00021     GameObject boss_blasts[max_boss_blasts];
00022     CircleBounds boss_bounds;
00023     CircleBounds boss_blast_bounds;
00024     
00025     /** 
00026      * Constructor 
00027      * Sets values for the boss' sprite body circle area, the blast circle 
00028      * area and the circle radius for collsion callculations. Also, resets the
00029      * cutscene.
00030      */ 
00031     Boss() {
00032         boss_bounds.center.x = 5;
00033         boss_bounds.center.y = 8;
00034         boss_bounds.radius = 10;
00035         
00036         boss_blast_bounds.center.x = 1;
00037         boss_blast_bounds.center.y = 0;
00038         boss_blast_bounds.radius = 1;
00039         
00040         switch_boss_y_dir = true;
00041         animation_counter = 0;
00042         resetCutscene();  
00043     }
00044     
00045     /** 
00046      * @brief Updates and draws the boss blasts accross the screen.
00047      */
00048      void updateAndDrawBossBlasts() {
00049         for (int i = 0; i < max_boss_blasts; ++i) {
00050             if (boss_blasts[i].active) {
00051                 boss_blasts[i].pos.x -= boss_blast_speed;
00052                 if (boss_blasts[i].pos.x <= 0){
00053                     boss_blasts[i].active = false;
00054                     continue;
00055                 }
00056                 drawSprite(boss_blasts[i].pos, blast_sprite);
00057             }
00058         }
00059     }
00060     /** 
00061      * @brief Updates and draws the boss.
00062      * @details this function is monitored in game.cpp and when the boss becomes
00063      * inactive, the gameplay would switch from boss to normal.
00064      * @returns bool active, when the boss is out of lives
00065      */
00066     bool updateAndDrawBoss(){
00067         if(switch_boss_y_dir) { pos.y += 1;}
00068         else                  { pos.y -= 1;}
00069         if (pos.y >= (game_area_height - enemy1_height)){ switch_boss_y_dir = false;}
00070         else if (pos.y <= game_area_y)                  { switch_boss_y_dir = true;}
00071         blast_countdown -= 1;
00072         if (blast_countdown <= 0) {
00073             fireNewBlast(pos.x, pos.y + 2);
00074             fireNewBlast(pos.x, pos.y + enemy1_height - 2);
00075             blast_countdown = 10;
00076         }
00077         if(!dead() && active){ draw();}
00078         else{
00079             GameGlobals::game_score += 300;
00080             GameGlobals::score_count_for_boss_mode = 0;
00081             updateAndDrawDeathExplosion();
00082             lcd.normalMode();
00083         }
00084         return active;
00085     }
00086     /** 
00087      * @brief Updates and draws the boss' cutscene of entering the game.
00088      * @details It freezes the screen until the boss is set on the screen. Also,
00089      * it sets its position and number of lives.
00090      * @returns bool active, when the boss is out of lives
00091      */
00092     void updateCutscene() {
00093         if (!started_cutscene) {
00094             started_cutscene = true;
00095             pos.y = screen_height/2 - (enemy1_height/2);
00096             pos.x = screen_width;
00097             animation_counter = 0;
00098             active = true;
00099             boss_lives = 10;
00100             dead_counter = 4;
00101             #ifdef DEBUGel17dg
00102             printf("boss lives set to: %i \n", boss_lives);
00103             #endif
00104             return;
00105         }
00106         if (animation_counter < animation_length) {
00107             pos.x -= 1;
00108             animation_counter++;
00109         }    
00110     }
00111     
00112     /** @brief draws boss' sprite.*/
00113     void draw() {  drawSpriteOnTop(pos, enemy1_sprite);}
00114     /** @brief resets the boss' cutscene.*/
00115     void resetCutscene() { started_cutscene = false; }
00116     /** 
00117      * @returns bool true 
00118      * @brief It starts the boss fight sequence when the cutscene is finished.
00119      */
00120     bool isFinishedCutscene() { return animation_counter >= animation_length; }
00121     /**
00122      * @var int boss_lives 
00123      * @brief contains boss' lives.
00124      */
00125     int boss_lives;
00126     
00127 private:
00128     void updateAndDrawDeathExplosion() {
00129         for(int dead_counter; dead_counter >= 0; dead_counter--){
00130             if (dead_counter > 0) {
00131                 if (dead_counter == 4){    
00132                     drawSpriteOnTop(pos, enemy1_quarter_exploded_sprite);
00133                 } else if (dead_counter == 3){
00134                     drawSpriteOnTop(pos, enemy1_half_exploded_sprite);
00135                 } else if (dead_counter == 2){
00136                     drawSpriteOnTop(pos, enemy1_second_quarter_exploded_sprite);
00137                 } else if (dead_counter == 1){
00138                     drawSpriteOnTop(pos, enemy1_fully_exploded_sprite);
00139                 }
00140             } else {
00141                 active = false;
00142             }
00143         }
00144     } 
00145 
00146     /** 
00147      * @brief Spawns a blast at the position of the boss.
00148      * @param x (int x) to give x position of the boss blast.
00149      * @param y (int y) to give y position of the boss blast.
00150      * @details For this function the parameters are required to be able to spawn
00151      * two independed from each other blasts at the same time and different
00152      * positions.
00153      */
00154     bool fireNewBlast(int x, int y) {
00155         // Search the array of blasts if inactive we can use it.
00156         int found = -1;
00157         for (int i = 0; i < max_boss_blasts; ++i) {
00158             if (!boss_blasts[i].active) {
00159                 found = i;
00160                 break;
00161             }
00162         }   
00163         if (found != -1) {
00164             boss_blasts[found].active = true;
00165             boss_blasts[found].pos.x = x;
00166             boss_blasts[found].pos.y = y;
00167             gamepad.tone(500,0.1);
00168             return true;
00169         }
00170         return false;
00171     }
00172     static const int boss_y_speed = 2;
00173     static const int boss_blast_speed = 4;
00174     static const int animation_length = 20;
00175     bool dead(){return boss_lives == 0;}
00176     bool started_cutscene;
00177     bool switch_boss_y_dir;
00178     int blast_countdown;
00179     int animation_counter;
00180     int dead_counter;
00181 };
00182 
00183 #endif