forked from AtomicGameEngine/AtomicGameEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cs
More file actions
397 lines (288 loc) · 11.4 KB
/
Scene.cs
File metadata and controls
397 lines (288 loc) · 11.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using static System.Reflection.IntrospectionExtensions;
namespace AtomicEngine
{
public partial class Scene : Node
{
/// <summary>
/// Explicitly destroys scene and disposes of children and components
/// </summary>
public override void Destroy()
{
if (Destroyed)
return;
cscomponents.Clear();
cscomponentStart.Clear();
base.Destroy();
}
internal override void PostNativeUpdate()
{
SubscribeToEvent<NodeAddedEvent>(this, e =>
{
//Log.Info($"Node ADDED: {e.Node.Name}");
// The NodeAdded event is generated when adding a node as a child
e.Node.GetComponents<CSComponent>(csComponentVector);
for (uint i = 0; i < csComponentVector.Size; i++)
{
AddCSComponent(csComponentVector[i]);
}
csComponentVector.Clear();
});
SubscribeToEvent<NodeRemovedEvent>(this, e =>
{
//Log.Info($"Node REMOVED: {e.Node.Name}");
// The NodeRemoved event is generated when explicitly removing nodes from a scene
// For general cleanup, it will not be generated
e.Node.GetComponents<CSComponent>(csComponentVector);
for (uint i = 0; i < csComponentVector.Size; i++)
{
HandleComponentRemoved(csComponentVector[i]);
}
csComponentVector.Clear();
});
SubscribeToEvent<CSComponentLoadEvent>(this, HandleCSComponentLoad);
SubscribeToEvent<ComponentAddedEvent>(this, e =>
{
Component component = null;
try
{
// will throw if component isn't a known native
component = e.Component;
}
catch
{
return;
}
HandleComponentAdded(component);
});
SubscribeToEvent<ComponentRemovedEvent>(this, e =>
{
Component component = null;
try
{
// will throw if component isn't a known native
component = e.Component;
}
catch
{
return;
}
HandleComponentRemoved(component);
});
// Update variable timestep logic
SubscribeToEvent<SceneUpdateEvent>(this, HandleSceneUpdate);
// Update scene subsystems. If a physics world is present, it will be updated, triggering fixed timestep logic updates
SubscribeToEvent<SceneSubsystemUpdateEvent>(this, HandleSceneSubsystemUpdate);
// Update transform smoothing
SubscribeToEvent<UpdateSmoothingEvent>(this, HandleUpdateSmoothing);
// Post-update variable timestep logic
SubscribeToEvent<ScenePostUpdateEvent>(this, HandleScenePostUpdate);
}
// Update variable timestep logic
void HandleSceneUpdate(SceneUpdateEvent e)
{
// Handle Start
if (cscomponentStart.Count > 0)
{
var started = new List<CSComponent>();
foreach (var csc in cscomponentStart.ToList())
{
if (!csc.IsEnabled())
{
continue;
}
// mark as started whether or not a Start method exists
csc.started = true;
started.Add(csc);
CSComponentInfo info;
if (CSComponentCore.csinfoLookup.TryGetValue(csc.GetType(), out info))
{
if (info.StartMethod != null)
{
info.StartMethod.Invoke(csc, null);
}
}
}
foreach (var csc in started)
{
cscomponentStart.Remove(csc);
}
}
// Handle Scene Update
Object[] args = new Object[1] { e.TimeStep };
foreach (var item in cscomponents.ToList())
{
var info = item.Key;
var UpdateMethod = info.UpdateMethod;
if (UpdateMethod == null)
continue;
foreach (var csc in item.Value.ToList())
{
if (!csc.Started || !csc.IsEnabled())
continue;
UpdateMethod.Invoke(csc, args);
}
}
}
// Update scene subsystems. If a physics world is present, it will be updated, triggering fixed timestep logic updates
void HandleSceneSubsystemUpdate(SceneSubsystemUpdateEvent e)
{
}
// Update transform smoothing
void HandleUpdateSmoothing(UpdateSmoothingEvent e)
{
}
// Post-update variable timestep logic
void HandleScenePostUpdate(ScenePostUpdateEvent e)
{
Object[] args = new Object[1] { e.TimeStep };
foreach (var item in cscomponents)
{
var info = item.Key;
var PostUpdateMethod = info.PostUpdateMethod;
if (PostUpdateMethod == null)
continue;
foreach (var csc in item.Value)
{
if (!csc.Started || !csc.IsEnabled())
continue;
PostUpdateMethod.Invoke(csc, args);
}
}
}
void HandlePhysicsPreStep(PhysicsPreStepEvent e)
{
Object[] args = new Object[1] { e.TimeStep };
foreach (var item in cscomponents)
{
var info = item.Key;
var PhysicsPreStepMethod = info.PhysicsPreStepMethod;
if (PhysicsPreStepMethod == null)
continue;
foreach (var csc in item.Value)
{
if (!csc.Started || !csc.IsEnabled())
continue;
PhysicsPreStepMethod.Invoke(csc, args);
}
}
}
void HandlePhysicsPostStep(PhysicsPostStepEvent e)
{
Object[] args = new Object[1] { e.TimeStep };
foreach (var item in cscomponents)
{
var info = item.Key;
var PhysicsPostStepMethod = info.PhysicsPostStepMethod;
if (PhysicsPostStepMethod == null)
continue;
foreach (var csc in item.Value)
{
if (!csc.Started || !csc.IsEnabled())
continue;
PhysicsPostStepMethod.Invoke(csc, args);
}
}
}
void HandleCSComponentLoad(CSComponentLoadEvent e)
{
var scriptMap = e.scriptMap;
// Get the NativeInstance as an IntPtr, otherwise we would be wrapping as a CSComponent
IntPtr csnative = scriptMap.GetVoidPtr("NativeInstance");
IntPtr fieldValues = IntPtr.Zero;
if (scriptMap.Contains("FieldValues"))
fieldValues = scriptMap.GetVoidPtr("FieldValues");
CSComponentInfo csinfo;
if (!CSComponentCore.componentCache.TryGetValue(e.ClassName, out csinfo))
{
Log.Error("Scene.HandleCSComponentLoad - unable to find CSComponent in cache for classname: " + e.ClassName );
return;
}
var component = CSComponent.LoadCreateInstance(csinfo.Type, csnative);
if (component == null)
{
Log.Error("Scene.HandleCSComponentLoad - unable to create CSComponent for classname: " + e.ClassName);
return;
}
if (fieldValues != IntPtr.Zero)
csinfo.ApplyFieldValues(component, fieldValues);
AddCSComponent(component);
}
void AddCSComponent(CSComponent csc)
{
CSComponentInfo info;
if (!CSComponentCore.csinfoLookup.TryGetValue(csc.GetType(), out info))
{
Log.Error("Scene.HandleComponentAdded - unable to get CSComponentInfo");
return;
}
List<CSComponent> cslist;
if (!cscomponents.TryGetValue(info, out cslist))
{
cslist = cscomponents[info] = new List<CSComponent>();
}
if (cslist.Contains(csc))
{
throw new InvalidOperationException("Scene.HandleComponentAdded - CSComponent already added to component list");
}
cslist.Add(csc);
if (!csc.started)
{
if (cscomponentStart.Contains(csc))
{
throw new InvalidOperationException("Scene.HandleComponentAdded CSComponent already added to start list");
}
cscomponentStart.Add(csc);
}
}
void HandleComponentAdded(Component component)
{
// Check null (CSComponent) or other abstract component
if (component == null)
{
return;
}
if (component.GetType() == typeof(PhysicsWorld) || component.GetType() == typeof(PhysicsWorld2D))
{
SubscribeToEvent<PhysicsPreStepEvent>(component, HandlePhysicsPreStep);
SubscribeToEvent<PhysicsPostStepEvent>(component, HandlePhysicsPostStep);
}
// CSComponent
if (component.GetType().GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
{
var csc = (CSComponent)component;
AddCSComponent(csc);
}
}
void HandleComponentRemoved(Component component)
{
if (component.GetType() == typeof(PhysicsWorld) || component.GetType() == typeof(PhysicsWorld2D))
{
UnsubscribeFromEvent<PhysicsPreStepEvent>();
UnsubscribeFromEvent<PhysicsPostStepEvent>();
}
if (component.GetType().GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
{
var csc = (CSComponent)component;
CSComponentInfo info;
if (!CSComponentCore.csinfoLookup.TryGetValue(csc.GetType(), out info))
{
return;
}
cscomponentStart.Remove(csc);
List<CSComponent> cslist;
if (!cscomponents.TryGetValue(info, out cslist))
{
return;
}
cslist.Remove(csc);
}
}
Vector<CSComponent> csComponentVector = new Vector<CSComponent>();
Dictionary<CSComponentInfo, List<CSComponent>> cscomponents = new Dictionary<CSComponentInfo, List<CSComponent>>();
List<CSComponent> cscomponentStart = new List<CSComponent>();
}
}