Rework input

This commit is contained in:
2023-12-07 14:19:10 +01:00
parent d3485303c9
commit 8543cc7b43
6 changed files with 220 additions and 122 deletions

View File

@@ -71,11 +71,13 @@ bool init(void *userData) {
input->ESC = KEY_ESCAPE;
input->CLICK_LIMIT = 0.2f;
input->unitSelectedQuery = ecs_query(ECS, {
// Create queries
input->queries.selected = ecs_query(ECS, {
.filter.terms = {
{ ecs_id(Position) }, { ecs_id(Size) }, { ecs_id(UnitSelected) }
{ ecs_id(Position) }, { ecs_id(Size) }, { ecs_id(Selected) }
}
});
input->unitPositions = bzArrayCreate(Position, 16);
@@ -94,7 +96,7 @@ bool init(void *userData) {
game->camera.target = (Vector2) {0, 0};
game->camera.offset = (Vector2) {screenWidth / 2.0f, screenHeight / 2.0f};
game->camera.rotation = 0.0f;
game->camera.zoom = 1.0f;
game->camera.zoom = 3.0f;
game->frameDuration = 0.16f;
game->terrainTileset = bzTilesetCreate( &(BzTilesetDesc) {
@@ -188,6 +190,9 @@ void deinit(void *userData) {
Game gameCopy = *game;
InputState inputCopy = *input;
// Destroy queries
ecs_query_fini(inputCopy.queries.selected);
ecs_fini(ECS);
ECS = NULL;
@@ -265,19 +270,33 @@ void imguiRender(float dt, void *userData) {
igText("Input state: %s", inputState);
if (igCollapsingHeader_TreeNodeFlags("Selection", 0)) {
switch (input->state) {
case INPUT_SELECTED_UNITS:
case INPUT_SELECTED_UNITS: {
igText("Selected units:");
ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery);
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
igText("\t%llu", it.entities[i]);
ecs_entity_t entity = it.entities[i];
igText("\tEntity %llu", entity);
}
}
break;
case INPUT_SELECTED_OBJECT:
break;
case INPUT_SELECTED_BUILDING:
}
case INPUT_SELECTED_OBJECT: {
igText("Selected objects:");
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i];
if (ecs_has(ECS, entity, Harvestable) &&
ecs_has(ECS, entity, Resource)) {
Resource res = *ecs_get(ECS, entity, Resource);
const char *resName = getResourceTypePrettyName(res.type);
igText("\tEntity %llu: %d %s", entity, res.amount, resName);
}
}
}
break;
}
default:
igText("NONE");
break;