Basic wood chopping

This commit is contained in:
2023-12-10 11:08:40 +01:00
parent b410867902
commit 11832ec1cc
10 changed files with 224 additions and 45 deletions

View File

@@ -74,14 +74,27 @@ void inputUnitAction(Game *game, InputState *input) {
const MouseButton actionBtn = input->mapping.secondaryBtn;
input->cursor = CURSOR_NONE;
if (queryEntity(game->entityGrid, input->mouseWorld, Harvestable)) {
ecs_entity_t taskEntity;
if ((taskEntity = queryEntity(game->entityGrid, input->mouseWorld, Harvestable))) {
input->cursor = CURSOR_COLLECT_WOOD;
if (isInputBtnJustUp(input, actionBtn)) {
bzLogInfo("Chop wood!");
ecs_defer_begin(ECS);
ecs_iter_t it = ecs_query_iter(ECS, query);
while (ecs_query_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
const ecs_entity_t entity = it.entities[i];
ecs_set(ECS, entity, HarvestTask, {taskEntity});
goto while_break;
}
}
while_break:
ecs_iter_fini(&it);
ecs_defer_end(ECS);
}
return;
}
if (isInputBtnJustDown(input, actionBtn)) {
if (isInputBtnJustUp(input, actionBtn)) {
// Note: We mustn't use ecs ecs_remove_all since this will also
// remove ongoing paths that are not part of this query.
iterateSelectedUnits(input->queries.selected, iterRemovePaths);
@@ -91,21 +104,11 @@ void inputUnitAction(Game *game, InputState *input) {
ecs_iter_t it = ecs_query_iter(ECS, query);
ecs_defer_begin(ECS);
while (ecs_iter_next(&it)) {
Position *pos = ecs_field(&it, Position, 1);
const Position *pos = ecs_field(&it, Position, 1);
for (i32 i = 0; i < it.count; i++) {
const ecs_entity_t entity = it.entities[i];
Path path = {NULL, 0};
pathfindAStar(&(PathfindingDesc) {
.start = pos[i],
.target = target,
.map = map,
.outPath = &path,
.pool = game->pools.pathData,
.alloc = &game->stackAlloc
});
if (!path.paths) continue;
ecs_set_ptr(ECS, entity, Path, &path);
entitySetPath(entity, target, game);
}
}
ecs_defer_end(ECS);
@@ -380,6 +383,7 @@ ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t
ecs_entity_t closest = 0;
while (bzSpatialGridQueryNext(&it)) {
ecs_entity_t entity = *(ecs_entity_t *) it.data;
if (!ecs_is_alive(ECS, entity)) continue;
if (!ecs_has_id(ECS, entity, Selectable)) continue;
if (!ecs_has_id(ECS, entity, tag)) continue;
Vector2 pos;