Limit how many workers can harvest the same resource

This commit is contained in:
2024-02-05 09:19:59 +01:00
parent ab0fef8ebf
commit d55ed29f97
11 changed files with 87 additions and 19 deletions

View File

@@ -84,7 +84,7 @@ void inputUnitAction(Game *game, InputState *input) {
input->cursor = CURSOR_NONE;
ecs_entity_t taskEntity;
bool isWorker = selectedAnyHasID(query, ecs_id(Worker));
if (isWorker && (taskEntity = queryEntity(game->entityGrid, input->mouseWorld, Harvestable))) {
if (isWorker && (taskEntity = queryEntity(game->entityGrid, input->mouseWorld, ecs_id(Harvestable)))) {
Resource resource = *ecs_get(ECS, taskEntity, Resource);
switch (resource.type) {
case RES_WOOD:
@@ -99,12 +99,39 @@ void inputUnitAction(Game *game, InputState *input) {
default:;
}
if (isInputBtnJustUp(input, actionBtn)) {
const f32 hRadius = 10.0f;
const Vector2 mPos = input->mouseWorld;
BzSpatialGridIter gridIt = bzSpatialGridIter(game->entityGrid,
mPos.x - hRadius, mPos.y - hRadius,
mPos.x + hRadius, mPos.y + hRadius);
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++) {
ecs_entity_t entity = it.entities[i];
Position target = *ecs_get(ECS, taskEntity, Position);
bool hasNext = false;
ecs_entity_t harvestEntity = 0;
do {
hasNext = bzSpatialGridQueryNext(&gridIt);
if (!hasNext) break;
harvestEntity = *(ecs_entity_t *) gridIt.data;
if (!ecs_has(ECS, harvestEntity, Resource))
continue;
const Resource *res = ecs_get(ECS, harvestEntity, Resource);
if (res->type != resource.type)
continue;
if (!ecs_has(ECS, harvestEntity, Harvestable))
continue;
Harvestable *harvestable = ecs_get_mut(ECS, harvestEntity, Harvestable);
if (harvestable->harvestCount >= harvestable->harvestLimit)
continue;
harvestable->harvestCount++;
break;
} while (hasNext);
if (!hasNext) break;
Position target = *ecs_get(ECS, harvestEntity, Position);
f32 proximity = 6.0f;
if (resource.type == RES_FOOD)
@@ -116,7 +143,7 @@ void inputUnitAction(Game *game, InputState *input) {
setAIBehaviour(entity, game->BTs.workerHarvest, &(AIBlackboard) {
.as.worker = {
.harvestType = resource.type,
.harvestTarget = taskEntity,
.harvestTarget = harvestEntity,
.harvestPos = target,
},
.proximity = proximity,