Add Delay node to BT

This commit is contained in:
Wojtek Figat
2023-08-20 21:42:43 +02:00
parent 2e9facc429
commit fce82247ab
2 changed files with 57 additions and 0 deletions

View File

@@ -1,7 +1,9 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#include "BehaviorTreeNodes.h"
#include "Behavior.h"
#include "BehaviorKnowledge.h"
#include "Engine/Core/Random.h"
#include "Engine/Serialization/Serialization.h"
BehaviorUpdateResult BehaviorTreeNode::InvokeUpdate(const BehaviorUpdateContext& context)
@@ -96,3 +98,23 @@ BehaviorUpdateResult BehaviorTreeSequenceNode::Update(BehaviorUpdateContext cont
return result;
}
int32 BehaviorTreeDelayNode::GetStateSize() const
{
return sizeof(State);
}
void BehaviorTreeDelayNode::InitState(Behavior* behavior, void* memory)
{
auto state = GetState<State>(memory);
if (!WaitTimeSelector.TryGet(behavior->GetKnowledge(), state->TimeLeft))
state->TimeLeft = WaitTime;
state->TimeLeft = Random::RandRange(Math::Max(WaitTime - RandomDeviation, 0.0f), WaitTime + RandomDeviation);
}
BehaviorUpdateResult BehaviorTreeDelayNode::Update(BehaviorUpdateContext context)
{
auto state = GetState<State>(context.Memory);
state->TimeLeft -= context.DeltaTime;
return state->TimeLeft <= 0.0f ? BehaviorUpdateResult::Success : BehaviorUpdateResult::Running;
}