From 7d4d948dfa92416c802229b1c444a44785ee2a0c Mon Sep 17 00:00:00 2001 From: nathansmithsmith Date: Tue, 12 Sep 2023 21:53:32 -0600 Subject: Guided missile worky worky --- src/world.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/world.c') diff --git a/src/world.c b/src/world.c index ebfc7ed..26ac9cd 100644 --- a/src/world.c +++ b/src/world.c @@ -13,6 +13,9 @@ void initWorld(World * world) { world->vacantIds = NULL; world->vacantIdsCount = 0; + world->entitiesToAdd = NULL; + world->entitiesToAddCount = 0; + // Set current fingerprint. SetRandomSeed(time(NULL)); @@ -227,6 +230,49 @@ KfError removeEntityFromWorld(World * world, EntityId id) { return KFSUCCESS; } +KfError scheduleEntityToAdd(World * world, Entity entity) { + ++world->entitiesToAddCount; + + // Allocate shit. + if (world->entitiesToAdd == NULL) + world->entitiesToAdd = (Entity*)KF_CALLOC(world->entitiesToAddCount, sizeof(Entity)); + else + world->entitiesToAdd = (Entity*)KF_REALLOCARRAY( + world->entitiesToAdd, + world->entitiesToAddCount, + sizeof(Entity) + ); + + if (world->entitiesToAdd == NULL) { + ALLOCATION_ERROR; + return KFERROR; + } + + world->entitiesToAdd[world->entitiesToAddCount - 1] = entity; + return KFSUCCESS; +} + +KfError handleScheduledEntities(World * world) { + int i; + + // No entities to add. + if (world->entitiesToAdd == NULL) + return KFSUCCESS; + + for (i = 0; i < world->entitiesToAddCount; ++i) { + // Add entity indeed. + if (addEntityToWorld(world, world->entitiesToAdd[i]) == ENTITY_NONE) + return KFERROR; + } + + // Clean up this shit. + KF_FREE(world->entitiesToAdd); + world->entitiesToAdd = NULL; + world->entitiesToAddCount = 0; + + return KFSUCCESS; +} + void handleCollisionInWorld(Entity * entity1, Entity * entity2) { //if (entity1->id != 0) // entity1->health = 0.0; @@ -303,6 +349,9 @@ void updateWorld(World * world, Game * game) { removeEntityFromWorld(world, kills[i]); } + + // Handle some shit. + handleScheduledEntities(world); } void drawWorld(World * world, Game * game) { -- cgit v1.2.3