Implement harvest worker AI

This commit is contained in:
2023-12-17 14:20:13 +01:00
parent 5564df4768
commit 33b28b620d
14 changed files with 338 additions and 40 deletions

View File

@@ -6,6 +6,7 @@
#include <raymath.h>
void actionMoveTo(ecs_entity_t entity, Action *action, Game *game) {
if (action->finished) return;
const Vector2 target = action->as.moveTo.target;
if (!ecs_has(ECS, entity, Path)) {
entitySetPath(entity, target, game);
@@ -16,18 +17,27 @@ void actionMoveTo(ecs_entity_t entity, Action *action, Game *game) {
f32 dst = Vector2Distance(pos, target);
if (dst < action->as.moveTo.proximityThreshold) {
action->finished = true;
ecs_remove(ECS, entity, Path);
}
}
void actionCollectResource(ecs_entity_t entity, Action *action, Game *game) {
if (action->finished) return;
if (action->elapsed > 2.0f) {
action->finished = true;
ecs_entity_t target = action->as.collectResource.entity;
ecs_delete(ECS, target);
}
}
void actionDepositResource(ecs_entity_t entity, Action *action, Game *game) {
if (action->elapsed > 0.2f)
action->finished = true;
}
void handleAction(ecs_entity_t entity, UnitAction *unitAction, Game *game) {
Action *action = unitAction->first;
if (action == NULL) return;
if (action->finished || action->failed) return;
switch (action->type) {
case ACTION_NONE:
break;
@@ -44,6 +54,12 @@ void handleAction(ecs_entity_t entity, UnitAction *unitAction, Game *game) {
BZ_ASSERT(0);
break;
}
}
void updateAction(UnitAction *unitAction, Game *game) {
Action *action = unitAction->first;
if (action == NULL) return;
if (action->failed) return;
action->elapsed += GetFrameTime();
if (action->finished) {
unitAction->first = action->next;
@@ -91,3 +107,16 @@ void addAction(ecs_entity_t entity, Game *game, const Action *action) {
unitAction->last = newAction;
}
}
const char *actionTypeToPrettyStr(ActionType type) {
switch (type) {
case ACTION_MOVE_TO:
return "MOVE TO";
case ACTION_COLLECT_RESOURCE:
return "COLLECT RESOURCE";
case ACTION_DEPOSIT_RESOURCE:
return "DEPOSIT RESOURCE";
default:
return "NONE";
}
}