Add initial Behavior simulation

This commit is contained in:
Wojtek Figat
2023-08-17 15:26:31 +02:00
parent d1e2d6699e
commit c18625e017
10 changed files with 444 additions and 10 deletions

View File

@@ -24,3 +24,51 @@ void BehaviorTreeCompoundNode::Init(BehaviorTree* tree)
for (BehaviorTreeNode* child : Children)
child->Init(tree);
}
BehaviorUpdateResult BehaviorTreeCompoundNode::Update(BehaviorUpdateContext context)
{
auto result = BehaviorUpdateResult::Success;
for (int32 i = 0; i < Children.Count() && result == BehaviorUpdateResult::Success; i++)
{
BehaviorTreeNode* child = Children[i];
result = child->Update(context);
}
return result;
}
int32 BehaviorTreeSequenceNode::GetStateSize() const
{
return sizeof(State);
}
void BehaviorTreeSequenceNode::InitState(Behavior* behavior, void* memory)
{
auto state = GetState<State>(memory);
new(state)State();
}
BehaviorUpdateResult BehaviorTreeSequenceNode::Update(BehaviorUpdateContext context)
{
auto state = GetState<State>(context.Memory);
if (state->CurrentChildIndex >= Children.Count())
return BehaviorUpdateResult::Success;
if (state->CurrentChildIndex == -1)
return BehaviorUpdateResult::Failed;
auto result = Children[state->CurrentChildIndex]->Update(context);
switch (result)
{
case BehaviorUpdateResult::Success:
state->CurrentChildIndex++; // Move to the next node
if (state->CurrentChildIndex < Children.Count())
result = BehaviorUpdateResult::Running; // Keep on running to the next child on the next update
break;
case BehaviorUpdateResult::Failed:
state->CurrentChildIndex = -1; // Mark whole sequence as failed
break;
}
return result;
}