58 lines
1.6 KiB
C
58 lines
1.6 KiB
C
#include "systems.h"
|
|
|
|
#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);
|
|
BzBTState *state = ecs_field(it, BzBTState, 1);
|
|
|
|
f32 dt = GetFrameTime();
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
ecs_entity_t entity = it->entities[i];
|
|
if (state[i].root == NULL) {
|
|
// No behaviour
|
|
continue;
|
|
}
|
|
if (ecs_has(ECS, entity, AIBlackboard)) {
|
|
AIBlackboard *blackboard = ecs_get_mut(ECS, entity, AIBlackboard);
|
|
blackboard->entity = entity;
|
|
state[i].userData = blackboard;
|
|
}
|
|
bzBTExecute(&state[i], dt);
|
|
}
|
|
}
|
|
|
|
void setAIBehaviour(ecs_entity_t entity, const BzBTNode *root,
|
|
const AIBlackboard *blackboard) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
if (ecs_has(ECS, entity, BzBTState)) {
|
|
BzBTState *prevState = ecs_get_mut(ECS, entity, BzBTState);
|
|
bzBTDestroyState(prevState);
|
|
}
|
|
|
|
ecs_set(ECS, entity, BzBTState, {
|
|
.root = root,
|
|
.nodeStatePool = game->pools.btNodeState,
|
|
});
|
|
AIBlackboard defaultBB = {
|
|
.entity = 0
|
|
};
|
|
if (blackboard) {
|
|
defaultBB = *blackboard;
|
|
}
|
|
defaultBB.entity = entity;
|
|
if (defaultBB.proximity < 2.0f)
|
|
defaultBB.proximity = 2.0f;
|
|
ecs_set_ptr(ECS, entity, AIBlackboard, &defaultBB);
|
|
}
|