-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtilityAIAgent.cs
More file actions
98 lines (84 loc) · 2.49 KB
/
UtilityAIAgent.cs
File metadata and controls
98 lines (84 loc) · 2.49 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
// Copyright (c) 2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/UtilityAI
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Profiling;
using Zor.SimpleBlackboard.Components;
using Zor.UtilityAI.Core;
using Zor.UtilityAI.Debugging;
using Zor.UtilityAI.Serialization;
namespace Zor.UtilityAI.Components
{
/// <summary>
/// Creator and holder of <see cref="Brain"/>.
/// </summary>
/// <remarks>
/// <para>Agent deserializes a <see cref="Brain"/> on Awake.</para>
/// <para>Agent disposes a <see cref="Brain"/> on OnDestroy.</para>
/// </remarks>
[AddComponentMenu("Utility AI/Utility AI Agent")]
public sealed class UtilityAIAgent : MonoBehaviour
{
[SerializeField, Tooltip("Serialized brain. It's automatically deserialized on Awake.")]
private SerializedBrain_Base m_SerializedBrain;
[SerializeField, Tooltip("Blackboard container. It's used as a blackboard for the brain.")]
private SimpleBlackboardContainer m_BlackboardContainer;
/// <summary>
/// Deserialized <see cref="Brain"/>.
/// </summary>
private Brain m_brain;
/// <summary>
/// Cached <see cref="GameObject.name"/> to use in <see cref="Profiler.BeginSample(string)"/>.
/// </summary>
private string m_name;
/// <summary>
/// Ticks a holden <see cref="Brain"/>.
/// </summary>
public void Tick()
{
Profiler.BeginSample(m_name);
m_brain.Tick();
Profiler.EndSample();
}
/// <summary>
/// Fills debug info of a holden <see cref="Brain"/> into <paramref name="brainDebugInfo"/>.
/// </summary>
/// <param name="brainDebugInfo">Brain debug info.</param>
public void FillDebugInfo([NotNull] BrainDebugInfo brainDebugInfo)
{
m_brain?.FillDebugInfo(brainDebugInfo);
}
/// <summary>
/// Recreates a <see cref="Brain"/> with the same serialized brain.
/// </summary>
/// <remarks>
/// This doesn't return a serialized brain or a blackboard to their original states.
/// </remarks>
[ContextMenu("Recreate Brain")]
public void RecreateBrain()
{
Awake();
}
private void Awake()
{
#if DEBUG
if (m_SerializedBrain == null)
{
UtilityAIDebug.LogError(this, "Serialized brain is null");
return;
}
if (m_BlackboardContainer == null)
{
UtilityAIDebug.LogError(this, "Blackboard container is null");
return;
}
#endif
m_brain = m_SerializedBrain.CreateBrain(m_BlackboardContainer.blackboard);
m_brain.Initialize();
m_name = gameObject.name;
}
private void OnDestroy()
{
m_brain?.Dispose();
}
}
}