BaseShotTarget.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ShotSimulator.Target
  5. {
  6. public enum ShotTargetInteractiveType
  7. {
  8. OnEnter,
  9. OnHovering,
  10. OnUpdate,
  11. OnFixedUpdate,
  12. OnClick,
  13. OnExit,
  14. OnNotEntering
  15. }
  16. public abstract class BaseShotTarget : MonoBehaviour
  17. {
  18. protected List<BaseShotTargetComponent> shotTargetComponents = new List<BaseShotTargetComponent>();
  19. public bool IsRunning { get; set; }
  20. public virtual void OnEnter()
  21. {
  22. ExecuteInteractive(ShotTargetInteractiveType.OnEnter);
  23. }
  24. public virtual void OnHovering()
  25. {
  26. ExecuteInteractive(ShotTargetInteractiveType.OnHovering);
  27. }
  28. public virtual void FixedUpdate()
  29. {
  30. ExecuteInteractive(ShotTargetInteractiveType.OnFixedUpdate);
  31. }
  32. public virtual void OnClick()
  33. {
  34. ExecuteInteractive(ShotTargetInteractiveType.OnClick);
  35. }
  36. public virtual void OnExit()
  37. {
  38. ExecuteInteractive(ShotTargetInteractiveType.OnExit);
  39. }
  40. private void ExecuteInteractive(ShotTargetInteractiveType type)
  41. {
  42. if (IsRunning)
  43. {
  44. foreach (var component in shotTargetComponents)
  45. {
  46. if (component.enable)
  47. {
  48. switch (type)
  49. {
  50. case ShotTargetInteractiveType.OnEnter:
  51. component.OnEnter();
  52. break;
  53. case ShotTargetInteractiveType.OnHovering:
  54. component.OnHovering();
  55. break;
  56. case ShotTargetInteractiveType.OnClick:
  57. component.OnClick();
  58. break;
  59. case ShotTargetInteractiveType.OnFixedUpdate:
  60. component.OnFixedUpdate();
  61. break;
  62. case ShotTargetInteractiveType.OnUpdate:
  63. component.OnUpdate();
  64. break;
  65. case ShotTargetInteractiveType.OnExit:
  66. component.OnExit();
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }