LogicComponent.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Urho.Physics;
  4. namespace Urho
  5. {
  6. /// <summary>
  7. /// Helper base class for user-defined game logic components that hooks up to update events and forwards them to virtual functions similar to ScriptInstance class.
  8. /// </summary>
  9. public class LogicComponent : Component
  10. {
  11. Subscription physicsPreStepSubscription;
  12. Subscription physicsPostStepSubscription;
  13. Subscription scenePostUpdateSubscription;
  14. [Preserve]
  15. public LogicComponent (IntPtr handle) : base (handle)
  16. {
  17. }
  18. [Preserve]
  19. public LogicComponent () : this (Application.CurrentContext)
  20. {
  21. }
  22. [DllImport (Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  23. internal static extern IntPtr LogicComponent_LogicComponent (IntPtr context);
  24. public LogicComponent (Context context) : base (UrhoObjectFlag.Empty)
  25. {
  26. Runtime.Validate (typeof(LogicComponent));
  27. handle = LogicComponent_LogicComponent ((object)context == null ? IntPtr.Zero : context.Handle);
  28. Runtime.RegisterObject (this);
  29. }
  30. public override void OnSceneSet(Scene scene)
  31. {
  32. if (scene != null)
  33. {
  34. if (ReceiveFixedUpdates)
  35. {
  36. var physicsWorld = scene.GetComponent<PhysicsWorld>();
  37. if (physicsWorld == null)
  38. throw new InvalidOperationException("Scene must have PhysicsWorld component in order to receive FixedUpdates");
  39. physicsPreStepSubscription = physicsWorld.SubscribeToPhysicsPreStep(OnFixedUpdate);
  40. }
  41. if (ReceiveFixedPostUpdates)
  42. {
  43. var physicsWorld = scene.GetComponent<PhysicsWorld>();
  44. if (physicsWorld == null)
  45. throw new InvalidOperationException("Scene must have PhysicsWorld component in order to receive FixedUpdates");
  46. physicsPostStepSubscription = physicsWorld.SubscribeToPhysicsPostStep(OnFixedPostUpdate);
  47. }
  48. if (ReceivePostUpdates)
  49. {
  50. scenePostUpdateSubscription = scene.SubscribeToScenePostUpdate(OnPostUpdate);
  51. }
  52. }
  53. else
  54. {
  55. physicsPreStepSubscription?.Unsubscribe();
  56. physicsPostStepSubscription?.Unsubscribe();
  57. scenePostUpdateSubscription?.Unsubscribe();
  58. }
  59. }
  60. protected bool ReceiveFixedUpdates { get; set; }
  61. protected bool ReceiveFixedPostUpdates { get; set; }
  62. protected bool ReceivePostUpdates { get; set; }
  63. protected virtual void OnFixedUpdate(PhysicsPreStepEventArgs e) { }
  64. protected virtual void OnFixedPostUpdate(PhysicsPostStepEventArgs e) { }
  65. protected virtual void OnPostUpdate(ScenePostUpdateEventArgs e) { }
  66. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  67. internal static extern int LogicComponent_GetType(IntPtr handle);
  68. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  69. internal static extern IntPtr LogicComponent_GetTypeName(IntPtr handle);
  70. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  71. internal static extern int LogicComponent_GetTypeStatic();
  72. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  73. internal static extern IntPtr LogicComponent_GetTypeNameStatic();
  74. public override StringHash Type => new StringHash(LogicComponent_GetType(handle));
  75. public override string TypeName => Marshal.PtrToStringAnsi(LogicComponent_GetTypeName(handle));
  76. [Preserve]
  77. public new static StringHash TypeStatic => new StringHash(LogicComponent_GetTypeStatic());
  78. public new static string TypeNameStatic => Marshal.PtrToStringAnsi(LogicComponent_GetTypeNameStatic());
  79. }
  80. }