NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
BTService.h
Go to the documentation of this file.
1
6
#pragma once
7
#include "
BTNativeRegistry.h
"
8
#include <memory>
9
#include <stdexcept>
10
#include <string>
11
namespace
NoJob
{
12
// Periodic service while its branch is active; action is executed on a timer.
13
class
BTService
final :
public
BTNode
{
14
public
:
15
BTService
(
BTNodeID
id
, std::shared_ptr<const BTNativeRegistry> registry,
16
std::string action,
float
interval, std::unique_ptr<BTNode> child)
17
:
BTNode
(id), m_Registry(std::move(registry)), m_Action(std::move(action)),
18
m_Interval(interval), m_Child(std::move(child)) {
19
if
(!m_Child || !m_Registry || !m_Registry->HasAction(m_Action) || interval <= 0)
20
throw
std::invalid_argument(
"Invalid BT service"
);
21
}
22
std::string
DebugLabel
()
const override
{
return
"Service: "
+ m_Action; }
23
BTStatus
Tick
(
BTContext
& ctx,
float
dt)
const override
{
24
auto
& state = ctx.
GetNodeState
(
GetID
());
25
// Time is stored per-agent; TickCount holds milliseconds of elapsed time.
26
const
auto
elapsed = ctx.
Blackboard
().
GetOr
<
float
>(TimerKey(), 0.f) + dt;
27
if
(elapsed >= m_Interval) {
28
ctx.
Blackboard
().
Set
(TimerKey(), 0.f);
29
m_Registry->GetAction(m_Action)(ctx, dt);
30
}
else
ctx.
Blackboard
().
Set
(TimerKey(), elapsed);
31
++state.TickCount;
32
auto
status = m_Child->Tick(ctx, dt);
33
ctx.
RecordStatus
(
GetID
(),
static_cast<
int
>
(status));
34
if
(status !=
BTStatus::Running
)
Reset
(ctx);
35
return
status;
36
}
37
void
Reset
(
BTContext
& ctx)
const override
{
38
m_Child->Reset(ctx);
39
ctx.
Blackboard
().
Remove
(TimerKey());
40
BTNode::Reset
(ctx);
41
}
42
void
CollectChildren
(std::vector<const BTNode*>& out)
const override
{ out.push_back(m_Child.get()); }
43
private
:
44
std::string TimerKey()
const
{
return
"__bt_service_"
+ std::to_string(
GetID
()); }
45
std::shared_ptr<const BTNativeRegistry> m_Registry;
46
std::string m_Action;
47
float
m_Interval;
48
std::unique_ptr<BTNode> m_Child;
49
};
50
}
BTNativeRegistry.h
Registry for native behavior tree callbacks.
NoJob::BTBlackboard::Set
void Set(std::string key, T &&value)
Definition
BTBlackboard.h:28
NoJob::BTBlackboard::GetOr
T GetOr(std::string_view key, T fallback) const
Definition
BTBlackboard.h:75
NoJob::BTBlackboard::Remove
bool Remove(std::string_view key)
Definition
BTBlackboard.h:84
NoJob::BTContext
Definition
BTContext.h:24
NoJob::BTContext::Blackboard
BTBlackboard & Blackboard()
Definition
BTContext.h:90
NoJob::BTContext::RecordStatus
void RecordStatus(BTNodeID id, int status)
Definition
BTContext.h:77
NoJob::BTContext::GetNodeState
BTNodeRuntimeState & GetNodeState(BTNodeID id)
Definition
BTContext.h:44
NoJob::BTNode::Reset
virtual void Reset(BTContext &context) const
Definition
BTNode.h:28
NoJob::BTNode::BTNode
BTNode(BTNodeID id)
Definition
BTNode.h:17
NoJob::BTNode::GetID
BTNodeID GetID() const
Definition
BTNode.h:21
NoJob::BTService::DebugLabel
std::string DebugLabel() const override
Definition
BTService.h:22
NoJob::BTService::Reset
void Reset(BTContext &ctx) const override
Definition
BTService.h:37
NoJob::BTService::BTService
BTService(BTNodeID id, std::shared_ptr< const BTNativeRegistry > registry, std::string action, float interval, std::unique_ptr< BTNode > child)
Definition
BTService.h:15
NoJob::BTService::Tick
BTStatus Tick(BTContext &ctx, float dt) const override
Evaluates this node for one update.
Definition
BTService.h:23
NoJob::BTService::CollectChildren
void CollectChildren(std::vector< const BTNode * > &out) const override
Definition
BTService.h:42
NoJob
Definition
BTActionNode.h:13
NoJob::BTNodeID
std::uint64_t BTNodeID
Definition
BTContext.h:19
NoJob::BTStatus
BTStatus
Result of a behavior tree node tick.
Definition
BTNode.h:13
NoJob::BTStatus::Running
@ Running
Definition
BTNode.h:13
Engine
AI
BehaviorTree
BTService.h
Generated by
1.18.0