-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBehaviorTreeAgent.cs
More file actions
118 lines (99 loc) · 3.09 KB
/
BehaviorTreeAgent.cs
File metadata and controls
118 lines (99 loc) · 3.09 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
// Copyright (c) 2020-2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Behavior-Tree
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Profiling;
using Zor.BehaviorTree.Core;
using Zor.BehaviorTree.Debugging;
using Zor.BehaviorTree.Serialization;
using Zor.SimpleBlackboard.Components;
namespace Zor.BehaviorTree.Components
{
/// <summary>
/// Creator and holder of a behavior tree.
/// </summary>
/// <remarks>
/// <para>Agent deserializes a tree on Awake.</para>
/// <para>Agent disposes a tree on OnDestroy.</para>
/// </remarks>
[AddComponentMenu("Behavior Tree/Behavior Tree Agent")]
public sealed class BehaviorTreeAgent : MonoBehaviour
{
#pragma warning disable CS0649
[SerializeField, Tooltip("Serialized behavior tree. It's automatically deserialized on Awake.")]
private SerializedBehaviorTree_Base m_SerializedBehaviorTree;
[SerializeField, Tooltip("Blackboard container. It's used as a blackboard for the behavior tree.")]
private SimpleBlackboardContainer m_BlackboardContainer;
#pragma warning restore CS0649
/// <summary>
/// <see cref="TreeRoot"/> of the deserialized and used behavior tree.
/// </summary>
private TreeRoot m_treeRoot;
/// <summary>
/// Cached <see cref="GameObject.name"/> to use in <see cref="Profiler.BeginSample(string)"/>.
/// </summary>
private string m_name;
/// <summary>
/// Calls <see cref="TreeRoot.Tick"/> of the internal behavior tree and returns its result.
/// </summary>
/// <returns>Result of the tick.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Status Tick()
{
Profiler.BeginSample(m_name);
Status status = m_treeRoot.Tick();
#if DEBUG
if (status == Status.Error)
{
BehaviorTreeDebug.LogError(this, $"[BehaviorTreeAgent] Behavior tree at {gameObject.name} finished a tick with an error");
}
#endif
Profiler.EndSample();
return status;
}
/// <summary>
/// Calls <see cref="TreeRoot.Abort"/> of the internal behavior tree and returns its result.
/// </summary>
/// <returns>Result of the abort.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining), ContextMenu("Abort")]
public Status Abort()
{
Profiler.BeginSample(m_name);
Status status = m_treeRoot.Abort();
Profiler.EndSample();
return status;
}
/// <summary>
/// Recreates a tree with the same serialized tree.
/// </summary>
/// <remarks>
/// This doesn't return a serialized tree or a blackboard to their original states.
/// </remarks>
[ContextMenu("Recreate Tree")]
public void RecreateTree()
{
Awake();
}
private void Awake()
{
#if DEBUG
if (m_SerializedBehaviorTree == null)
{
BehaviorTreeDebug.LogError(this, "Serialized behavior tree is null");
return;
}
if (m_BlackboardContainer == null)
{
BehaviorTreeDebug.LogError(this, "Blackboard container is null");
return;
}
#endif
m_treeRoot = m_SerializedBehaviorTree.CreateTree(m_BlackboardContainer.blackboard);
m_treeRoot.Initialize();
m_name = gameObject.name;
}
private void OnDestroy()
{
m_treeRoot?.Dispose();
}
}
}