Component.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// Base class for all components. Components represent primary logic elements in the scene. They are attached to
  9. /// scene objects.
  10. ///
  11. /// Implementations of <see cref="Component"/> can implement a set of callbacks that will be called by the runtime
  12. /// at specified occassions:
  13. /// void OnInitialize() - Called once when the component is instantiated. Only called when the game is playing.
  14. /// void OnUpdate() - Called every frame while the game is running and the component is enabled.
  15. /// void OnEnable() - Called whenever a component is enabled, or instantiated as enabled in which case it is called
  16. /// after OnInitialize. Only called when the game is playing or paused.
  17. /// void OnDisable() - Called whenever a component is disabled. This includes destruction where it is called before
  18. /// OnDestroy. Only called when the game is playing or paused.
  19. /// void OnDestroy() - Called before the component is destroyed. Destruction is usually delayed until the end of the
  20. /// current frame unless specified otherwise in a call to Destroy.
  21. /// void OnReset() - Called when script assemblies have been refreshed or when the component is initialized. During
  22. /// initialization it is called after OnInitialize but before OnEnable. Only relevant in editor.
  23. ///
  24. /// You can also make these callbacks trigger when the game is stopped/paused by using the <see cref="RunInEditor"/>
  25. /// attribute on the component.
  26. /// </summary>
  27. public class Component : GameObject
  28. {
  29. // Internal use only
  30. protected Component()
  31. { }
  32. /// <summary>
  33. /// Returns the scene object this component is attached to.
  34. /// </summary>
  35. public SceneObject SceneObject
  36. {
  37. get { return Internal_GetSceneObject(mCachedPtr); }
  38. }
  39. /// <summary>
  40. /// Destroys the component, removing it from its scene object and stopping component updates.
  41. /// </summary>
  42. /// <param name="immediate">If true the component will be fully destroyed immediately. This means that objects
  43. /// that are still referencing this component might fail. Normally destruction is delayed
  44. /// until the end of the frame to give other objects a chance to stop using it.</param>
  45. public void Destroy(bool immediate = false)
  46. {
  47. Internal_Destroy(mCachedPtr, immediate);
  48. }
  49. /// <summary>
  50. /// Calculates bounds of the visible content for this component.
  51. /// </summary>
  52. /// <param name="box">Bounds in world space represented as an axis aligned bounding box.</param>
  53. /// <param name="sphere">Bounds in world space represented as a sphere.</param>
  54. /// <returns>True if the bounds have non-zero volume, false otherwise.</returns>
  55. internal protected virtual bool CalculateBounds(out AABox box, out Sphere sphere)
  56. {
  57. Vector3 pos = SceneObject.Position;
  58. box = new AABox(pos, pos);
  59. sphere = new Sphere(pos, 0.0f);
  60. return false;
  61. }
  62. [MethodImpl(MethodImplOptions.InternalCall)]
  63. internal static extern Component Internal_AddComponent(SceneObject parent, Type type);
  64. [MethodImpl(MethodImplOptions.InternalCall)]
  65. internal static extern Component Internal_GetComponent(SceneObject parent, Type type);
  66. [MethodImpl(MethodImplOptions.InternalCall)]
  67. internal static extern Component[] Internal_GetComponents(SceneObject parent);
  68. [MethodImpl(MethodImplOptions.InternalCall)]
  69. internal static extern Component[] Internal_GetComponentsPerType(SceneObject parent, Type type);
  70. [MethodImpl(MethodImplOptions.InternalCall)]
  71. internal static extern Component Internal_RemoveComponent(SceneObject parent, Type type);
  72. [MethodImpl(MethodImplOptions.InternalCall)]
  73. internal static extern SceneObject Internal_GetSceneObject(IntPtr nativeInstance);
  74. [MethodImpl(MethodImplOptions.InternalCall)]
  75. private static extern void Internal_Destroy(IntPtr nativeInstance, bool immediate);
  76. }
  77. }