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
game/enemies.h
- Committer:
- Noximilien
- Date:
- 2019-03-31
- Revision:
- 23:240bc00ef25b
- Parent:
- 21:0eb394495b8a
- Child:
- 24:0570cb4b92d7
File content as of revision 23:240bc00ef25b:
#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:
/**@brief
* 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;
}
/**@brief
* 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() {
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:
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 = 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;
gamepad.tone(500,0.1);
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);
}
}
}
};
#endif
