Add destructor for SpatialGridID and Path, remove unused Animation code

This commit is contained in:
2023-12-29 09:06:53 +01:00
parent 0c9fae7781
commit 6e2c68cb70
4 changed files with 25 additions and 100 deletions

View File

@@ -133,6 +133,20 @@ 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;
}
})
bool init(void *userData) {
BZ_UNUSED(userData);
@@ -185,10 +199,14 @@ bool init(void *userData) {
.path="assets/game.tsj",
.texturePath="assets/game.png"
});
loadMap(game, "assets/maps/main_menu_01.tmj");
ECS_OBSERVER(ECS, entitySpatialRemove, EcsOnRemove, SpatialGridID);
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_OBSERVER(ECS, entitySetAnimationState, EcsOnSet, Animation, AnimationType);
@@ -217,6 +235,8 @@ bool init(void *userData) {
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation, TextureEntities);
loadMap(game, "assets/maps/main_menu_01.tmj");
renderDebugPathSystem = renderDebugPath;
renderCollidersSystem = renderColliders;

View File

@@ -118,7 +118,8 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
f32 posX = layer->offsetX + x * sizeX;
f32 posY = layer->offsetY + y * sizeY;
ecs_entity_t e = ecs_new_id(ECS);
bzSpatialGridInsert(game->entityGrid, &e, posX, posY, sizeX, sizeY);
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &e, posX, posY, sizeX, sizeY);
ecs_set(ECS, e, SpatialGridID, {gridID});
posX += sizeX * 0.5f;
posY += sizeY * 0.5f;
ecs_add(ECS, e, TextureTerrain);

View File

@@ -41,13 +41,7 @@ void updateUnitActionsSystem(ecs_iter_t *it);
* Entity Systems
**********************************/
/* Observer (for unregistering collision)
* 0: Game (singleton)
* 1: SpatialGridID
*/
void entitySpatialRemove(ecs_iter_t *it);
/* Observer (for releasing path objects)
/* Observer (for removing TargetPosition)
* 0: Game (singleton) for object pool
* 1: Path
*/

View File

@@ -35,47 +35,15 @@ static Position getBottomLeftPos(Position pos, Size size) {
return (Position) {pos.x - size.x * 0.5f, pos.y - size.y * 0.5f};
}
void entitySpatialRemove(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 1);
for (i32 i = 0; i < it->count; i++) {
bzSpatialGridRemove(game->entityGrid, spatialID[i]);
}
}
void entityPathRemove(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
BzObjectPool *pool = game->pools.pathData;
Path *path = ecs_field(it, Path, 1);
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t entity = it->entities[i];
ecs_remove(ECS, entity, TargetPosition);
PathData *cur = path[i].paths;
while (cur) {
bzObjectPoolRelease(pool, cur);
cur = cur->next;
}
}
}
void entitySetAnimationState(ecs_iter_t *it) {
/*
Animation *anim = ecs_field(it, Animation, 1);
AnimationType *animType = ecs_field(it, AnimationType , 2);
for (i32 i = 0; i < it->count; i++) {
EntityType entityType = anim[i].entityType;
AnimationType type = animType[i];
BZ_ASSERT(entityHasAnimation(entityType, type));
anim[i].animType = type;
anim[i].sequence = getEntityAnimation(entityType, type);
}
*/
}
void entityUpdateSpatialID(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
@@ -194,64 +162,6 @@ void entityFollowPath(ecs_iter_t *it) {
}
}
/*
void entityHarvestTaskSystem(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
const Position *position = ecs_field(it, Position, 1);
const Rotation *rotation = ecs_field(it, Rotation, 2);
const HarvestTask *harvestTask = ecs_field(it, HarvestTask, 3);
for (i32 i = 0; i < it->count; i++) {
const ecs_entity_t entity = it->entities[i];
const ecs_entity_t targetEntity = harvestTask[i].entity;
const Position *pTarget = ecs_get(ECS, targetEntity, Position);
BZ_ASSERT(pTarget);
const Position target = *pTarget;
const f32 DST_LIMIT = 10.0f;
Resource *resource = ecs_get_mut(ECS, targetEntity, Resource);
if (resource->amount <= 0) {
ecs_delete(ECS, targetEntity);
ecs_remove(ECS, entity, HarvestTask);
continue;
}
const f32 dst = Vector2Distance(position[i], target);
if (!ecs_has(ECS, entity, Path) && dst > DST_LIMIT) {
bzLogInfo("%.2f", dst);
// Pathfind to target
entitySetPath(entity, target, game);
continue;
} else if (dst < DST_LIMIT && !ecs_has(ECS, entity, Path)) {
if (!ecs_has(ECS, entity, Path)) {
bzLogInfo("Mine");
resource->amount -= 5;
}
ecs_remove(ECS, entity, Path);
// MINE
// find nearest warehouse for wood
ecs_entity_t storage = findNearestStorage(RES_WOOD);
if (storage) {
const Position *storagePos = ecs_get(ECS, storage, Position);
BZ_ASSERT(storagePos);
entitySetPath(entity, *storagePos, game);
}
}
// Harvest
const i32 carryCapacity = 5;
}
}
*/
void entityUpdateAnimationState(ecs_iter_t *it) {
Animation *anim = ecs_field(it, Animation, 1);
TextureRegion *text = ecs_field(it, TextureRegion, 2);