Fix harvesting and add resource depositing

This commit is contained in:
2023-12-29 14:54:13 +01:00
parent 31a9289770
commit 681080f3ed
5 changed files with 27 additions and 8 deletions

View File

@@ -23,8 +23,6 @@ ECS_COMPONENT_DECLARE(TextureRegion);
ECS_COMPONENT_DECLARE(Animation); ECS_COMPONENT_DECLARE(Animation);
ECS_COMPONENT_DECLARE(Easing); ECS_COMPONENT_DECLARE(Easing);
ECS_COMPONENT_DECLARE(HarvestEvent);
ECS_COMPONENT_DECLARE(UnitAI); ECS_COMPONENT_DECLARE(UnitAI);
ECS_COMPONENT_DECLARE(UnitAction); ECS_COMPONENT_DECLARE(UnitAction);
@@ -61,8 +59,6 @@ void initComponentIDs(ecs_world_t *ecs) {
ECS_COMPONENT_DEFINE(ecs, Animation); ECS_COMPONENT_DEFINE(ecs, Animation);
ECS_COMPONENT_DEFINE(ecs, Easing); ECS_COMPONENT_DEFINE(ecs, Easing);
ECS_COMPONENT_DEFINE(ecs, HarvestEvent);
ECS_COMPONENT_DEFINE(ecs, UnitAI); ECS_COMPONENT_DEFINE(ecs, UnitAI);
ECS_COMPONENT_DEFINE(ecs, UnitAction); ECS_COMPONENT_DEFINE(ecs, UnitAction);

View File

@@ -149,7 +149,9 @@ typedef struct EntityArms {
typedef struct HarvestEvent { typedef struct HarvestEvent {
i32 amount; i32 amount;
} HarvestEvent; } HarvestEvent;
extern ECS_COMPONENT_DECLARE(HarvestEvent); typedef struct DepositEvent {
i32 amount;
} DepositEvent;
/********************************************************** /**********************************************************
* Gameplay components * Gameplay components

View File

@@ -26,3 +26,6 @@ i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) {
return event.amount; return event.amount;
} }
void depositEvent(ecs_entity_t entity, DepositEvent event) {
}

View File

@@ -127,6 +127,7 @@ void renderDebugPath(ecs_iter_t *it);
**********************************/ **********************************/
i32 harvestEvent(ecs_entity_t entity, HarvestEvent event); i32 harvestEvent(ecs_entity_t entity, HarvestEvent event);
void depositEvent(ecs_entity_t entity, DepositEvent event);
/********************************** /**********************************

View File

@@ -31,7 +31,7 @@ void actionCollectResource(ecs_entity_t entity, Action *action, Game *game) {
ecs_entity_t target = action->as.collectResource.entity; ecs_entity_t target = action->as.collectResource.entity;
bool targetAlive = ecs_is_alive(ECS, target); bool targetAlive = ecs_is_alive(ECS, target);
action->finished = !targetAlive; action->finished |= !targetAlive;
if (!action->finished && action->elapsed > worker->collectSpeed) { if (!action->finished && action->elapsed > worker->collectSpeed) {
i32 spareCapacity = worker->carryCapacity - worker->carry; i32 spareCapacity = worker->carryCapacity - worker->carry;
@@ -44,9 +44,26 @@ void actionCollectResource(ecs_entity_t entity, Action *action, Game *game) {
} }
void actionDepositResource(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; 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) { void handleAction(ecs_entity_t entity, UnitAction *unitAction, Game *game) {
Action *action = unitAction->first; Action *action = unitAction->first;