OnEventGameActionTrigger.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.Core.GameActions
  8. {
  9. [System.Serializable, HideReferenceObjectPicker]
  10. public class OnEventGameActionTrigger : GameActionTriggerBase
  11. {
  12. public override string name
  13. {
  14. get
  15. {
  16. return $"On Event \"{eventName}\"";
  17. }
  18. }
  19. public override GameActionTriggersController controller {
  20. get => _controller;
  21. set
  22. {
  23. _controller = value;
  24. typeName = "OnEventGameActionTrigger";
  25. }
  26. }
  27. public override string GetTypeName() => "OnEventGameActionTrigger";
  28. public override string GetTriggerName() => "On Event";
  29. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/OnEventGameActionTriggerIcon.png")] public string eventName = "event";
  30. [IconFoldoutGroup("@name")] public float delay = 0;
  31. [IconFoldoutGroup("@name")] public bool oneShot = false;
  32. [IconFoldoutGroup("@name")] public bool debugEvents = false;
  33. private float _counter = 0;
  34. private bool _triggered = false;
  35. public override void OnEnable()
  36. {
  37. GenericEvents.StartListening(eventName, ReceiveEvent);
  38. if(debugEvents) Debug.Log($"Started listening to \'{eventName}\' events");
  39. _counter = 0f;
  40. _triggered = false;
  41. }
  42. public override void Update()
  43. {
  44. if(_triggered)
  45. {
  46. _counter += Time.deltaTime;
  47. if(_counter > delay) TriggerActions();
  48. }
  49. }
  50. public override void OnDisable()
  51. {
  52. GenericEvents.StopListening(eventName, TriggerActions);
  53. if(debugEvents) Debug.Log($"Stopped listening to \'{eventName}\' events");
  54. }
  55. private void ReceiveEvent()
  56. {
  57. if(debugEvents) Debug.Log($"Event \'{eventName}\' triggered");
  58. _triggered = true;
  59. if(delay == 0) TriggerActions();
  60. }
  61. private void TriggerActions()
  62. {
  63. controller.actionsController.Restart();
  64. controller.TriggerActions();
  65. if(oneShot)
  66. {
  67. GenericEvents.StopListening(eventName, ReceiveEvent);
  68. if(debugEvents) Debug.Log($"Stopped listening to \'{eventName}\' events");
  69. }
  70. else
  71. {
  72. _counter = 0f;
  73. _triggered = false;
  74. }
  75. }
  76. public static OnEventGameActionTrigger JSONToOnEventGameActionTrigger(string data)
  77. {
  78. return JsonUtility.FromJson<OnEventGameActionTrigger>(data);
  79. }
  80. private OnEventGameActionTrigger Duplicate()
  81. {
  82. OnEventGameActionTrigger trigger = new OnEventGameActionTrigger();
  83. trigger.controller = controller;
  84. return trigger;
  85. }
  86. }
  87. }