Initial publish
Dependencies: mbed
Fork of el17dg by
game/enemies.h
- Committer:
- Noximilien
- Date:
- 2019-04-08
- Revision:
- 27:f05f4e738ba9
- Parent:
- 26:676874c42883
- Child:
- 28:35af3843de8f
File content as of revision 27:f05f4e738ba9:
#ifndef ENEMIES_H #define ENEMIES_H #include "constants.h" #include "geometry.h" #include "game.h" extern int enemy_speed = 1; extern int enemy_blast_speed = 3; const int max_enemies = 4; const int max_enemy_blasts = max_enemies*2; /**@brief * This is a class to describe the states of one enemy ship */ class Enemy : public GameObject { public: /**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; } /** This is a death function of an nemy when the the collision between the * player blast and enemy ship is true. It sets the enemy ship to not active. */ void die() { gamepad.tone(87,0.1); gamepad.tone(187,0.2); gamepad.tone(123,0.3); dead = true; dead_counter = 3; } /** This function draws each individual enemy ship on the screen. */ void updateAndDraw() { pos.x -= enemy_speed; if (!dead) { drawSprite(pos, enemy_sprite); } else { updateAndDrawDeathExplosion(); } if (pos.x < 0) { game_score -= 50; score_count_for_difficulty -= 50; active = false; } } /** This is an explosion function that draws the enemy ships explosion sprites * when the player's shot hits it/ */ void updateAndDrawDeathExplosion() { if (dead_counter > 0) { dead_counter--; if (dead_counter == 2){ drawSprite(pos, enemy_half_exploded_sprite); } else if (dead_counter == 1){ drawSprite(pos, enemy_exploded_sprite); } } else { active = false; } } bool dead; int dead_counter; int blast_countdown; }; /**@brief * This class describes the states and actions of several enemy ships, including * random position generation (y - position) and enemy ship shooting drawing (also describing * intervals between shots). */ class Enemies { public: Enemy enemy_blasts[max_enemy_blasts]; Enemy enemies[max_enemies]; CircleBounds enemy_bounds; CircleBounds enemy_blast_bounds; 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; } /** This function spawns a new enemy when there is a free spance 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(); } } /** 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; } } } } } /** 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 */ 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; } /** 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