Files
PixelDefense/game/unit_actions.c
2023-12-17 09:06:14 +01:00

94 lines
2.7 KiB
C

#include "unit_actions.h"
#include "game_state.h"
#include "components.h"
#include "systems.h"
#include <raymath.h>
void actionMoveTo(ecs_entity_t entity, Action *action, Game *game) {
const Vector2 target = action->as.moveTo.target;
if (!ecs_has(ECS, entity, Path)) {
entitySetPath(entity, target, game);
return;
}
Vector2 pos = *ecs_get(ECS, entity, Position);
f32 dst = Vector2Distance(pos, target);
if (dst < action->as.moveTo.proximityThreshold) {
action->finished = true;
}
}
void actionCollectResource(ecs_entity_t entity, Action *action, Game *game) {
}
void actionDepositResource(ecs_entity_t entity, Action *action, Game *game) {
}
void handleAction(ecs_entity_t entity, UnitAction *unitAction, Game *game) {
Action *action = unitAction->first;
if (action == NULL) return;
switch (action->type) {
case ACTION_NONE:
break;
case ACTION_MOVE_TO:
actionMoveTo(entity, action, game);
break;
case ACTION_COLLECT_RESOURCE:
actionCollectResource(entity, action, game);
break;
case ACTION_DEPOSIT_RESOURCE:
actionDepositResource(entity, action, game);
break;
default:
BZ_ASSERT(0);
break;
}
action->elapsed += GetFrameTime();
if (action->finished) {
unitAction->first = action->next;
if (unitAction->last == action) {
unitAction->last = NULL;
BZ_ASSERT(unitAction->first == unitAction->last);
}
BzObjectPool *pool = game->pools.actions;
bzObjectPoolRelease(pool, action);
}
}
void clearActions(ecs_entity_t entity, Game *game) {
if (!ecs_has(ECS, entity, UnitAction)) return;
UnitAction *unitAction = ecs_get_mut(ECS, entity, UnitAction);
BzObjectPool *pool = game->pools.actions;
Action *pAction = unitAction->first;
while (pAction) {
bzObjectPoolRelease(pool, pAction);
pAction = pAction->next;
}
unitAction->first = NULL;
unitAction->last = NULL;
}
void addAction(ecs_entity_t entity, Game *game, const Action *action) {
BZ_ASSERT(action);
BZ_ASSERT(ecs_has(ECS, entity, UnitAction));
UnitAction *unitAction = ecs_get_mut(ECS, entity, UnitAction);
BzObjectPool *pool = game->pools.actions;
Action *newAction = bzObjectPool(pool);
BZ_ASSERT(newAction);
*newAction = *action;
newAction->next = NULL;
if (unitAction->last) {
Action *last = unitAction->last;
last->next = newAction;
unitAction->last = newAction;
} else {
unitAction->first = newAction;
unitAction->last = newAction;
}
}