forked from AtomicGameEngine/AtomicGameEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cs
More file actions
231 lines (183 loc) · 7.56 KB
/
Node.cs
File metadata and controls
231 lines (183 loc) · 7.56 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using static System.Reflection.IntrospectionExtensions;
namespace AtomicEngine
{
public partial class Node : Animatable
{
/// <summary>
/// Whether the node has been explicitly destroyed
/// </summary>
public bool Destroyed { get; private set; }
/// <summary>
/// Explicitly removes node from hierarchy and disposes of children and components
/// </summary>
public virtual void Destroy()
{
// Destroying node hierarchy is flattened and not recursive
// for performance considerations
if (Destroyed)
return;
Destroyed = true;
UnsubscribeFromAllEvents();
// Remove from hierarchy
Remove();
// list of nodes/components to dispose
var disposeList = new List<RefCounted>();
// IMPORTANT: Care must be taken to clear these vectors
// otherwise, references will be held until the Vector is GC'd
// and the child nodes/components/resources will not be immediately disposed
var nodes = new Vector<Node>();
var components = new Vector<Component>();
// Get node components and add to dispose list
GetComponents(components);
disposeList.AddRange(components);
components.Clear();
// get all children of node and add their components to the dispose list
GetChildren(nodes, true);
foreach (var node in nodes)
{
node.Destroyed = true;
node.GetComponents(components);
disposeList.AddRange(components);
components.Clear();
}
// add nodes to the back of the list
disposeList.AddRange(nodes);
nodes.Clear();
// Add ourself to the dispose list
disposeList.Add(this);
// dispose of list
RefCountedCache.Dispose(disposeList);
}
public void RemoveComponent<T>() where T : Component
{
var type = typeof(T);
var name = type.Name;
// TODO: This doesn't allow removing of a specific component type derived from CSComponent
if (type.GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
{
name = "CSComponent";
throw new InvalidOperationException("Node.RemoveComponent - Add functionality to remove specific CSComponent derived instances");
}
RemoveComponent(name);
}
public T CreateComponent<T>(CreateMode mode = CreateMode.REPLICATED, uint id = 0) where T : Component
{
var type = typeof(T);
if (type.GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
{
Component component = (Component)Activator.CreateInstance(type);
AddComponent(component, id, mode);
return (T)component;
}
return (T)CreateComponent(type.Name, mode, id);
}
public void AddComponent(Component component)
{
var type = component.GetType();
AddComponent(component, 0, CreateMode.REPLICATED);
}
public void GetChildrenWithComponent<T>(Vector<Node> dest, bool recursive = false)
{
var type = typeof(T);
// If we're a CSComponent, get "CSComponents" native side and filter here
if (type.GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
{
Vector<Node> temp = new Vector<Node>();
GetChildrenWithComponent(temp, "CSComponent", recursive);
// filter based on actual type
for (uint i = 0; i < temp.Size; i++)
{
var node = temp[i];
Vector<Component> components = new Vector<Component>();
node.GetComponents(components, "Component", false);
for (uint j = 0; j < components.Size; j++)
{
if (components[j].GetType() == type)
{
dest.Push(node);
break;
}
}
}
return;
}
GetChildrenWithComponent(dest, type.Name, recursive);
}
public T GetComponent<T>(bool recursive = false) where T : Component
{
return (T)GetComponent(typeof(T).Name, recursive);
}
public void GetComponents<T>(Vector<T> dest, bool recursive = false) where T : Component
{
dest.Clear();
GetComponents(ComponentVector, typeof(T).Name, recursive);
for (int i = 0; i < ComponentVector.Size; i++)
{
if (ComponentVector[i] != null)
dest.Push((T)ComponentVector[i]);
}
ComponentVector.Clear();
}
public void GetDerivedComponents<T>(Vector<T> dest, bool recursive = false) where T : Component
{
dest.Clear();
GetComponents(ComponentVector, typeof(Component).Name, recursive);
for (int i = 0; i < ComponentVector.Size; ++i)
{
T t = ComponentVector[i] as T;
if (t != null)
dest.Push(t);
}
ComponentVector.Clear();
}
public T GetCSComponent<T>(bool recursive = false) where T : CSComponent
{
GetComponents(ComponentVector, nameof(CSComponent), recursive);
for (int i = 0; i < ComponentVector.Size; i++)
{
Component component = ComponentVector[i];
if (component != null &&
component.GetType() == typeof(T))
{
ComponentVector.Clear();
return (T)component;
}
}
ComponentVector.Clear();
return null;
}
public void GetCSComponents<T>(Vector<T> dest, bool recursive = false) where T : CSComponent
{
dest.Clear();
GetComponents(ComponentVector, nameof(CSComponent), recursive);
for (int i = 0; i < ComponentVector.Size; i++)
{
Component component = ComponentVector[i];
if (component != null &&
component.GetType() == typeof(T))
dest.Push((T)component);
}
ComponentVector.Clear();
}
public void GetDerivedCSComponents<T>(Vector<T> dest, bool recursive = false) where T : CSComponent
{
dest.Clear();
GetComponents(ComponentVector, nameof(CSComponent), recursive);
for (int i = 0; i < ComponentVector.Size; ++i)
{
T t = ComponentVector[i] as T;
if (t != null)
dest.Push(t);
}
ComponentVector.Clear();
}
// Reuse vectors to avoid churn, but don't have one on every referenced node
// Wrapping in a static property, instead of just immediate static allocation,
// Because Runtime isn't ready at time of static initialization:
// https://github.com/AtomicGameEngine/AtomicGameEngine/issues/1512
private static Vector<Component> ComponentVector => lazyComponentVector.Value;
private static Lazy<Vector<Component>> lazyComponentVector = new Lazy<Vector<Component>>();
}
}