Move system initialization in systems.c, properly delete all entities when unloading map
This commit is contained in:
@@ -15,6 +15,7 @@ add_executable(PixelDefense
|
|||||||
game/systems/s_event.c
|
game/systems/s_event.c
|
||||||
game/systems/s_input.c
|
game/systems/s_input.c
|
||||||
game/systems/s_ui.c
|
game/systems/s_ui.c
|
||||||
|
game/systems/systems.c
|
||||||
game/systems/systems.h
|
game/systems/systems.h
|
||||||
|
|
||||||
game/buildings.c
|
game/buildings.c
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
|
|||||||
BZ_ASSERT(buildingTile != -1);
|
BZ_ASSERT(buildingTile != -1);
|
||||||
|
|
||||||
// Create entity
|
// Create entity
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = entityCreate(ECS);
|
||||||
|
|
||||||
ecs_set(ECS, e, TilePosition, { .x = tileX, .y = tileY });
|
ecs_set(ECS, e, TilePosition, { .x = tileX, .y = tileY });
|
||||||
ecs_set(ECS, e, TileSize, { .sizeX = sizeX, .sizeY = sizeY });
|
ecs_set(ECS, e, TileSize, { .sizeX = sizeX, .sizeY = sizeY });
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include "unit_ai.h"
|
#include "unit_ai.h"
|
||||||
#include "unit_actions.h"
|
#include "unit_actions.h"
|
||||||
|
|
||||||
|
ECS_TAG_DECLARE(GameEntity);
|
||||||
|
|
||||||
ECS_COMPONENT_DECLARE(Resource);
|
ECS_COMPONENT_DECLARE(Resource);
|
||||||
|
|
||||||
ECS_COMPONENT_DECLARE(TilePosition);
|
ECS_COMPONENT_DECLARE(TilePosition);
|
||||||
@@ -38,6 +40,8 @@ ECS_TAG_DECLARE(Workable);
|
|||||||
ECS_TAG_DECLARE(Attackable);
|
ECS_TAG_DECLARE(Attackable);
|
||||||
|
|
||||||
void initComponentIDs(ecs_world_t *ecs) {
|
void initComponentIDs(ecs_world_t *ecs) {
|
||||||
|
ECS_TAG_DEFINE(ecs, GameEntity);
|
||||||
|
|
||||||
ECS_COMPONENT_DEFINE(ecs, Resource);
|
ECS_COMPONENT_DEFINE(ecs, Resource);
|
||||||
|
|
||||||
ECS_COMPONENT_DEFINE(ecs, TilePosition);
|
ECS_COMPONENT_DEFINE(ecs, TilePosition);
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
|
|
||||||
#include "game_tileset.h"
|
#include "game_tileset.h"
|
||||||
|
|
||||||
|
// Needed, so we can clean up all game created entities
|
||||||
|
extern ECS_TAG_DECLARE(GameEntity);
|
||||||
|
|
||||||
typedef enum ResourceType {
|
typedef enum ResourceType {
|
||||||
RES_IRON,
|
RES_IRON,
|
||||||
RES_WOOD,
|
RES_WOOD,
|
||||||
@@ -190,5 +193,11 @@ extern ECS_TAG_DECLARE(Attackable);
|
|||||||
|
|
||||||
void initComponentIDs(ecs_world_t *ecs);
|
void initComponentIDs(ecs_world_t *ecs);
|
||||||
|
|
||||||
|
static ecs_entity_t entityCreate(ecs_world_t *ecs) {
|
||||||
|
ecs_entity_t entity = ecs_new_id(ecs);
|
||||||
|
ecs_add_id(ecs, entity, GameEntity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif //PIXELDEFENSE_COMPONENTS_H
|
#endif //PIXELDEFENSE_COMPONENTS_H
|
||||||
|
|||||||
58
game/main.c
58
game/main.c
@@ -25,9 +25,6 @@ ECS_COMPONENT_DECLARE(InputState);
|
|||||||
BzUI *UI = NULL;
|
BzUI *UI = NULL;
|
||||||
ecs_world_t *ECS = NULL;
|
ecs_world_t *ECS = NULL;
|
||||||
|
|
||||||
static ecs_entity_t renderCollidersSystem;
|
|
||||||
static ecs_entity_t renderDebugPathSystem;
|
|
||||||
|
|
||||||
bool init(void *userData);
|
bool init(void *userData);
|
||||||
void deinit(void *userData);
|
void deinit(void *userData);
|
||||||
|
|
||||||
@@ -86,6 +83,7 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void unloadMap(Game *game) {
|
void unloadMap(Game *game) {
|
||||||
|
ecs_delete_with(ECS, GameEntity);
|
||||||
if (game->map.isValid) {
|
if (game->map.isValid) {
|
||||||
bzTileMapDestroy(&game->map);
|
bzTileMapDestroy(&game->map);
|
||||||
game->map.isValid = false;
|
game->map.isValid = false;
|
||||||
@@ -134,21 +132,6 @@ void loadMap(Game *game, const char *path) {
|
|||||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
|
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) {
|
int cmpDrawData(const void *a, const void *b) {
|
||||||
const DrawData *lhs = (DrawData *) a;
|
const DrawData *lhs = (DrawData *) a;
|
||||||
const DrawData *rhs = (DrawData *) b;
|
const DrawData *rhs = (DrawData *) b;
|
||||||
@@ -221,42 +204,9 @@ bool init(void *userData) {
|
|||||||
.texturePath="assets/game.png"
|
.texturePath="assets/game.png"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setupSystems();
|
||||||
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);
|
|
||||||
|
|
||||||
loadMap(game, "assets/maps/main_menu_01.tmj");
|
loadMap(game, "assets/maps/main_menu_01.tmj");
|
||||||
|
|
||||||
renderDebugPathSystem = renderDebugPath;
|
|
||||||
renderCollidersSystem = renderColliders;
|
|
||||||
|
|
||||||
game->debugDraw.mapColliders = true;
|
game->debugDraw.mapColliders = true;
|
||||||
game->debugDraw.spatialGrid = true;
|
game->debugDraw.spatialGrid = true;
|
||||||
@@ -269,6 +219,8 @@ void deinit(void *userData) {
|
|||||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||||
|
|
||||||
|
unloadMap(game);
|
||||||
|
|
||||||
// Destroy queries
|
// Destroy queries
|
||||||
ecs_query_fini(input->queries.selected);
|
ecs_query_fini(input->queries.selected);
|
||||||
ecs_query_fini(game->drawQuery);
|
ecs_query_fini(game->drawQuery);
|
||||||
@@ -283,7 +235,6 @@ void deinit(void *userData) {
|
|||||||
game = &gameCopy;
|
game = &gameCopy;
|
||||||
input = &inputCopy;
|
input = &inputCopy;
|
||||||
|
|
||||||
unloadMap(game);
|
|
||||||
bzTilesetDestroy(&game->tileset);
|
bzTilesetDestroy(&game->tileset);
|
||||||
|
|
||||||
bzStackAllocDestroy(&game->stackAlloc);
|
bzStackAllocDestroy(&game->stackAlloc);
|
||||||
@@ -454,6 +405,7 @@ static void renderMainMenu(Game *game, float dt) {
|
|||||||
});
|
});
|
||||||
if (uiMainMenuButton("Play") || true) {
|
if (uiMainMenuButton("Play") || true) {
|
||||||
game->screen = SCREEN_GAME;
|
game->screen = SCREEN_GAME;
|
||||||
|
unloadMap(game);
|
||||||
loadMap(game, "assets/maps/map_01.tmj");
|
loadMap(game, "assets/maps/map_01.tmj");
|
||||||
}
|
}
|
||||||
if (uiMainMenuButton("Settings")) {
|
if (uiMainMenuButton("Settings")) {
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
|||||||
*/
|
*/
|
||||||
bzLogInfo("%d %.2f %.2f", object.gid, object.shape.sizeX, object.shape.sizeY);
|
bzLogInfo("%d %.2f %.2f", object.gid, object.shape.sizeX, object.shape.sizeY);
|
||||||
//EntityArms arms = {
|
//EntityArms arms = {
|
||||||
// .left=ecs_new_id(ECS),
|
// .left=createEntity(ECS),
|
||||||
// .right=ecs_new_id(ECS),
|
// .right=createEntity(ECS),
|
||||||
//};
|
//};
|
||||||
//ecs_set_ptr(ECS, e, EntityArms, &arms);
|
//ecs_set_ptr(ECS, e, EntityArms, &arms);
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
|
|
||||||
const i32 tileWidth = map->tileWidth;
|
const i32 tileWidth = map->tileWidth;
|
||||||
const i32 tileHeight = map->tileHeight;
|
const i32 tileHeight = map->tileHeight;
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = entityCreate(ECS);
|
||||||
Size size = {.x = tileSize.sizeX * tileWidth, .y = tileSize.sizeY * tileHeight };
|
Size size = {.x = tileSize.sizeX * tileWidth, .y = tileSize.sizeY * tileHeight };
|
||||||
ecs_set_ptr(ECS, e, Size, &size);
|
ecs_set_ptr(ECS, e, Size, &size);
|
||||||
ecs_set(ECS, e, Position, {
|
ecs_set(ECS, e, Position, {
|
||||||
@@ -117,7 +117,7 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
f32 sizeY = tileset->tileHeight;
|
f32 sizeY = tileset->tileHeight;
|
||||||
f32 posX = layer->offsetX + x * sizeX;
|
f32 posX = layer->offsetX + x * sizeX;
|
||||||
f32 posY = layer->offsetY + y * sizeY;
|
f32 posY = layer->offsetY + y * sizeY;
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = entityCreate(ECS);
|
||||||
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &e, posX, posY, sizeX, sizeY);
|
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &e, posX, posY, sizeX, sizeY);
|
||||||
ecs_set(ECS, e, SpatialGridID, {gridID});
|
ecs_set(ECS, e, SpatialGridID, {gridID});
|
||||||
posX += sizeX * 0.5f;
|
posX += sizeX * 0.5f;
|
||||||
@@ -137,7 +137,7 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ecs_entity_t createWorker(Position position, Size size, BzSpatialGrid *grid, BzTileset *tileset, BzTile gid) {
|
ecs_entity_t createWorker(Position position, Size size, BzSpatialGrid *grid, BzTileset *tileset, BzTile gid) {
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = entityCreate(ECS);
|
||||||
ecs_set_ptr(ECS, e, Position, &position);
|
ecs_set_ptr(ECS, e, Position, &position);
|
||||||
ecs_set_ptr(ECS, e, Size, &size);
|
ecs_set_ptr(ECS, e, Size, &size);
|
||||||
BzSpatialGridID spatialID = bzSpatialGridInsert(grid, &e,
|
BzSpatialGridID spatialID = bzSpatialGridInsert(grid, &e,
|
||||||
|
|||||||
56
game/systems/systems.c
Normal file
56
game/systems/systems.c
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include "systems.h"
|
||||||
|
|
||||||
|
#include "../game_state.h"
|
||||||
|
|
||||||
|
ecs_entity_t renderCollidersSystem;
|
||||||
|
ecs_entity_t renderDebugPathSystem;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
void setupSystems() {
|
||||||
|
ecs_set_hooks(ECS, SpatialGridID, {
|
||||||
|
.dtor = ecs_dtor(SpatialGridID)
|
||||||
|
});
|
||||||
|
ecs_set_hooks(ECS, Path, {
|
||||||
|
.dtor = ecs_dtor(Path)
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
renderDebugPathSystem = renderDebugPath;
|
||||||
|
renderCollidersSystem = renderColliders;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -163,29 +163,9 @@ void drawPlayerInputUI();
|
|||||||
* MISC
|
* MISC
|
||||||
**********************************/
|
**********************************/
|
||||||
|
|
||||||
static void setupSystems(ecs_world_t *ecs) {
|
extern ecs_entity_t renderCollidersSystem;
|
||||||
|
extern ecs_entity_t renderDebugPathSystem;
|
||||||
|
|
||||||
ECS_OBSERVER(ecs, entityPathRemove, EcsOnRemove, Path);
|
void setupSystems();
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||||
|
|||||||
Reference in New Issue
Block a user