From 681080f3edfe4407137066d2e6794ceb53c4f48c Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Fri, 29 Dec 2023 14:54:13 +0100 Subject: [PATCH] Fix harvesting and add resource depositing --- game/components.c | 4 ---- game/components.h | 4 +++- game/systems/s_event.c | 3 +++ game/systems/systems.h | 3 ++- game/unit_actions.c | 21 +++++++++++++++++++-- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/game/components.c b/game/components.c index 3d41350..5479576 100644 --- a/game/components.c +++ b/game/components.c @@ -23,8 +23,6 @@ ECS_COMPONENT_DECLARE(TextureRegion); ECS_COMPONENT_DECLARE(Animation); ECS_COMPONENT_DECLARE(Easing); -ECS_COMPONENT_DECLARE(HarvestEvent); - ECS_COMPONENT_DECLARE(UnitAI); ECS_COMPONENT_DECLARE(UnitAction); @@ -61,8 +59,6 @@ void initComponentIDs(ecs_world_t *ecs) { ECS_COMPONENT_DEFINE(ecs, Animation); ECS_COMPONENT_DEFINE(ecs, Easing); - ECS_COMPONENT_DEFINE(ecs, HarvestEvent); - ECS_COMPONENT_DEFINE(ecs, UnitAI); ECS_COMPONENT_DEFINE(ecs, UnitAction); diff --git a/game/components.h b/game/components.h index 43cc8ba..d666037 100644 --- a/game/components.h +++ b/game/components.h @@ -149,7 +149,9 @@ typedef struct EntityArms { typedef struct HarvestEvent { i32 amount; } HarvestEvent; - extern ECS_COMPONENT_DECLARE(HarvestEvent); + typedef struct DepositEvent { + i32 amount; + } DepositEvent; /********************************************************** * Gameplay components diff --git a/game/systems/s_event.c b/game/systems/s_event.c index adbc44b..4a0600a 100644 --- a/game/systems/s_event.c +++ b/game/systems/s_event.c @@ -26,3 +26,6 @@ i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) { return event.amount; } + +void depositEvent(ecs_entity_t entity, DepositEvent event) { +} diff --git a/game/systems/systems.h b/game/systems/systems.h index aa5b063..6825b77 100644 --- a/game/systems/systems.h +++ b/game/systems/systems.h @@ -126,7 +126,8 @@ void renderDebugPath(ecs_iter_t *it); * Event Systems **********************************/ -i32 harvestEvent(ecs_entity_t entity, HarvestEvent event); +i32 harvestEvent(ecs_entity_t entity, HarvestEvent event); +void depositEvent(ecs_entity_t entity, DepositEvent event); /********************************** diff --git a/game/unit_actions.c b/game/unit_actions.c index fcd8025..ee60010 100644 --- a/game/unit_actions.c +++ b/game/unit_actions.c @@ -31,7 +31,7 @@ void actionCollectResource(ecs_entity_t entity, Action *action, Game *game) { ecs_entity_t target = action->as.collectResource.entity; bool targetAlive = ecs_is_alive(ECS, target); - action->finished = !targetAlive; + action->finished |= !targetAlive; if (!action->finished && action->elapsed > worker->collectSpeed) { i32 spareCapacity = worker->carryCapacity - worker->carry; @@ -44,8 +44,25 @@ void actionCollectResource(ecs_entity_t entity, Action *action, Game *game) { } void actionDepositResource(ecs_entity_t entity, Action *action, Game *game) { - if (action->elapsed > 0.2f) + if (action->finished) return; + + BZ_ASSERT(ecs_has(ECS, entity, Worker)); + Worker *worker = ecs_get_mut(ECS, entity, Worker); + + if (worker->carry == 0) { action->finished = true; + } + ecs_entity_t target = action->as.depositResource.entity; + bool targetAlive = ecs_is_alive(ECS, target); + action->finished |= !targetAlive; + + if (!action->finished && action->elapsed > worker->depositSpeed) { + depositEvent(target, (DepositEvent) { + .amount = worker->carry + }); + worker->carry = 0; + action->finished = true; + } } void handleAction(ecs_entity_t entity, UnitAction *unitAction, Game *game) {