Allocate path with object pool for pathfinding
This commit is contained in:
@@ -74,17 +74,24 @@ void updatePos(ecs_iter_t *it) {
|
||||
}
|
||||
#include <stdlib.h>
|
||||
void targetFinish(ecs_iter_t *it) {
|
||||
const Game *game = ecs_singleton_get(ECS, Game);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t e = it->entities[i];
|
||||
Path *path = ecs_get_mut(it->world, e, Path);
|
||||
if (!path) continue;
|
||||
path->curWaypoint++;
|
||||
if (path->curWaypoint >= path->numWaypoints) {
|
||||
if (path->curWaypoint >= path->paths->numWaypoints) {
|
||||
PathData *finished = path->paths;
|
||||
path->paths = finished->next;
|
||||
path->curWaypoint = 0;
|
||||
bzObjectPoolRelease(game->pools.pathData, finished);
|
||||
// Finished
|
||||
ecs_remove(it->world, e, Path);
|
||||
continue;
|
||||
if (path->paths == NULL) {
|
||||
ecs_remove(it->world, e, Path);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
TargetPosition target = path->waypoints[path->curWaypoint - 1];
|
||||
TargetPosition target = path->paths->waypoints[path->curWaypoint];
|
||||
path->curWaypoint++;
|
||||
target.x += rand() % (4 + 1 + 2) -2;
|
||||
ecs_set(it->world, e, TargetPosition, {target.x, target.y});
|
||||
}
|
||||
@@ -95,28 +102,34 @@ void startPath(ecs_iter_t *it) {
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t e = it->entities[i];
|
||||
if (path->numWaypoints == 0) {
|
||||
if (path->paths == NULL) {
|
||||
ecs_remove(it->world, e, Path);
|
||||
continue;
|
||||
}
|
||||
ecs_set(it->world, e, TargetPosition, {path[i].waypoints[0].x, path[i].waypoints[0].y});
|
||||
path[i].curWaypoint++;
|
||||
ecs_set(it->world, e, TargetPosition, {path[i].paths->waypoints[0].x, path[i].paths->waypoints[0].y});
|
||||
path[i].curWaypoint = 1;
|
||||
}
|
||||
}
|
||||
void drawDebugPath(ecs_iter_t *it) {
|
||||
Path *path = ecs_field(it, Path, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
for (i32 iPath = 0; iPath < path[i].numWaypoints; iPath++) {
|
||||
Color color = RED;
|
||||
if (iPath < path[i].curWaypoint - 1)
|
||||
color = GREEN;
|
||||
else if (iPath == path[i].curWaypoint - 1)
|
||||
color = ORANGE;
|
||||
color.a = 180;
|
||||
PathData *pathData = path->paths;
|
||||
bool first = true;
|
||||
while (pathData) {
|
||||
for (i32 iPath = 0; iPath < pathData->numWaypoints; iPath++) {
|
||||
Color color = RED;
|
||||
if (first && iPath < path[i].curWaypoint - 1)
|
||||
color = GREEN;
|
||||
else if (first && iPath == path[i].curWaypoint - 1)
|
||||
color = ORANGE;
|
||||
color.a = 180;
|
||||
|
||||
Position pos = path[i].waypoints[iPath];
|
||||
DrawCircle(pos.x, pos.y, 3, color);
|
||||
Position pos = pathData->waypoints[iPath];
|
||||
DrawCircle(pos.x, pos.y, 3, color);
|
||||
}
|
||||
first = false;
|
||||
pathData = pathData->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user