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:
- 21:0eb394495b8a
- Child:
- 23:240bc00ef25b
diff -r 557e84189a57 -r 0eb394495b8a game/enemies.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/game/enemies.h Wed Mar 27 00:00:32 2019 +0000
@@ -0,0 +1,139 @@
+#ifndef ENEMIES_H
+#define ENEMIES_H
+
+#include "constants.h"
+#include "geometry.h"
+
+const int max_enemies = 1;
+const int enemy_speed = 1;
+const int max_enemy_blasts = max_enemies * 5;
+const int enemy_blast_speed = 5;
+
+class Enemy : public GameObject {
+public:
+ void spawn() {
+ 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;
+ }
+
+ void die() {
+ dead = true;
+ dead_counter = 3;
+ }
+
+ void updateAndDraw() {
+ pos.x -= enemy_speed;
+ if (!dead) {
+ drawSprite(pos, enemy_sprite);
+ } else {
+ updateAndDrawDeathExplosion();
+ }
+ if (pos.x < 0) {
+ active = false;
+ }
+ }
+
+ 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;
+};
+
+class Enemies {
+public:
+ Enemies () {
+ enemy_bounds.center.x = 5;
+ enemy_bounds.center.y = 3;
+ enemy_bounds.radius = 8;
+
+ enemy_blast_bounds.center.x = 0;
+ enemy_blast_bounds.center.y = 1;
+ enemy_blast_bounds.radius = 1;
+ }
+
+ 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();
+ }
+ }
+
+ 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 = 10;
+ }
+ }
+ }
+ }
+ }
+
+ 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;
+ return true;
+ }
+ return false;
+ }
+
+ 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);
+ }
+ }
+ }
+
+
+ GameObject enemy_blasts[max_enemy_blasts];
+ Enemy enemies[max_enemies];
+ CircleBounds enemy_bounds;
+ CircleBounds enemy_blast_bounds;
+};
+
+#endif
\ No newline at end of file
