Scene.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using static System.Reflection.IntrospectionExtensions;
  6. namespace AtomicEngine
  7. {
  8. public partial class Scene : Node
  9. {
  10. internal override void PostNativeUpdate()
  11. {
  12. SubscribeToEvent<NodeAddedEvent>(this, e =>
  13. {
  14. //Log.Info($"Node ADDED: {e.Node.Name}");
  15. });
  16. SubscribeToEvent<NodeRemovedEvent>(this, e =>
  17. {
  18. //Log.Info($"Node REMOVED: {e.Node.Name}");
  19. });
  20. SubscribeToEvent<CSComponentLoadEvent>(this, HandleCSComponentLoad);
  21. SubscribeToEvent<ComponentAddedEvent>(this, HandleComponentAdded);
  22. SubscribeToEvent<ComponentRemovedEvent>(this, HandleComponentRemoved);
  23. // Update variable timestep logic
  24. SubscribeToEvent<SceneUpdateEvent>(this, HandleSceneUpdate);
  25. // Update scene subsystems. If a physics world is present, it will be updated, triggering fixed timestep logic updates
  26. SubscribeToEvent<SceneSubsystemUpdateEvent>(this, HandleSceneSubsystemUpdate);
  27. // Update transform smoothing
  28. SubscribeToEvent<UpdateSmoothingEvent>(this, HandleUpdateSmoothing);
  29. // Post-update variable timestep logic
  30. SubscribeToEvent<ScenePostUpdateEvent>(this, HandleScenePostUpdate);
  31. }
  32. // Update variable timestep logic
  33. void HandleSceneUpdate(SceneUpdateEvent e)
  34. {
  35. // Handle Start
  36. if (cscomponentStart.Count > 0)
  37. {
  38. var started = new List<CSComponent>();
  39. foreach (var csc in cscomponentStart.ToList())
  40. {
  41. if (!csc.IsEnabled())
  42. {
  43. continue;
  44. }
  45. // mark as started whether or not a Start method exists
  46. csc.started = true;
  47. started.Add(csc);
  48. CSComponentInfo info;
  49. if (CSComponentCore.csinfoLookup.TryGetValue(csc.GetType(), out info))
  50. {
  51. if (info.StartMethod != null)
  52. {
  53. info.StartMethod.Invoke(csc, null);
  54. }
  55. }
  56. }
  57. foreach (var csc in started)
  58. {
  59. cscomponentStart.Remove(csc);
  60. }
  61. }
  62. // Handle Scene Update
  63. Object[] args = new Object[1] { e.TimeStep };
  64. foreach (var item in cscomponents.ToList())
  65. {
  66. var info = item.Key;
  67. var UpdateMethod = info.UpdateMethod;
  68. if (UpdateMethod == null)
  69. continue;
  70. foreach (var csc in item.Value.ToList())
  71. {
  72. if (!csc.Started || !csc.IsEnabled())
  73. continue;
  74. UpdateMethod.Invoke(csc, args);
  75. }
  76. }
  77. }
  78. // Update scene subsystems. If a physics world is present, it will be updated, triggering fixed timestep logic updates
  79. void HandleSceneSubsystemUpdate(SceneSubsystemUpdateEvent e)
  80. {
  81. }
  82. // Update transform smoothing
  83. void HandleUpdateSmoothing(UpdateSmoothingEvent e)
  84. {
  85. }
  86. // Post-update variable timestep logic
  87. void HandleScenePostUpdate(ScenePostUpdateEvent e)
  88. {
  89. Object[] args = new Object[1] { e.TimeStep };
  90. foreach (var item in cscomponents)
  91. {
  92. var info = item.Key;
  93. var PostUpdateMethod = info.PostUpdateMethod;
  94. if (PostUpdateMethod == null)
  95. continue;
  96. foreach (var csc in item.Value)
  97. {
  98. if (!csc.Started || !csc.IsEnabled())
  99. continue;
  100. PostUpdateMethod.Invoke(csc, args);
  101. }
  102. }
  103. }
  104. void HandlePhysicsPreStep(PhysicsPreStepEvent e)
  105. {
  106. Object[] args = new Object[1] { e.TimeStep };
  107. foreach (var item in cscomponents)
  108. {
  109. var info = item.Key;
  110. var PhysicsPreStepMethod = info.PhysicsPreStepMethod;
  111. if (PhysicsPreStepMethod == null)
  112. continue;
  113. foreach (var csc in item.Value)
  114. {
  115. if (!csc.Started || !csc.IsEnabled())
  116. continue;
  117. PhysicsPreStepMethod.Invoke(csc, args);
  118. }
  119. }
  120. }
  121. void HandlePhysicsPostStep(PhysicsPostStepEvent e)
  122. {
  123. Object[] args = new Object[1] { e.TimeStep };
  124. foreach (var item in cscomponents)
  125. {
  126. var info = item.Key;
  127. var PhysicsPostStepMethod = info.PhysicsPostStepMethod;
  128. if (PhysicsPostStepMethod == null)
  129. continue;
  130. foreach (var csc in item.Value)
  131. {
  132. if (!csc.Started || !csc.IsEnabled())
  133. continue;
  134. PhysicsPostStepMethod.Invoke(csc, args);
  135. }
  136. }
  137. }
  138. void HandleComponentRemoved(ComponentRemovedEvent e)
  139. {
  140. Component component;
  141. try
  142. {
  143. // will throw if component isn't a known native
  144. component = e.Component;
  145. }
  146. catch
  147. {
  148. return;
  149. }
  150. if (component.GetType() == typeof(PhysicsWorld) || component.GetType() == typeof(PhysicsWorld2D))
  151. {
  152. UnsubscribeFromEvent<PhysicsPreStepEvent>();
  153. UnsubscribeFromEvent<PhysicsPostStepEvent>();
  154. }
  155. if (component.GetType().GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
  156. {
  157. var csc = (CSComponent)component;
  158. CSComponentInfo info;
  159. if (!CSComponentCore.csinfoLookup.TryGetValue(csc.GetType(), out info))
  160. {
  161. return;
  162. }
  163. cscomponentStart.Remove(csc);
  164. List<CSComponent> cslist;
  165. if (!cscomponents.TryGetValue(info, out cslist))
  166. {
  167. return;
  168. }
  169. cslist.Remove(csc);
  170. }
  171. }
  172. void HandleCSComponentLoad(CSComponentLoadEvent e)
  173. {
  174. var scriptMap = e.scriptMap;
  175. // Get the NativeInstance as an IntPtr, otherwise we would be wrapping as a CSComponent
  176. IntPtr csnative = scriptMap.GetVoidPtr("NativeInstance");
  177. IntPtr fieldValues = IntPtr.Zero;
  178. if (scriptMap.Contains("FieldValues"))
  179. fieldValues = scriptMap.GetVoidPtr("FieldValues");
  180. CSComponentInfo csinfo;
  181. if (!CSComponentCore.componentCache.TryGetValue(e.ClassName, out csinfo))
  182. {
  183. return;
  184. }
  185. NativeCore.NativeContructorOverride = csnative;
  186. var component = (CSComponent)Activator.CreateInstance(csinfo.Type);
  187. NativeCore.VerifyNativeContructorOverrideConsumed();
  188. if (fieldValues != IntPtr.Zero)
  189. csinfo.ApplyFieldValues(component, fieldValues);
  190. AddCSComponent(component);
  191. }
  192. void AddCSComponent(CSComponent csc)
  193. {
  194. CSComponentInfo info;
  195. if (!CSComponentCore.csinfoLookup.TryGetValue(csc.GetType(), out info))
  196. {
  197. Log.Error("Scene.HandleComponentAdded - unable to get CSComponentInfo");
  198. return;
  199. }
  200. List<CSComponent> cslist;
  201. if (!cscomponents.TryGetValue(info, out cslist))
  202. {
  203. cslist = cscomponents[info] = new List<CSComponent>();
  204. }
  205. if (cslist.Contains(csc))
  206. {
  207. throw new InvalidOperationException("Scene.HandleComponentAdded - CSComponent already added to component list");
  208. }
  209. cslist.Add(csc);
  210. if (cscomponentStart.Contains(csc))
  211. {
  212. throw new InvalidOperationException("Scene.HandleComponentAdded CSComponent already added to start list");
  213. }
  214. if (csc.started)
  215. {
  216. throw new InvalidOperationException("Scene.HandleComponentAdded CSComponent already started");
  217. }
  218. cscomponentStart.Add(csc);
  219. }
  220. void HandleComponentAdded(ComponentAddedEvent e)
  221. {
  222. Component component;
  223. try
  224. {
  225. // will throw if component isn't a known native
  226. component = e.Component;
  227. }
  228. catch
  229. {
  230. return;
  231. }
  232. // Check null (CSComponent) or other abstract component
  233. if (component == null)
  234. {
  235. return;
  236. }
  237. // Log.Info($"Component {component.TypeName} ADDED From Node {e.Node.Name}");
  238. if (component.GetType() == typeof(PhysicsWorld) || component.GetType() == typeof(PhysicsWorld2D))
  239. {
  240. SubscribeToEvent<PhysicsPreStepEvent>(component, HandlePhysicsPreStep);
  241. SubscribeToEvent<PhysicsPostStepEvent>(component, HandlePhysicsPostStep);
  242. }
  243. // CSComponent
  244. if (component.GetType().GetTypeInfo().IsSubclassOf(typeof(CSComponent)))
  245. {
  246. var csc = (CSComponent)component;
  247. AddCSComponent(csc);
  248. }
  249. }
  250. Dictionary<CSComponentInfo, List<CSComponent>> cscomponents = new Dictionary<CSComponentInfo, List<CSComponent>>();
  251. List<CSComponent> cscomponentStart = new List<CSComponent>();
  252. }
  253. }