forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
80 lines (65 loc) · 1.75 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <behavior_tree.h>
class MyCondition : public BT::ConditionNode
{
public:
MyCondition(const std::string& name);
~MyCondition();
BT::ReturnStatus Tick();
};
MyCondition::MyCondition(const std::string& name) : BT::ConditionNode::ConditionNode(name)
{
}
BT::ReturnStatus MyCondition::Tick()
{
std::cout << "The Condition is true" << std::endl;
return NodeStatus::SUCCESS;
}
class MyAction : public BT::ActionNode
{
public:
MyAction(const std::string& name);
~MyAction();
BT::ReturnStatus Tick();
void Halt();
};
MyAction::MyAction(const std::string& name) : ActionNode::ActionNode(name)
{
}
BT::ReturnStatus MyAction::Tick()
{
std::cout << "The Action is doing some operations" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (is_halted())
{
return NodeStatus::IDLE;
}
std::cout << "The Action is doing some others operations" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (is_halted())
{
return NodeStatus::IDLE;
}
std::cout << "The Action is doing more operations" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (is_halted())
{
return NodeStatus::IDLE;
}
std::cout << "The Action has succeeded" << std::endl;
return NodeStatus::SUCCESS;
}
void MyAction::Halt()
{
}
int main(int argc, char* argv[])
{
BT::SequenceNode* seq = new BT::SequenceNode("Sequence");
MyCondition* my_con_1 = new MyCondition("Condition");
MyAction* my_act_1 = new MyAction("Action");
int tick_time_milliseconds = 1000;
seq->AddChild(my_con_1);
seq->AddChild(my_act_1);
Execute(seq, tick_time_milliseconds);
return 0;
}