Only allow for workers to harvest

This commit is contained in:
2024-01-28 14:26:39 +01:00
parent 5d96a02284
commit d0ad91d5b2
2 changed files with 21 additions and 4 deletions

View File

@@ -593,7 +593,7 @@ void imguiRender(float dt, void *userData) {
Game *game = ecs_singleton_get_mut(ECS, Game);
InputState *input = ecs_singleton_get_mut(ECS, InputState);
igShowDemoWindow(NULL);
//igShowDemoWindow(NULL);
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
igBegin("Debug Menu", NULL, 0);

View File

@@ -14,6 +14,8 @@ ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t
bool selectEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag, Player player);
void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player);
static bool selectedAnyHasID(ecs_query_t *query, ecs_entity_t id);
void addEntityToInspected(ecs_entity_t entity, Game *game);
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos));
@@ -81,7 +83,8 @@ void inputUnitAction(Game *game, InputState *input) {
input->cursor = CURSOR_NONE;
ecs_entity_t taskEntity;
if ((taskEntity = queryEntity(game->entityGrid, input->mouseWorld, Harvestable))) {
bool isWorker = selectedAnyHasID(query, ecs_id(Worker));
if (isWorker && (taskEntity = queryEntity(game->entityGrid, input->mouseWorld, Harvestable))) {
Resource resource = *ecs_get(ECS, taskEntity, Resource);
switch (resource.type) {
case RES_WOOD:
@@ -233,8 +236,8 @@ void updatePlayerInput() {
const MouseButton secondaryBtn = input->mapping.secondaryBtn;
i32 count = ecs_query_entity_count(input->queries.selected);
if (count > 0)
input->state = INPUT_SELECTED_UNITS;
//if (count > 0)
// input->state = INPUT_SELECTED_UNITS;
switch (input->state) {
case INPUT_NONE: {
@@ -414,6 +417,20 @@ void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player) {
}
}
static bool selectedAnyHasID(ecs_query_t *query, ecs_entity_t id) {
ecs_iter_t it = ecs_query_iter(ECS, query);
while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i];
if (ecs_has_id(ECS, entity, id)) {
ecs_iter_fini(&it);
return true;
}
}
}
return false;
}
void addEntityToInspected(ecs_entity_t entity, Game *game) {
bool alreadyInspecting = false;
for (i32 i = 0; i < bzArraySize(game->debug.inspecting); i++) {