Commit bf76a842 authored by Max Kobelt's avatar Max Kobelt
Browse files

Aliens shoot projectiles

parent 1860090d
...@@ -17,6 +17,10 @@ Aliens *spawnAliens() { ...@@ -17,6 +17,10 @@ Aliens *spawnAliens() {
aliens->velocity = LASER_CANON_SPEED; aliens->velocity = LASER_CANON_SPEED;
aliens->lastShots = calloc(ALIEN_COLS, sizeof *aliens->lastShots); aliens->lastShots = calloc(ALIEN_COLS, sizeof *aliens->lastShots);
for (int i = 0; i < ALIEN_MAX_PROJECTILES; ++i) {
aliens->projectiles[i] = NULL;
}
Alien ***instances = malloc(sizeof *instances * ALIEN_COLS); Alien ***instances = malloc(sizeof *instances * ALIEN_COLS);
for (int j = 0; j < ALIEN_COLS; ++j) { for (int j = 0; j < ALIEN_COLS; ++j) {
instances[j] = malloc(sizeof **instances * ALIEN_ROWS); instances[j] = malloc(sizeof **instances * ALIEN_ROWS);
...@@ -52,9 +56,21 @@ void drawAliens(Aliens *aliens) { ...@@ -52,9 +56,21 @@ void drawAliens(Aliens *aliens) {
} }
} }
} }
for (int i = 0; i < ALIEN_MAX_PROJECTILES; ++i) {
drawProjectile(aliens->projectiles[i]);
}
} }
void aliensTick(Aliens *aliens) { void aliensTick(Aliens *aliens) {
for (int i = 0; i < ALIEN_MAX_PROJECTILES; ++i) {
Projectile *proj = aliens->projectiles[i];
if (proj) {
aliens->projectiles[i] = projectileTick(proj);
}
}
int outerCol = aliens->movingRight ? ALIEN_COLS - 1 : 0; int outerCol = aliens->movingRight ? ALIEN_COLS - 1 : 0;
while (!aliens->instances[outerCol + (aliens->movingRight ? -1 : 1)]) { while (!aliens->instances[outerCol + (aliens->movingRight ? -1 : 1)]) {
aliens->movingRight ? --outerCol : ++outerCol; aliens->movingRight ? --outerCol : ++outerCol;
...@@ -69,14 +85,29 @@ void aliensTick(Aliens *aliens) { ...@@ -69,14 +85,29 @@ void aliensTick(Aliens *aliens) {
for (int j = 0; j < ALIEN_COLS; ++j) { for (int j = 0; j < ALIEN_COLS; ++j) {
if (aliens->instances[j]) { if (aliens->instances[j]) {
Alien* lowest = aliens->instances[j][ALIEN_ROWS - 1]; int lowestI = ALIEN_ROWS - 1;
while (!lowest) --lowest; Alien* lowest = aliens->instances[j][lowestI];
while (!lowest && --lowestI) --lowest; // Side effect: lowestI won't get decremented if lowest is truthy
if (lowest->shooting && (aliens->lastShots[j] / (FRAME_RATE / 2)) % 2) { // Close mouth after projectile has been launched if (lowest->shooting && (aliens->lastShots[j] / (FRAME_RATE / 2)) % 2) { // Close mouth after projectile has been launched
lowest->shooting = false; lowest->shooting = false;
} else if (!lowest->shooting && (aliens->lastShots[j] / (FRAME_RATE * 2)) % 2 && rand() <= RAND_MAX * 0.005f) { // Chance to shoot a projectile } 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; for (int i = 0; i < ALIEN_MAX_PROJECTILES; ++i) {
if (!aliens->projectiles[i]) {
lowest->shooting = true;
aliens->lastShots[j] = 0;
Projectile *proj = malloc(sizeof *proj);
proj->movingUp = false;
proj->pos = (Vector2) { aliens->pos.x + j * (ALIEN_SIZE + ALIEN_OFFSET) + (ALIEN_SIZE - PROJECTILE_WIDTH) / 2, aliens->pos.y + lowestI * (ALIEN_SIZE + ALIEN_OFFSET) + ALIEN_SIZE };
aliens->projectiles[i] = proj;
break;
}
}
} }
++aliens->lastShots[j]; ++aliens->lastShots[j];
......
#pragma once #pragma once
#include "projectile.h"
#include "raylib.h" #include "raylib.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#define ALIEN_ROWS 3 #define ALIEN_ROWS 3
#define ALIEN_COLS 5 #define ALIEN_COLS 5
#define ALIEN_MAX_PROJECTILES 3
typedef struct { typedef struct {
bool shooting; bool shooting;
...@@ -15,6 +17,7 @@ typedef struct { ...@@ -15,6 +17,7 @@ typedef struct {
typedef struct { typedef struct {
Alien ***instances; Alien ***instances;
Projectile *projectiles[ALIEN_MAX_PROJECTILES];
Vector2 pos; Vector2 pos;
bool movingRight; bool movingRight;
int velocity; int velocity;
......
...@@ -55,9 +55,9 @@ void calcGame() { ...@@ -55,9 +55,9 @@ void calcGame() {
if (aliens->instances[j][i]->alive) { if (aliens->instances[j][i]->alive) {
aliens->instances[j][i]->alive = false; aliens->instances[j][i]->alive = false;
return; return;
} }
} }
} }
} }
laserCanon.x = laserCanonPos.x; laserCanon.x = laserCanonPos.x;
...@@ -70,9 +70,7 @@ void drawGame() { ...@@ -70,9 +70,7 @@ void drawGame() {
DrawTexture(CANON_TEXTURE, (int) laserCanonPos.x, (int) laserCanonPos.y, WHITE); DrawTexture(CANON_TEXTURE, (int) laserCanonPos.x, (int) laserCanonPos.y, WHITE);
DrawRectangleLines((int) gameBorder.x, (int) gameBorder.y, (int) gameBorder.width, (int) gameBorder.height, WHITE); DrawRectangleLines((int) gameBorder.x, (int) gameBorder.y, (int) gameBorder.width, (int) gameBorder.height, WHITE);
if (playerProj) { drawProjectile(playerProj);
DrawRectangleRec((Rectangle) { playerProj->pos.x, playerProj->pos.y, PROJECTILE_WIDTH, PROJECTILE_HEIGHT }, WHITE);
}
drawAliens(aliens); drawAliens(aliens);
} }
......
...@@ -11,3 +11,9 @@ Projectile* projectileTick(Projectile *proj) { ...@@ -11,3 +11,9 @@ Projectile* projectileTick(Projectile *proj) {
} }
proj->pos.y = newY; proj->pos.y = newY;
} }
void drawProjectile(Projectile *proj) {
if (!proj) return;
DrawRectangleRec((Rectangle) { proj->pos.x, proj->pos.y, PROJECTILE_WIDTH, PROJECTILE_HEIGHT }, WHITE);
}
...@@ -16,3 +16,4 @@ typedef struct { ...@@ -16,3 +16,4 @@ typedef struct {
} Projectile; } Projectile;
Projectile* projectileTick(Projectile *proj); Projectile* projectileTick(Projectile *proj);
void drawProjectile(Projectile *proj);
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