aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-04 14:21:23 +1000
committerquou <quou@disroot.org>2023-05-04 14:21:23 +1000
commit7fa5b3c842c276859c7d4016b9660e1b16ac30c5 (patch)
treef9290a27a7385962b878be19a2647a792ec0dd68
parentd61dcdcc384249ec7ea60c9cc18aab9df1f80577 (diff)
Add entity destruction.
-rw-r--r--bullet.c4
-rw-r--r--world.c12
-rw-r--r--world.h3
3 files changed, 18 insertions, 1 deletions
diff --git a/bullet.c b/bullet.c
index f2e1a2e..9acefc2 100644
--- a/bullet.c
+++ b/bullet.c
@@ -75,6 +75,10 @@ void bullet_system(World* world) {
pos->x += bullet->vx;
pos->y += bullet->vy;
bullet->life--;
+
+ if (bullet->life <= 0) {
+ destroy_entity(world, i);
+ }
}
}
}
diff --git a/world.c b/world.c
index b271c3d..3b736e6 100644
--- a/world.c
+++ b/world.c
@@ -7,11 +7,21 @@ void init_world(World* world) {
Entity new_entity(World* world) {
Entity e;
- e = world->entity_count++;
+ e =
+ world->recycle_bin_count > 0 ?
+ world->recycle_bin[--world->recycle_bin_count] :
+ world->entity_count++;
+
world->bitmask[e] = 0;
+
return e;
}
+void destroy_entity(World* world, Entity e) {
+ world->recycle_bin[world->recycle_bin_count++] = e;
+ world->bitmask[e] = 0;
+}
+
void add_components(World* world, Entity e, CType bits) {
world->bitmask[e] |= bits;
}
diff --git a/world.h b/world.h
index 3127f37..d9995ab 100644
--- a/world.h
+++ b/world.h
@@ -11,8 +11,10 @@ typedef struct World World;
struct World {
int entity_count;
+ int recycle_bin_count;
unsigned char bitmask[max_entities];
+ Entity recycle_bin[max_entities];
CSprite sprites [max_entities];
CPosition positions [max_entities];
@@ -24,6 +26,7 @@ struct World {
void init_world(World* world);
Entity new_entity(World* world);
+void destroy_entity(World* world, Entity e);
void add_components(World* world, Entity e, CType bits);
void remove_components(World* world, Entity e, CType bits);