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.
Dependencies: mbed
Fork of el17dg by
Diff: game/enemies.h
- Revision:
- 31:becb8f6bf7b7
- Parent:
- 30:d454d0cb72bc
- Child:
- 32:5403bb974294
--- a/game/enemies.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/enemies.h Tue Apr 23 18:18:57 2019 +0000
@@ -16,17 +16,19 @@
/** 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.
+/**
+ * Enemy Class
+ * @brief A class to describe the states of an 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.
- */
+ /**
+ * @brief Enemy spawn function.
+ * @details This function spawns an enemy on the right side of the screen
+ * 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));
@@ -35,30 +37,29 @@
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.
- */
+ /**
+ * @brief enemy death sound and status reset.
+ * @details Marks enemy as dead, so it can display explosion anation.
+ */
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.
- */
+
+ /**
+ * @brief Draws and updates enemy sprites on the screen and substracts the score
+ * if the ship leaves the screen limits.
+ *
+ * @details The function draws this enemy ship on the screen, and
+ * changes their x and y positions each time the function is called.
+ * If ship is dead then it draws enemy explosion animation and then disables it.
+ * Disables the ship 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 (GameGlobals::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)){
@@ -72,16 +73,17 @@
else { updateAndDrawDeathExplosion();}
if (pos.x < 0) {
- game_score -= 50;
- score_count_for_difficulty -= 50;
+ GameGlobals::game_score -= 50;
+ GameGlobals::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".
- */
+ /**
+ * @brief Draws enemy explosion sprite
+ * @details 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--;
@@ -101,13 +103,14 @@
};
-/** 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
- */
+/**
+ * Enemies Class
+ * @brief The class manages all enemy ships.
+ * @details Responsible for spawning new ships and holding and updating them
+ * and controlling time intervals between enemy shots.
+ * @author Dmitrijs Griskovs
+ * @date 15/04/2019
+ */
class Enemies {
public:
Enemy enemy_blasts[max_enemy_blasts];
@@ -115,9 +118,10 @@
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.
- */
+ /**
+ * 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;
@@ -127,10 +131,12 @@
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.
- */
+
+ /**
+ * @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) {
@@ -144,10 +150,12 @@
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.
- */
+
+ /**
+ * @brief Draws the ships and the blasts.
+ * @details Updates and draws all active enemy ships and decides when they
+ * should fire blasts.
+ */
void updateAndDrawEnemies() {
for (int i = 0; i < max_enemies; ++i) {
if (enemies[i].active){
@@ -164,12 +172,11 @@
}
}
}
-/** @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&)
- */
+
+ /**
+ * @brief Spawns a blast at the position of given enemy.
+ * @param enemy (const Enemy&)
+ */
bool fireEnemyBlast(const Enemy& enemy) {
int found = -1;
for (int i = 0; i < max_enemy_blasts; ++i) {
@@ -187,11 +194,9 @@
}
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.
- */
+ /**
+ * @brief Moves and draws all active blasts.
+ */
void updateAndDrawEnemyBlasts() {
for (int i = 0; i < max_enemy_blasts; ++i) {
if (enemy_blasts[i].active) {
