Limit how many workers can harvest the same resource
This commit is contained in:
@@ -3,6 +3,13 @@
|
||||
#include "../game_state.h"
|
||||
#include "../ai_actions.h"
|
||||
|
||||
void resetHarvestCount(ecs_iter_t *it) {
|
||||
Harvestable *harvestable = ecs_field(it, Harvestable, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
harvestable[i].harvestCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void updateAISystem(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "../sounds.h"
|
||||
|
||||
i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) {
|
||||
BZ_ASSERT(ecs_has_id(ECS, entity, Harvestable));
|
||||
BZ_ASSERT(ecs_has_id(ECS, entity, ecs_id(Harvestable)));
|
||||
BZ_ASSERT(ecs_has(ECS, entity, Resource));
|
||||
|
||||
ecs_set(ECS, entity, Easing, {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -125,6 +125,7 @@ void setupSystems() {
|
||||
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
|
||||
ECS_SYSTEM(ECS, entityUpdateArms, EcsOnUpdate, Position, Velocity, Rotation, Orientation, Arms);
|
||||
|
||||
ECS_SYSTEM(ECS, resetHarvestCount, EcsOnUpdate, Harvestable);
|
||||
ECS_SYSTEM(ECS, updateAISystem, EcsOnUpdate, BzBTState);
|
||||
|
||||
ECS_SYSTEM(ECS, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
|
||||
|
||||
@@ -18,6 +18,11 @@ bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game);
|
||||
* AI systems
|
||||
**********************************/
|
||||
|
||||
/*
|
||||
* 1. Harvestable
|
||||
*/
|
||||
void resetHarvestCount(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton)
|
||||
* 1: BzBTState
|
||||
|
||||
Reference in New Issue
Block a user