Ember
Loading...
Searching...
No Matches
DefaultTreeFactory.cpp
Go to the documentation of this file.
2#include "Core/BehaviorTree.h"
3#include "Core/Node.h"
4#include <memory>
5
6namespace EmberCore {
7
8std::shared_ptr<ITreeStructure> DefaultTreeFactory::CreateTree(const EmberCore::String &name) {
9 // Create a BehaviorTree with a root node
10 auto behaviorTree = std::make_shared<BehaviorTree>(name);
11 auto root = std::make_shared<Node>(name, Node::Type::Control);
12 behaviorTree->SetRootNode(root);
13
14 // Create DirectTreeAdapter that wraps BehaviorTree
15 auto adapter = std::make_shared<DirectTreeAdapter>(behaviorTree);
16
17 return adapter;
18}
19
20std::shared_ptr<ITreeNode> DefaultTreeFactory::CreateNode(const EmberCore::String &name, ITreeNode::NodeType type) {
21 // Convert the abstract node type to the concrete Node::Type
22 Node::Type concrete_type = ConvertNodeType(type);
23
24 // Create a new Node using the current implementation
25 std::unique_ptr<Node> node;
26 switch (concrete_type) {
29 break;
32 break;
35 break;
38 break;
39 default:
40 node = std::make_unique<Node>(name, Node::Type::None);
41 break;
42 }
43
44 // Create an adapter that wraps the node
45 auto adapter = std::make_shared<NodeAdapter>(node.get());
46
47 // Store the node in the adapter to maintain ownership
48 // Note: This is a simplified approach. In a real implementation,
49 // you might want to use a more sophisticated ownership model
50
51 return adapter;
52}
53
54std::shared_ptr<ITreeStructure> DefaultTreeFactory::CreateSampleTree() {
55 // Create a BehaviorTree with sample data
56 auto behaviorTree = std::make_shared<BehaviorTree>("Sample Tree");
57 auto sampleTree = NodeFactory::CreateSampleTree();
58
59 // Convert unique_ptr to shared_ptr and set in BehaviorTree
60 auto sharedTree = std::shared_ptr<Node>(sampleTree.release());
61 behaviorTree->SetRootNode(sharedTree);
62
63 // Create DirectTreeAdapter that wraps BehaviorTree
64 auto adapter = std::make_shared<DirectTreeAdapter>(behaviorTree);
65
66 return adapter;
67}
68
83
98
99} // namespace EmberCore
Node::Type ConvertNodeType(ITreeNode::NodeType type) const
std::shared_ptr< ITreeNode > CreateNode(const EmberCore::String &name, ITreeNode::NodeType type) override
Create a new node.
std::shared_ptr< ITreeStructure > CreateTree(const EmberCore::String &name="New Tree") override
Create a new empty behavior tree.
std::shared_ptr< ITreeStructure > CreateSampleTree() override
Create a sample behavior tree for testing.
NodeType
Node types for visualization categorization.
Definition ITreeNode.h:36
static std::unique_ptr< Node > CreateConditionNode(const String &name)
Definition Node.cpp:408
static std::unique_ptr< Node > CreateSampleTree()
Definition Node.cpp:420
static std::unique_ptr< Node > CreateActionNode(const String &name)
Definition Node.cpp:400
static std::unique_ptr< Node > CreateControlNode(const String &name)
Definition Node.cpp:404
static std::unique_ptr< Node > CreateDecoratorNode(const String &name)
Definition Node.cpp:412
Type
Node types for behavior tree classification.
Definition Node.h:25
Main types header for EmberCore.
std::string String
Framework-agnostic string type.
Definition String.h:14