From 8622d86c2f977c8786be08d1b555f68d93201d69 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 1 Mar 2025 11:19:00 -0500 Subject: [PATCH] Separate entity lists --- game.c | 49 ++++++++++++++++++++++++++++++++----------------- game.h | 3 ++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/game.c b/game.c index 9e1a210..15a2d2d 100644 --- a/game.c +++ b/game.c @@ -15,42 +15,53 @@ #include "enemy.h" #include "bullet.h" -#define FOREACH_ENTITY for (size_t i = 0; i < g->entities->count; i++) -#define INIT_ENTITY Entity *e = g->entities->entities[i] +#define FOREACH_ENEMY for (size_t i = 0; i < g->enemies->count; 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) { g->should_close = false; g->player = malloc(sizeof(Player)); initialize_player(g->player); - g->entities = malloc(sizeof(Entities)); - g->entities->entities = malloc(10 * sizeof(Entity*)); - g->entities->count = 0; - g->entities->capacity = 10; + g->enemies = malloc(sizeof(Entities)); + g->enemies->entities = malloc(10 * sizeof(Entity*)); + g->enemies->count = 0; + 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) { - if (g->entities->count >= g->entities->capacity) { - g->entities->capacity *= 2; - g->entities->entities = realloc(g->entities->entities, g->entities->capacity * sizeof(Entity*)); +void add_entity(Entities *entities, Entity *e) { + if (entities->count >= entities->capacity) { + entities->capacity *= 2; + 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) { handle_player_input(g->player); if (IsKeyPressed(KEY_S)) { - add_entity(g, spawn_enemy()); + add_entity(g->enemies, spawn_enemy()); } 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) { handle_input(g); move_player(g->player); - FOREACH_ENTITY { - INIT_ENTITY; + FOREACH_ENEMY { + INIT_ENEMY; + e->tick(e, GetFrameTime()); + } + + FOREACH_BULLET { + INIT_BULLET; e->tick(e, GetFrameTime()); } } @@ -59,8 +70,12 @@ void draw_frame(Game *g) { BeginDrawing(); ClearBackground(RAYWHITE); draw_player(g->player); - FOREACH_ENTITY { - INIT_ENTITY; + FOREACH_ENEMY { + INIT_ENEMY; + e->draw(e); + } + FOREACH_BULLET { + INIT_BULLET; e->draw(e); } EndDrawing(); diff --git a/game.h b/game.h index c3fb0e6..a153a46 100644 --- a/game.h +++ b/game.h @@ -17,7 +17,8 @@ typedef struct Game { Player *player; - Entities *entities; + Entities *enemies; + Entities *bullets; /* Levels levels; */