Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
enemies.h
00001 #ifndef ENEMIES_H 00002 #define ENEMIES_H 00003 00004 /** 00005 * Enemy Class 00006 * @brief A class to describe the states of an enemy ship. 00007 * @author Dmitrijs Griskovs 00008 * @date 15/04/2019 00009 */ 00010 class Enemy : public GameObject { 00011 public: 00012 Enemy () { 00013 /** A bool that changes states between whehter to go up or down in y-direction.*/ 00014 switch_enemy_y_dir = false; 00015 enemy_speed = 1; 00016 } 00017 /** 00018 * @brief Enemy spawn function. 00019 * @details This function spawns an enemy on the right side of the screen 00020 * and at random position in the y-direction. 00021 */ 00022 void spawn() { 00023 Point spawn_pos(screen_width, game_area_y + rand() % (game_area_height - enemy2_height)); 00024 GameObject::spawn(spawn_pos); 00025 dead = false; 00026 dead_counter = 0; 00027 blast_countdown = 0; 00028 } 00029 /** 00030 * @brief enemy death sound and status reset. 00031 * @details Marks enemy as dead, so it can display explosion anation. 00032 */ 00033 void die() { 00034 gamepad.tone(123,0.3); 00035 dead = true; 00036 dead_counter = 3; 00037 } 00038 00039 /** 00040 * @brief Draws and updates enemy sprites on the screen and substracts the score 00041 * if the ship leaves the screen limits. 00042 * 00043 * @details The function draws this enemy ship on the screen, and 00044 * changes their x and y positions each time the function is called. 00045 * If ship is dead then it draws enemy explosion animation and then disables it. 00046 * Disables the ship when it leaves the screen limits and substracts 00047 * the in game score by -50. 00048 */ 00049 void updateAndDraw() { 00050 pos.x -= enemy_speed; 00051 if (GameGlobals::game_score >= 200){ 00052 if (!switch_enemy_y_dir){pos.y -= 1;} 00053 else if(switch_enemy_y_dir){pos.y += 1;} 00054 if (pos.y >= (game_area_height - enemy2_height)){ 00055 switch_enemy_y_dir = false; 00056 } 00057 else if (pos.y <= game_area_y){ 00058 switch_enemy_y_dir = true; 00059 } 00060 } 00061 if (!dead) { drawSprite(pos, enemy2_sprite);} 00062 else { updateAndDrawDeathExplosion();} 00063 00064 if (pos.x < 0) { 00065 GameGlobals::game_score -= 50; 00066 active = false; 00067 } 00068 } 00069 00070 /** 00071 * @var bool dead 00072 * @brief sets if to true when enemy is hit to draw explossion. 00073 */ 00074 bool dead; 00075 /** 00076 * @var int dead_counter 00077 * @brief For drawing small explosion animatioin. 00078 */ 00079 int dead_counter; 00080 /** 00081 * @var int blast_countdown 00082 * @brief A variable for delaying the enemy shots. 00083 */ 00084 int blast_countdown; 00085 /** 00086 * @var int enemy_speed 00087 * @brief How fast should an enemy move. 00088 */ 00089 int enemy_speed; 00090 private:/** 00091 * @brief Draws enemy explosion sprite 00092 * @details Draws the enemy ships explosion sprites when the player's 00093 * shot hits them or when the player collides with them. 00094 * the animation consists of two sprites "Exploded" and "Half exploded". 00095 */ 00096 void updateAndDrawDeathExplosion() { 00097 if (dead_counter > 0) { 00098 dead_counter--; 00099 if (dead_counter == 2){ 00100 drawSprite(pos, enemy2_half_exploded_sprite); 00101 } else if (dead_counter == 1){ 00102 drawSprite(pos, enemy2_exploded_sprite); 00103 } 00104 } else { 00105 active = false; 00106 } 00107 } 00108 00109 bool switch_enemy_y_dir; 00110 }; 00111 00112 00113 /** 00114 * Enemies Class 00115 * @brief The class manages all enemy ships. 00116 * @details Responsible for spawning new ships and holding and updating them 00117 * and controlling time intervals between enemy shots. 00118 * @author Dmitrijs Griskovs 00119 * @date 15/04/2019 00120 */ 00121 class Enemies { 00122 public: 00123 /** 00124 * @var static const int max_enemies 00125 * @brief Maximum enemies allowed on the screen 00126 */ 00127 static const int max_enemies = 4; 00128 /** 00129 * @var static const int max_enemy_blasts 00130 * @brief Maximum enemies' blasts allowed on the screen 00131 */ 00132 static const int max_enemy_blasts = max_enemies*2; 00133 00134 Enemy enemy_blasts[max_enemy_blasts]; 00135 Enemy enemies[max_enemies]; 00136 CircleBounds enemy_bounds; 00137 CircleBounds enemy_blast_bounds; 00138 00139 /** 00140 * Constructor 00141 * Sets values for the enemy's sprite body circle area, the blast circle 00142 * area and the circle radius for collsion callculations. 00143 */ 00144 Enemies () { 00145 enemy_bounds.center.x = 5; 00146 enemy_bounds.center.y = 3; 00147 enemy_bounds.radius = 6; 00148 00149 enemy_blast_bounds.center.x = 0; 00150 enemy_blast_bounds.center.y = 1; 00151 enemy_blast_bounds.radius = 1; 00152 00153 enemy_blast_speed = 2; 00154 } 00155 00156 /** 00157 * @brief This function spawns a new enemy on the screen. 00158 * @details It spawns a new enemy on the screen when there is a free space 00159 * in the enemy aray. 00160 */ 00161 void spawnNewEnemy() { 00162 int found = -1; 00163 for (int i = 0; i < max_enemies; ++i) { 00164 if (!enemies[i].active) { 00165 found = i; 00166 break; 00167 } 00168 } 00169 00170 if (found != -1) { 00171 enemies[found].spawn(); 00172 } 00173 } 00174 00175 /** 00176 * @brief Draws the ships and the blasts. 00177 * @details Updates and draws all active enemy ships and decides when they 00178 * should fire blasts. 00179 */ 00180 void updateAndDrawEnemies() { 00181 for (int i = 0; i < max_enemies; ++i) { 00182 if (enemies[i].active){ 00183 enemies[i].updateAndDraw(); 00184 00185 // Spawn blast on enemy if counter is ready 00186 enemies[i].blast_countdown -= 1; 00187 if (enemies[i].blast_countdown <= 0) { 00188 bool fired = fireEnemyBlast(enemies[i]); 00189 if (fired) { 00190 enemies[i].blast_countdown = 20; 00191 } 00192 } 00193 } 00194 } 00195 } 00196 00197 00198 /** 00199 * @brief Uodates and draws all active blasts. 00200 */ 00201 void updateAndDrawEnemyBlasts() { 00202 for (int i = 0; i < max_enemy_blasts; ++i) { 00203 if (enemy_blasts[i].active) { 00204 enemy_blasts[i].pos.x -= enemy_blast_speed; 00205 if (enemy_blasts[i].pos.x <= -1){ 00206 enemy_blasts[i].active = false; 00207 } 00208 drawSprite(enemy_blasts[i].pos, blast_sprite); 00209 } 00210 } 00211 } 00212 /** 00213 * @var int blast_speed 00214 * @brief ow fast should an enemy blast moves. 00215 */ 00216 int enemy_blast_speed; 00217 00218 private: 00219 /** 00220 * @brief Spawns a blast at the position of given enemy. 00221 * @param enemy (const Enemy&) 00222 */ 00223 bool fireEnemyBlast(const Enemy& enemy) { 00224 int found = -1; 00225 for (int i = 0; i < max_enemy_blasts; ++i) { 00226 if (!enemy_blasts[i].active) { 00227 found = i; 00228 break; 00229 } 00230 } 00231 if (found != -1) { 00232 enemy_blasts[found].active = true; 00233 enemy_blasts[found].pos.x = enemy.pos.x; 00234 enemy_blasts[found].pos.y = enemy.pos.y + enemy2_height / 2; 00235 gamepad.tone(500,0.1); 00236 return true; 00237 } 00238 return false; 00239 } 00240 }; 00241 00242 #endif
Generated on Wed Dec 20 2023 20:30:17 by
1.7.2