Refactor entity input system

This commit is contained in:
2023-12-07 18:31:31 +01:00
parent 56a73fe28c
commit 85511812b5
3 changed files with 271 additions and 2 deletions

View File

@@ -55,7 +55,43 @@ void inputPrimaryAction(Game *game, InputState *input) {
if (selectedCount == 0)
input->state = INPUT_NONE;
}
void inputUnitAction(Game *game, InputState *input) {
void inputUnitAction(Game *game, const InputState *input) {
ecs_query_t *query = input->queries.selected;
BzTileMap *map = &game->map;
const i32 numUnits = ecs_query_entity_count(query);
BZ_ASSERT(numUnits > 0);
const MouseButton actionBtn = input->mapping.secondaryBtn;
if (isInputBtnJustDown(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);
const Position target = input->mouseWorld;
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);
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);
}
}
ecs_defer_end(ECS);
}
}
@@ -113,6 +149,10 @@ void updatePlayerInput() {
case INPUT_SELECTED_UNITS: {
inputPrimaryAction(game, input);
if (input->state != INPUT_SELECTED_UNITS) break;
inputUnitAction(game, input);
/*
i32 selectedCount = ecs_query_entity_count(input->queries.selected);
if (selectedCount > 1 && isInputBtnJustDragged(input, secondaryBtn)) {
// TODO: For click it should just move them
@@ -127,7 +167,6 @@ void updatePlayerInput() {
i32 unitPosIdx = 0;
ecs_defer_begin(ECS);
iterateSelectedUnits(input->queries.selected, iterRemovePaths);
ecs_defer_end(ECS);
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
@@ -185,6 +224,7 @@ void updatePlayerInput() {
}
ecs_defer_end(ECS);
}
*/
break;
}
case INPUT_SELECTED_OBJECT: {