Component.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /// Calculates bounds of the visible content for this component.
  41. /// </summary>
  42. /// <param name="box">Bounds in world space represented as an axis aligned bounding box.</param>
  43. /// <param name="sphere">Bounds in world space represented as a sphere.</param>
  44. /// <returns>True if the bounds have non-zero volume, false otherwise.</returns>
  45. internal protected virtual bool CalculateBounds(out AABox box, out Sphere sphere)
  46. {
  47. Vector3 pos = SceneObject.Position;
  48. box = new AABox(pos, pos);
  49. sphere = new Sphere(pos, 0.0f);
  50. return false;
  51. }
  52. [MethodImpl(MethodImplOptions.InternalCall)]
  53. internal static extern Component Internal_AddComponent(SceneObject parent, Type type);
  54. [MethodImpl(MethodImplOptions.InternalCall)]
  55. internal static extern Component Internal_GetComponent(SceneObject parent, Type type);
  56. [MethodImpl(MethodImplOptions.InternalCall)]
  57. internal static extern Component[] Internal_GetComponents(SceneObject parent);
  58. [MethodImpl(MethodImplOptions.InternalCall)]
  59. internal static extern Component Internal_RemoveComponent(SceneObject parent, Type type);
  60. [MethodImpl(MethodImplOptions.InternalCall)]
  61. internal static extern SceneObject Internal_GetSceneObject(IntPtr nativeInstance);
  62. }
  63. }