Add Selector node to BT
This commit is contained in:
@@ -99,6 +99,40 @@ BehaviorUpdateResult BehaviorTreeSequenceNode::Update(BehaviorUpdateContext cont
|
||||
return result;
|
||||
}
|
||||
|
||||
int32 BehaviorTreeSelectorNode::GetStateSize() const
|
||||
{
|
||||
return sizeof(State);
|
||||
}
|
||||
|
||||
void BehaviorTreeSelectorNode::InitState(Behavior* behavior, void* memory)
|
||||
{
|
||||
auto state = GetState<State>(memory);
|
||||
new(state)State();
|
||||
}
|
||||
|
||||
BehaviorUpdateResult BehaviorTreeSelectorNode::Update(BehaviorUpdateContext context)
|
||||
{
|
||||
auto state = GetState<State>(context.Memory);
|
||||
|
||||
if (state->CurrentChildIndex >= Children.Count())
|
||||
return BehaviorUpdateResult::Failed;
|
||||
|
||||
auto result = Children[state->CurrentChildIndex]->InvokeUpdate(context);
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case BehaviorUpdateResult::Success:
|
||||
return BehaviorUpdateResult::Success;
|
||||
case BehaviorUpdateResult::Failed:
|
||||
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;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int32 BehaviorTreeDelayNode::GetStateSize() const
|
||||
{
|
||||
return sizeof(State);
|
||||
|
||||
@@ -45,6 +45,26 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Selector node updates all its children (from left to right) until one of them succeeds. If all children fail, the selector fails.
|
||||
/// </summary>
|
||||
API_CLASS() class FLAXENGINE_API BehaviorTreeSelectorNode : public BehaviorTreeCompoundNode
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(BehaviorTreeSelectorNode, BehaviorTreeCompoundNode);
|
||||
|
||||
public:
|
||||
// [BehaviorTreeNode]
|
||||
int32 GetStateSize() const override;
|
||||
void InitState(Behavior* behavior, void* memory) override;
|
||||
BehaviorUpdateResult Update(BehaviorUpdateContext context) override;
|
||||
|
||||
private:
|
||||
struct State
|
||||
{
|
||||
int32 CurrentChildIndex = 0;
|
||||
};
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Root node of the behavior tree. Contains logic properties and definitions for the runtime.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user