Move system initialization in systems.c, properly delete all entities when unloading map

This commit is contained in:
2023-12-29 17:18:06 +01:00
parent 5190c86316
commit cc66f15131
8 changed files with 84 additions and 82 deletions

View File

@@ -25,9 +25,6 @@ ECS_COMPONENT_DECLARE(InputState);
BzUI *UI = NULL;
ecs_world_t *ECS = NULL;
static ecs_entity_t renderCollidersSystem;
static ecs_entity_t renderDebugPathSystem;
bool init(void *userData);
void deinit(void *userData);
@@ -86,6 +83,7 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) {
}
void unloadMap(Game *game) {
ecs_delete_with(ECS, GameEntity);
if (game->map.isValid) {
bzTileMapDestroy(&game->map);
game->map.isValid = false;
@@ -134,21 +132,6 @@ void loadMap(Game *game, const char *path) {
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
}
ECS_DTOR(SpatialGridID, gridID, {
Game *game = ecs_singleton_get_mut(ECS, Game);
bzSpatialGridRemove(game->entityGrid, *gridID);
})
ECS_DTOR(Path, path, {
Game *game = ecs_singleton_get_mut(ECS, Game);
BzObjectPool *pool = game->pools.pathData;
PathData *cur = path[i].paths;
while (cur) {
bzObjectPoolRelease(pool, cur);
cur = cur->next;
}
})
int cmpDrawData(const void *a, const void *b) {
const DrawData *lhs = (DrawData *) a;
const DrawData *rhs = (DrawData *) b;
@@ -221,42 +204,9 @@ bool init(void *userData) {
.texturePath="assets/game.png"
});
ecs_set_hooks(ECS, SpatialGridID, {
.dtor = ecs_dtor(SpatialGridID)
});
ecs_set_hooks(ECS, Path, {
.dtor = ecs_dtor(Path)
});
//setupSystems(ECS)
ECS_OBSERVER(ECS, entityPathRemove, EcsOnRemove, Path);
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID);
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, Steering);
ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Rotation, Velocity, TargetPosition, Steering);
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, handleUnitActionsSystem, EcsOnUpdate, UnitAction);
ECS_SYSTEM(ECS, updateUnitAISystem, EcsOnUpdate, UnitAI, UnitAction);
// Needs to be called after AI update, since it removes finished actions
ECS_SYSTEM(ECS, updateUnitActionsSystem, EcsOnUpdate, UnitAction);
ECS_SYSTEM(ECS, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateAnimation, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateEasingSystem, EcsOnUpdate, Easing, Position, Size, Rotation);
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation);
setupSystems();
loadMap(game, "assets/maps/main_menu_01.tmj");
renderDebugPathSystem = renderDebugPath;
renderCollidersSystem = renderColliders;
game->debugDraw.mapColliders = true;
game->debugDraw.spatialGrid = true;
@@ -269,6 +219,8 @@ void deinit(void *userData) {
Game *game = ecs_singleton_get_mut(ECS, Game);
InputState *input = ecs_singleton_get_mut(ECS, InputState);
unloadMap(game);
// Destroy queries
ecs_query_fini(input->queries.selected);
ecs_query_fini(game->drawQuery);
@@ -283,7 +235,6 @@ void deinit(void *userData) {
game = &gameCopy;
input = &inputCopy;
unloadMap(game);
bzTilesetDestroy(&game->tileset);
bzStackAllocDestroy(&game->stackAlloc);
@@ -454,6 +405,7 @@ static void renderMainMenu(Game *game, float dt) {
});
if (uiMainMenuButton("Play") || true) {
game->screen = SCREEN_GAME;
unloadMap(game);
loadMap(game, "assets/maps/map_01.tmj");
}
if (uiMainMenuButton("Settings")) {