-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeRoot.cs
More file actions
164 lines (142 loc) · 4 KB
/
Copy pathTreeRoot.cs
File metadata and controls
164 lines (142 loc) · 4 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2020-2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Behavior-Tree
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using UnityEngine.Profiling;
using Zor.SimpleBlackboard.Core;
namespace Zor.BehaviorTree.Core
{
/// <summary>
/// Root of a behavior tree.
/// </summary>
public sealed class TreeRoot : IDisposable
{
/// <summary>
/// Necessary define if it's possible to access a <see cref="Blackboard"/>
/// of a behavior tree from different threads.
/// </summary>
public const string MultithreadingDefine = "BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING";
/// <summary>
/// <see cref="Blackboard"/> of the behavior tree.
/// </summary>
[NotNull] private readonly Blackboard m_blackboard;
/// <summary>
/// Root <see cref="Behavior"/>.
/// </summary>
[NotNull] private readonly Behavior m_rootBehavior;
/// <param name="blackboard"><see cref="Blackboard"/> of the behavior tree.</param>
/// <param name="rootBehavior">Root <see cref="Behavior"/>.</param>
public TreeRoot([NotNull] Blackboard blackboard, [NotNull] Behavior rootBehavior)
{
m_blackboard = blackboard;
m_rootBehavior = rootBehavior;
m_rootBehavior.SetBlackboard(m_blackboard);
}
/// <summary>
/// <see cref="Blackboard"/> of the behavior tree.
/// </summary>
[NotNull]
public Blackboard blackboard
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_blackboard;
}
/// <summary>
/// Initializes the behavior tree.
/// </summary>
/// <remarks>
/// You must call it before a first tick.
/// </remarks>
#if !BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public void Initialize()
{
Profiler.BeginSample("TreeRoot.Initialize");
#if BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
lock (m_blackboard)
#endif
{
m_rootBehavior.Initialize();
}
Profiler.EndSample();
}
/// <summary>
/// Ticks the behavior tree and returns its result.
/// </summary>
/// <returns>Result of the tick.</returns>
/// <remarks>
/// You must call <see cref="Initialize"/> before a first tick.
/// </remarks>
#if !BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Status Tick()
{
Profiler.BeginSample("TreeRoot.Tick");
Status status;
#if BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
lock (m_blackboard)
#endif
{
status = m_rootBehavior.Tick();
}
Profiler.EndSample();
return status;
}
/// <summary>
/// Aborts a behavior tree if it's in <see cref="Status.Running"/> state.
/// </summary>
/// <returns>
/// <para>
/// New state (<see cref="Status.Abort"/>) if the behavior tree was in <see cref="Status.Running"/> state
/// before the call.
/// </para>
/// <para>
/// Current state if the behavior tree wasn't in <see cref="Status.Running"/> state before the call.
/// </para>
/// </returns>
#if !BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Status Abort()
{
Profiler.BeginSample("TreeRoot.Abort");
Status status;
#if BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
lock (m_blackboard)
#endif
{
status = m_rootBehavior.Abort();
}
Profiler.EndSample();
return status;
}
/// <summary>
/// Disposes the behavior tree.
/// </summary>
/// <remarks>
/// <para>You must call it before it's destroyed.</para>
/// <para>The method automatically calls <see cref="Abort"/>.</para>
/// </remarks>
#if !BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public void Dispose()
{
Profiler.BeginSample("TreeRoot.Dispose call");
#if BEHAVIOR_TREE_BLACKBOARD_MULTITHREADING
lock (m_blackboard)
#endif
{
Profiler.BeginSample("TreeRoot.Abort");
m_rootBehavior.Abort();
Profiler.EndSample();
Profiler.BeginSample("TreeRoot.Dispose");
m_rootBehavior.Dispose();
Profiler.EndSample();
}
Profiler.EndSample();
}
}
}