-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackboardContainerComponent.cs
140 lines (123 loc) · 4.44 KB
/
BlackboardContainerComponent.cs
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
// Copyright (c) 2019-2020 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Event-Based-Blackboard
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using UnityEngine;
using Zor.EventBasedBlackboard.Core;
using Zor.EventBasedBlackboard.Debugging;
using Zor.EventBasedBlackboard.Serialization;
namespace Zor.EventBasedBlackboard.Components.Main
{
/// <summary>
/// Container of <see cref="Zor.EventBasedBlackboard.Core.Blackboard"/> for using that as
/// <see cref="UnityEngine.Component"/>.
/// </summary>
/// <remarks>
/// As <see cref="Zor.EventBasedBlackboard.Core.Blackboard"/> requires flushing to update its values, use any
/// <see cref="Zor.EventBasedBlackboard.Components.Main.Flushers.BlackboardContainerFlusher"/> to call
/// <see cref="Zor.EventBasedBlackboard.Core.Blackboard.Flush"/> of
/// <see cref="Zor.EventBasedBlackboard.Core.Blackboard"/> in this component.
/// </remarks>
[AddComponentMenu(BlackboardAddComponentConstants.MainFolder + "Blackboard Container")]
public sealed class BlackboardContainerComponent : MonoBehaviour
{
#pragma warning disable CS0649
[SerializeField, Tooltip("Array of serialized properties for Blackboard.\nIt is applied to Blackboard only on Awake.")]
private SerializedContainer[] m_SerializedContainers;
#pragma warning restore CS0649
private Blackboard m_blackboard;
/// <summary>
/// Contained <see cref="Zor.EventBasedBlackboard.Core.Blackboard"/>.
/// </summary>
[NotNull]
public Blackboard blackboard
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_blackboard;
}
/// <summary>
/// How many serialized containers this <see cref="BlackboardContainerComponent"/> depends on.
/// </summary>
public int serializedContainersCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_SerializedContainers.Length;
}
/// <summary>
/// Gets a <see cref="SerializedContainer"/> at the index <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns><see cref="SerializedContainer"/> at the index <paramref name="index"/>.</returns>
/// <remarks>
/// If you change a gotten <see cref="SetSerializedContainer"/>,
/// you need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), NotNull, Pure]
public SerializedContainer GetSerializedContainer(int index)
{
return m_SerializedContainers[index];
}
/// <summary>
/// Sets the serialized container <paramref name="serializedContainer"/> at the index <paramref name="index"/>
/// </summary>
/// <param name="serializedContainer"></param>
/// <param name="index"></param>
/// <remarks>
/// You need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSerializedContainer([NotNull] SerializedContainer serializedContainer, int index)
{
m_SerializedContainers[index] = serializedContainer;
}
/// <summary>
/// Sets the serialized containers <paramref name="serializedContainers"/>.
/// </summary>
/// <param name="serializedContainers"></param>
/// <remarks>
/// You need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSerializedContainers([NotNull] SerializedContainer[] serializedContainers)
{
m_SerializedContainers = serializedContainers;
}
/// <summary>
/// Creates a new <see cref="Blackboard"/> and applies current serialized containers to it.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining), ContextMenu("Recreate Blackboard")]
public void RecreateBlackboard()
{
Awake();
}
private void Awake()
{
m_blackboard = new Blackboard();
for (int i = 0, count = m_SerializedContainers.Length; i < count; ++i)
{
SerializedContainer container = m_SerializedContainers[i];
if (container == null)
{
BlackboardDebug.LogWarning($"[BlackboardContainerComponent] SerializedContainer at index '{i}' is null", this);
continue;
}
container.Apply(m_blackboard);
}
m_blackboard.Flush();
}
[ContextMenu("Flush All")]
private void FlushAll()
{
m_blackboard.Flush();
}
[ContextMenu("Flush Single Pass")]
private void FlushSinglePass()
{
m_blackboard.Flush(true);
}
[ContextMenu("Log")]
private void Log()
{
Debug.Log(m_blackboard.ToString(), this);
}
}
}