From f501cdc0db1cd3351d1186668b5545d655fb54ca Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 2 Mar 2025 18:58:52 -0500 Subject: [PATCH] Enemies fire at you --- Makefile | 6 ++++++ bullet.c | 9 +++++---- bullet.h | 6 ++++-- enemy.c | 14 +++++++++++++- enemy.h | 5 ++++- entity.h | 4 +++- game.c | 26 +++++++++++++++++--------- game.h | 7 +++++-- player.h | 2 ++ 9 files changed, 59 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index cd5fc4b..c805d89 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,15 @@ ITCH_CHANNEL_MAC=mac run: $(GAME)_linux SKIP_INTRO=1 ./$(GAME)_linux +debug: $(GAME)_linux_dbg + SKIP_INTRO=1 gdb ./$(GAME)_linux + $(GAME)_linux: *.c $(CC) *.c $(CFLAGS) -o $(GAME)_linux +$(GAME)_linux_dbg: *.c + $(CC) *.c -g $(CFLAGS) -o $(GAME)_linux + $(GAME)_windows.exe: *.c $(WIN_CC) *.c $(WIN_LIB) $(WIN_CFLAGS) -o $(GAME)_windows.exe diff --git a/bullet.c b/bullet.c index 3856607..563a563 100644 --- a/bullet.c +++ b/bullet.c @@ -14,7 +14,7 @@ #include "bullet.h" #include "entity.h" -Entity *spawn_bullet(Vector2 spawn_at) { +Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy) { Entity *e = malloc(sizeof(Entity)); e->name = "Bullet"; e->properties = malloc(sizeof(Entity)); @@ -25,7 +25,8 @@ Entity *spawn_bullet(Vector2 spawn_at) { props->position.width = 4; props->position.height = 10; props->velocity.x = 0; - props->velocity.y = -10; + props->velocity.y = from_enemy ? 10 : -10; + props->from_enemy = from_enemy; e->draw = &draw_bullet; e->tick = &tick_bullet; e->free = &free_bullet; @@ -34,10 +35,10 @@ Entity *spawn_bullet(Vector2 spawn_at) { void draw_bullet(Entity *e) { BulletProperties *props = e->properties; - DrawRectangleRec(props->position, BLACK); + DrawRectangleRec(props->position, props->from_enemy ? RED : BLACK); } -void tick_bullet(Entity *e, float dt) { +void tick_bullet(Game *g, Entity *e, float dt) { BulletProperties *props = e->properties; e->timer -= dt; props->position.y += props->velocity.y; diff --git a/bullet.h b/bullet.h index 36fddad..e4834b9 100644 --- a/bullet.h +++ b/bullet.h @@ -12,15 +12,17 @@ #include #include "entity.h" +#include "game.h" typedef struct BulletProperties { Rectangle position; Vector2 velocity; + bool from_enemy; } BulletProperties; -Entity *spawn_bullet(Vector2 spawn_at); +Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy); void draw_bullet(Entity *e); -void tick_bullet(Entity *e, float dt); +void tick_bullet(Game *g, Entity *e, float dt); void free_bullet(Entity *e); #endif diff --git a/enemy.c b/enemy.c index 813ae75..4af65a2 100644 --- a/enemy.c +++ b/enemy.c @@ -26,6 +26,7 @@ Entity *spawn_enemy(void) { props->position.height = 25; props->velocity.x = 3; props->velocity.y = 0; + props->bullet_timer = rand() % 4; e->draw = &draw_enemy; e->tick = &tick_enemy; e->free = &free_enemy; @@ -37,7 +38,7 @@ void draw_enemy(Entity *e) { DrawRectangleRec(props->position, RED); } -void tick_enemy(Entity *e, float dt) { +void update_position_enemy(Entity *e, float dt) { EnemyProperties *props = e->properties; if (props->position.x < 200) props->velocity.x = 3; if (props->position.x > 600) props->velocity.x = -3; @@ -45,6 +46,17 @@ void tick_enemy(Entity *e, float dt) { props->position.x += props->velocity.x; } +void tick_enemy(Game *g, Entity *e, float dt) { + EnemyProperties *props = e->properties; + update_position_enemy(e, dt); + props->bullet_timer -= dt; + if (props->bullet_timer < 0.) { + props->bullet_timer = (float)(rand() % 4); + + add_entity(g->bullets, spawn_bullet((Vector2) { props->position.x, props->position.y }, true)); + } +} + void free_enemy(Entity *e) { free(e->properties); free(e); diff --git a/enemy.h b/enemy.h index d74f35d..a730922 100644 --- a/enemy.h +++ b/enemy.h @@ -12,15 +12,18 @@ #include #include "entity.h" +#include "bullet.h" +#include "game.h" typedef struct EnemyProperties { Rectangle position; Vector2 velocity; + float bullet_timer; } EnemyProperties; Entity *spawn_enemy(void); void draw_enemy(Entity *e); -void tick_enemy(Entity *e, float dt); +void tick_enemy(Game *g, Entity *e, float dt); void free_enemy(Entity *e); #endif diff --git a/entity.h b/entity.h index 982145f..f1e9e0d 100644 --- a/entity.h +++ b/entity.h @@ -17,12 +17,14 @@ typedef struct Entity Entity; typedef struct Entities Entities; +#include "game.h" + struct Entity { char *name; void *properties; float timer; void (*draw)(Entity *e); - void (*tick)(Entity *e, float dt); + void (*tick)(Game *g, Entity *e, float dt); void (*free)(Entity *e); }; diff --git a/game.c b/game.c index e390f03..8d25ab6 100644 --- a/game.c +++ b/game.c @@ -63,18 +63,25 @@ void handle_input(Game *g) { add_entity(g->enemies, spawn_enemy()); } if (IsKeyPressed(KEY_SPACE)) { - add_entity(g->bullets, spawn_bullet(g->player->position)); + add_entity(g->bullets, spawn_bullet(g->player->position, false)); } } void process_bullet_collisions(Game *g, Entity *bullet) { - FOREACH_ENEMY { - INIT_ENEMY; - Rectangle bullet_rec = ((BulletProperties*)bullet->properties)->position; - Rectangle enemy_rec = ((EnemyProperties*)e->properties)->position; - if (CheckCollisionRecs(bullet_rec, enemy_rec)) { + BulletProperties *bullet_props = bullet->properties; + Rectangle bullet_rec = bullet_props->position; + if (bullet_props->from_enemy) { + if (CheckCollisionPointRec(g->player->position, bullet_rec)) { remove_entity(g->bullets, bullet); - remove_entity(g->enemies, e); + } + } else { + FOREACH_ENEMY { + INIT_ENEMY; + Rectangle enemy_rec = ((EnemyProperties*)e->properties)->position; + if (CheckCollisionRecs(bullet_rec, enemy_rec)) { + remove_entity(g->bullets, bullet); + remove_entity(g->enemies, e); + } } } } @@ -88,16 +95,17 @@ void run_frame(Game *g) { remove_entity(g->enemies, e); continue; } - e->tick(e, GetFrameTime()); + e->tick(g, e, GetFrameTime()); } FOREACH_BULLET { INIT_BULLET; + if (entity_expired(e)) { remove_entity(g->bullets, e); continue; } - e->tick(e, GetFrameTime()); + e->tick(g, e, GetFrameTime()); process_bullet_collisions(g, e); } } diff --git a/game.h b/game.h index a153a46..b4b9806 100644 --- a/game.h +++ b/game.h @@ -11,11 +11,13 @@ #ifndef GAME_H #define GAME_H +typedef struct Game Game; + #include #include "player.h" #include "entity.h" -typedef struct Game { +struct Game { Player *player; Entities *enemies; Entities *bullets; @@ -23,9 +25,10 @@ typedef struct Game { Levels levels; */ bool should_close; -} Game; +}; void initialize_game(Game *g); void run_until_closing(Game *g); +void add_entity(Entities *entities, Entity *e); #endif diff --git a/player.h b/player.h index ddeac68..b09141c 100644 --- a/player.h +++ b/player.h @@ -10,6 +10,8 @@ #ifndef PLAYER_H #define PLAYER_H +#include + #define ACCEL_X 0.7 #define MAX_VEL_X 5 #define DAMPING_X 0.85