Fix bug with follow path system

This commit is contained in:
2023-12-07 11:35:35 +01:00
parent ddb562a62e
commit e7e4d1e4ce
4 changed files with 22 additions and 9 deletions

View File

@@ -135,8 +135,8 @@ bool init(void *userData) {
.userDataSize=sizeof(ecs_entity_t)
});
ECS_OBSERVER(ECS, entitySpatialRemoved, EcsOnRemove, Position, SpatialGridID);
ECS_OBSERVER(ECS, entityPathRemoved, EcsOnRemove, Path);
ECS_OBSERVER(ECS, entitySpatialRemove, EcsOnRemove, Position, SpatialGridID);
ECS_OBSERVER(ECS, entityPathRemove, EcsOnRemove, Path);
ECS_OBSERVER(ECS, entitySetAnimationState, EcsOnSet, Animation, AnimationType);

View File

@@ -132,6 +132,17 @@ bool pathfindAStar(const PathfindingDesc *desc) {
BZ_ASSERT(start.x >= 0 && start.x < map->width);
BZ_ASSERT(start.y >= 0 && start.y < map->height);
// Perform very cheap ray cast check
if (bzTileMapCanRayCastLine(map, desc->start, desc->target)) {
PathData *pathData = bzObjectPool(desc->pool);
BZ_ASSERT(pathData);
pathData->waypoints[0] = desc->target;
pathData->numWaypoints = 1;
pathData->next = NULL;
*desc->outPath = (Path) {pathData, 0};
return true;
}
i32 numTiles = map->width * map->height;
PathNodeRecord *closedSet = bzStackAlloc(desc->alloc, sizeof(*closedSet) * numTiles);

View File

@@ -15,13 +15,13 @@
* 1: Position
* 3: SpatialGridID
*/
void entitySpatialRemoved(ecs_iter_t *it);
void entitySpatialRemove(ecs_iter_t *it);
/* Observer (for releasing path objects)
* 0: Game (singleton) for object pool
* 1: Path
*/
void entityPathRemoved(ecs_iter_t *it);
void entityPathRemove(ecs_iter_t *it);
/* Observer (for updating animation state)
* 1: Animation

View File

@@ -9,7 +9,7 @@ static Position getBottomLeftPos(Position pos, Size size) {
return (Position) {pos.x - size.x * 0.5f, pos.y - size.y * 0.5f};
}
void entitySpatialRemoved(ecs_iter_t *it) {
void entitySpatialRemove(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *pos = ecs_field(it, Position, 1);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID , 2);
@@ -20,7 +20,7 @@ void entitySpatialRemoved(ecs_iter_t *it) {
}
void entityPathRemoved(ecs_iter_t *it) {
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);
@@ -154,11 +154,11 @@ void entityFollowPath(ecs_iter_t *it) {
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t entity = it->entities[i];
TargetPosition target = path[i].paths->waypoints[path[i].curWaypoint];
Path *entityPath = &path[i];
TargetPosition target = entityPath->paths->waypoints[entityPath->curWaypoint];
if (!ecs_has(ECS, entity, TargetPosition)) {
ecs_set_ptr(ECS, entity, TargetPosition, &target);
path[i].curWaypoint++;
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
path[i].curWaypoint = 0;
PathData *oldPath = path[i].paths;
@@ -166,6 +166,8 @@ void entityFollowPath(ecs_iter_t *it) {
path[i].paths = path[i].paths->next;
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
}
ecs_set_ptr(ECS, entity, TargetPosition, &target);
path[i].curWaypoint++;
}
}
}