NoJobEngine 2.0.7
Loading...
Searching...
No Matches
BTParallel.h
Go to the documentation of this file.
1
6#pragma once
7#include "BTNode.h"
8#include <memory>
9#include <stdexcept>
10namespace NoJob {
11// Main task and background task advance on the same frame, no threads.
12class BTParallel final : public BTNode {
13public:
14 BTParallel(BTNodeID id, std::unique_ptr<BTNode> mainTask, std::unique_ptr<BTNode> background)
15 : BTNode(id), m_Main(std::move(mainTask)), m_Background(std::move(background)) {
16 if (!m_Main || !m_Background) throw std::invalid_argument("Parallel needs two children");
17 }
18 std::string DebugLabel() const override { return "Simple Parallel"; }
19 BTStatus Tick(BTContext& ctx, float dt) const override {
20 const auto primary = m_Main->Tick(ctx, dt);
21 if (primary == BTStatus::Running) m_Background->Tick(ctx, dt);
22 else Reset(ctx);
23 ctx.RecordStatus(GetID(), static_cast<int>(primary));
24 return primary;
25 }
26 void Reset(BTContext& ctx) const override {
27 m_Main->Reset(ctx); m_Background->Reset(ctx); BTNode::Reset(ctx);
28 }
29 void CollectChildren(std::vector<const BTNode*>& out) const override {
30 out.push_back(m_Main.get()); out.push_back(m_Background.get());
31 }
32private:
33 std::unique_ptr<BTNode> m_Main, m_Background;
34};
35}
Base interface for behavior tree nodes.
void RecordStatus(BTNodeID id, int status)
Definition BTContext.h:77
virtual void Reset(BTContext &context) const
Definition BTNode.h:28
BTNode(BTNodeID id)
Definition BTNode.h:17
BTNodeID GetID() const
Definition BTNode.h:21
BTParallel(BTNodeID id, std::unique_ptr< BTNode > mainTask, std::unique_ptr< BTNode > background)
Definition BTParallel.h:14
void CollectChildren(std::vector< const BTNode * > &out) const override
Definition BTParallel.h:29
std::string DebugLabel() const override
Definition BTParallel.h:18
BTStatus Tick(BTContext &ctx, float dt) const override
Evaluates this node for one update.
Definition BTParallel.h:19
void Reset(BTContext &ctx) const override
Definition BTParallel.h:26
std::uint64_t BTNodeID
Definition BTContext.h:19
BTStatus
Result of a behavior tree node tick.
Definition BTNode.h:13