Separate entity lists

This commit is contained in:
Bill Rossi 2025-03-01 11:19:00 -05:00
parent 61e89820f4
commit 8622d86c2f
2 changed files with 34 additions and 18 deletions

49
game.c
View File

@ -15,42 +15,53 @@
#include "enemy.h" #include "enemy.h"
#include "bullet.h" #include "bullet.h"
#define FOREACH_ENTITY for (size_t i = 0; i < g->entities->count; i++) #define FOREACH_ENEMY for (size_t i = 0; i < g->enemies->count; i++)
#define INIT_ENTITY Entity *e = g->entities->entities[i] #define INIT_ENEMY Entity *e = g->enemies->entities[i]
#define FOREACH_BULLET for (size_t i = 0; i < g->bullets->count; i++)
#define INIT_BULLET Entity *e = g->bullets->entities[i]
void initialize_game(Game *g) { void initialize_game(Game *g) {
g->should_close = false; g->should_close = false;
g->player = malloc(sizeof(Player)); g->player = malloc(sizeof(Player));
initialize_player(g->player); initialize_player(g->player);
g->entities = malloc(sizeof(Entities)); g->enemies = malloc(sizeof(Entities));
g->entities->entities = malloc(10 * sizeof(Entity*)); g->enemies->entities = malloc(10 * sizeof(Entity*));
g->entities->count = 0; g->enemies->count = 0;
g->entities->capacity = 10; g->enemies->capacity = 10;
g->bullets = malloc(sizeof(Entities));
g->bullets->entities = malloc(10 * sizeof(Entity*));
g->bullets->count = 0;
g->bullets->capacity = 10;
} }
void add_entity(Game *g, Entity *e) { void add_entity(Entities *entities, Entity *e) {
if (g->entities->count >= g->entities->capacity) { if (entities->count >= entities->capacity) {
g->entities->capacity *= 2; entities->capacity *= 2;
g->entities->entities = realloc(g->entities->entities, g->entities->capacity * sizeof(Entity*)); entities->entities = realloc(entities->entities, entities->capacity * sizeof(Entity*));
} }
g->entities->entities[g->entities->count++] = e; entities->entities[entities->count++] = e;
} }
void handle_input(Game *g) { void handle_input(Game *g) {
handle_player_input(g->player); handle_player_input(g->player);
if (IsKeyPressed(KEY_S)) { if (IsKeyPressed(KEY_S)) {
add_entity(g, spawn_enemy()); add_entity(g->enemies, spawn_enemy());
} }
if (IsKeyPressed(KEY_SPACE)) { if (IsKeyPressed(KEY_SPACE)) {
add_entity(g, spawn_bullet(g->player->position)); add_entity(g->bullets, spawn_bullet(g->player->position));
} }
} }
void run_frame(Game *g) { void run_frame(Game *g) {
handle_input(g); handle_input(g);
move_player(g->player); move_player(g->player);
FOREACH_ENTITY { FOREACH_ENEMY {
INIT_ENTITY; INIT_ENEMY;
e->tick(e, GetFrameTime());
}
FOREACH_BULLET {
INIT_BULLET;
e->tick(e, GetFrameTime()); e->tick(e, GetFrameTime());
} }
} }
@ -59,8 +70,12 @@ void draw_frame(Game *g) {
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
draw_player(g->player); draw_player(g->player);
FOREACH_ENTITY { FOREACH_ENEMY {
INIT_ENTITY; INIT_ENEMY;
e->draw(e);
}
FOREACH_BULLET {
INIT_BULLET;
e->draw(e); e->draw(e);
} }
EndDrawing(); EndDrawing();

3
game.h
View File

@ -17,7 +17,8 @@
typedef struct Game { typedef struct Game {
Player *player; Player *player;
Entities *entities; Entities *enemies;
Entities *bullets;
/* /*
Levels levels; Levels levels;
*/ */