Commit 51ec8ab5 authored by Max Kobelt's avatar Max Kobelt
Browse files

Add random shooting (without projectiles) to aliens

parent a228c66a
......@@ -15,12 +15,14 @@ Aliens *spawnAliens() {
aliens->pos = (Vector2) { 0, 0 };
aliens->movingRight = true;
aliens->velocity = LASER_CANON_SPEED;
aliens->lastShots = calloc(ALIEN_COLS, sizeof *aliens->lastShots);
bool **instances = malloc(sizeof *instances * ALIEN_COLS);
Alien ***instances = malloc(sizeof *instances * ALIEN_COLS);
for (int j = 0; j < ALIEN_COLS; ++j) {
instances[j] = malloc(sizeof **instances * ALIEN_ROWS);
for (int i = 0; i < ALIEN_ROWS; ++i) {
instances[j][i] = true;
instances[j][i] = malloc(sizeof ***instances);
instances[j][i]->shooting = false;
}
}
......@@ -31,12 +33,23 @@ Aliens *spawnAliens() {
void drawAliens(Aliens *aliens) {
for (int j = 0; j < ALIEN_COLS; ++j) {
for (int i = 0; i < ALIEN_ROWS; ++i) {
if (aliens->instances[j][i]) {
bool up = (i + j) % 2 == (FRAMES_COUNT / FRAME_RATE) % 2;
DrawTexture(up ? ALIEN_UP : ALIEN_DOWN, aliens->pos.x + j * (ALIEN_SIZE + ALIEN_OFFSET), aliens->pos.y + i * (ALIEN_SIZE + ALIEN_OFFSET), WHITE);
if (aliens->instances[j]) {
for (int i = 0; i < ALIEN_ROWS; ++i) {
Alien *alien = aliens->instances[j][i];
if (alien) {
Texture2D *texture = NULL;
if (alien->shooting) {
texture = &ALIEN_SHOOT;
} else {
bool up = (i + j) % 2 == (FRAMES_COUNT / FRAME_RATE) % 2;
texture = up ? &ALIEN_UP : &ALIEN_DOWN;
}
DrawTexture(*texture, aliens->pos.x + j * (ALIEN_SIZE + ALIEN_OFFSET), aliens->pos.y + i * (ALIEN_SIZE + ALIEN_OFFSET), WHITE);
}
}
}
}
}
......@@ -51,12 +64,33 @@ void aliensTick(Aliens *aliens) {
}
aliens->pos.x += LASER_CANON_SPEED * (aliens->movingRight ? 1 : -1);
for (int j = 0; j < ALIEN_COLS; ++j) {
if (aliens->instances[j]) {
Alien* lowest = aliens->instances[j][ALIEN_ROWS - 1];
while (!lowest) --lowest;
if (lowest->shooting && (aliens->lastShots[j] / (FRAME_RATE / 2)) % 2) { // Close mouth after projectile has been launched
lowest->shooting = false;
} else if (!lowest->shooting && (aliens->lastShots[j] / (FRAME_RATE * 2)) % 2 && rand() <= RAND_MAX * 0.005f) { // Chance to shoot a projectile
lowest->shooting = true;
aliens->lastShots[j] = 0;
}
++aliens->lastShots[j];
}
}
}
void destructAliens(Aliens *aliens) {
for (int j = 0; j < ALIEN_COLS; ++j) {
for (int i = 0; i < ALIEN_ROWS; ++i) {
free(aliens->instances[j][i]);
}
free(aliens->instances[j]);
}
free(aliens->instances);
free(aliens->lastShots);
free(aliens);
}
......@@ -8,13 +8,16 @@
#define ALIEN_ROWS 3
#define ALIEN_COLS 5
typedef Vector2 Alien;
typedef struct {
bool shooting;
} Alien;
typedef struct {
bool **instances;
Alien ***instances;
Vector2 pos;
bool movingRight;
size_t velocity;
int velocity;
int *lastShots;
} Aliens;
Aliens *spawnAliens();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment