diff --git a/bullet.c b/bullet.c index b7893d9..3856607 100644 --- a/bullet.c +++ b/bullet.c @@ -18,6 +18,7 @@ Entity *spawn_bullet(Vector2 spawn_at) { Entity *e = malloc(sizeof(Entity)); e->name = "Bullet"; e->properties = malloc(sizeof(Entity)); + e->timer = 0.5; BulletProperties *props = (BulletProperties*)e->properties; props->position.x = spawn_at.x; props->position.y = spawn_at.y; @@ -38,6 +39,7 @@ void draw_bullet(Entity *e) { void tick_bullet(Entity *e, float dt) { BulletProperties *props = e->properties; + e->timer -= dt; props->position.y += props->velocity.y; } diff --git a/enemy.c b/enemy.c index 7e64330..813ae75 100644 --- a/enemy.c +++ b/enemy.c @@ -18,6 +18,7 @@ Entity *spawn_enemy(void) { Entity *e = malloc(sizeof(Entity)); e->name = "Enemy"; e->properties = malloc(sizeof(Entity)); + e->timer = 1.0; EnemyProperties *props = (EnemyProperties*)e->properties; props->position.x = 400; props->position.y = 50; diff --git a/entity.c b/entity.c new file mode 100644 index 0000000..515eb07 --- /dev/null +++ b/entity.c @@ -0,0 +1,15 @@ +// Copyright 2025 Bill Rossi +// +// This file is part of Starship Futuretime +// +// Starship Futuretime is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +// +// Starship Futuretime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see . + +#include "entity.h" + +bool entity_expired(Entity *e) { + return e->timer < 0; +} diff --git a/entity.h b/entity.h index 8cea678..982145f 100644 --- a/entity.h +++ b/entity.h @@ -11,12 +11,16 @@ #ifndef ENTITY_H #define ENTITY_H +#include +#include + typedef struct Entity Entity; typedef struct Entities Entities; struct Entity { char *name; void *properties; + float timer; void (*draw)(Entity *e); void (*tick)(Entity *e, float dt); void (*free)(Entity *e); @@ -28,4 +32,6 @@ struct Entities { size_t capacity; }; +bool entity_expired(Entity *e); + #endif diff --git a/game.c b/game.c index 15a2d2d..b0892a0 100644 --- a/game.c +++ b/game.c @@ -8,6 +8,7 @@ // // You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see . #include +#include #include #include @@ -42,6 +43,20 @@ void add_entity(Entities *entities, Entity *e) { entities->entities[entities->count++] = e; } +void remove_entity(Entities *entities, Entity *e) { + size_t i; + for (i = 0; entities->entities[i] != e; i++) { + if (i > entities->count) { + printf("Couldn't find entity %s\n", e->name); + return; + } + } + for (; i < entities->count - 1; i++) { + entities->entities[i] = entities->entities[i + 1]; + } + entities->count--; +} + void handle_input(Game *g) { handle_player_input(g->player); if (IsKeyPressed(KEY_S)) { @@ -57,11 +72,19 @@ void run_frame(Game *g) { move_player(g->player); FOREACH_ENEMY { INIT_ENEMY; + if (entity_expired(e)) { + remove_entity(g->enemies, e); + continue; + } e->tick(e, GetFrameTime()); } FOREACH_BULLET { INIT_BULLET; + if (entity_expired(e)) { + remove_entity(g->bullets, e); + continue; + } e->tick(e, GetFrameTime()); } }