GameActionTriggersController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.Core.GameActions
  9. {
  10. public enum GameActionTriggerBehaviour
  11. {
  12. TriggerOnce,
  13. WaitForActions,
  14. RestartActions
  15. }
  16. [System.Serializable]
  17. public class GameActionTriggersController : ISerializationCallbackReceiver
  18. {
  19. [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = true, Expanded = false, ShowPaging = false, ShowItemCount = true)]
  20. [ShowInInspector, NonSerialized]
  21. [Space(4)]
  22. public List<GameActionTrigger> triggers = new List<GameActionTrigger>();
  23. [NonSerialized]
  24. public GameActionsController actionsController = null;
  25. private bool actionsRunning = false;
  26. #region Flow
  27. public void TriggerActions()
  28. {
  29. if(actionsRunning) return;
  30. actionsController.Restart();
  31. actionsController.Start();
  32. actionsRunning = true;
  33. //foreach (var trigger in triggers) trigger.OnDisable();
  34. }
  35. public void OnEnable()
  36. {
  37. foreach (var trigger in triggers) trigger.OnEnable();
  38. }
  39. public void OnDisable()
  40. {
  41. foreach (var trigger in triggers) trigger.OnDisable();
  42. }
  43. public void Update()
  44. {
  45. if(actionsController.started && actionsController.isDone) actionsRunning = false;
  46. if(!actionsRunning) foreach (var trigger in triggers) trigger.Update();
  47. }
  48. public void AddTrigger(GameActionTrigger trigger)
  49. {
  50. for (int i = 0; i < triggers.Count; i++)
  51. {
  52. if(triggers[i].name == trigger.name)
  53. {
  54. triggers[i] = trigger;
  55. return;
  56. }
  57. }
  58. triggers.Add(trigger);
  59. }
  60. #endregion
  61. #region Serialization
  62. [SerializeField, HideInInspector] private List<string> serializedTriggers = new List<string>();
  63. [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer();
  64. private void SerializeTriggers()
  65. {
  66. serializedTriggers = new List<string>();
  67. objectSerializer.ClearGameObjects();
  68. objectSerializer.ClearScriptableObjects();
  69. for (int i = 0; i < triggers.Count; i++)
  70. {
  71. triggers[i].OnBeforeSerialize(objectSerializer, i);
  72. string data = JsonUtility.ToJson(triggers[i]);
  73. serializedTriggers.Add(data);
  74. }
  75. }
  76. private void DeserializeTriggers()
  77. {
  78. if(serializedTriggers == null) return;
  79. for (int i = 0; i < serializedTriggers.Count; i++)
  80. {
  81. GameActionTrigger trigger = JsonUtility.FromJson<GameActionTriggerBase>(serializedTriggers[i]);
  82. trigger = GameActionTrigger.InvokeStringMethod(trigger.className, trigger.typeName, serializedTriggers[i]);
  83. trigger.controller = this;
  84. trigger.OnBeforeDeserialize(objectSerializer, i);
  85. AddTrigger(trigger);
  86. }
  87. objectSerializer.ClearGameObjects();
  88. objectSerializer.ClearScriptableObjects();
  89. }
  90. public void OnBeforeSerialize() => SerializeTriggers();
  91. public void OnAfterDeserialize() => DeserializeTriggers();
  92. #endregion
  93. #region NewAction
  94. [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewTrigger")]
  95. [ValueDropdown("possibleTriggers", IsUniqueList = false)]
  96. [LabelText("Add New Trigger")]
  97. [NonSerialized]
  98. public GameActionTrigger newTrigger;
  99. private IEnumerable possibleTriggers = new ValueDropdownList<GameActionTrigger>();
  100. private void GetCompatibleVariablenames()
  101. {
  102. possibleTriggers = ReflectiveEnumerator.GetEnumerableOfType<GameActionTrigger>()
  103. .Where(x => x.GetTypeName() != "GameActionTriggerBase")
  104. .Where(x => x.GetTypeName() != "GameActionTrigger")
  105. .Select(x => new ValueDropdownItem(x.GetTriggerName(), x));
  106. }
  107. private void AddNewTrigger()
  108. {
  109. if(newTrigger != null)
  110. {
  111. newTrigger.controller = this;
  112. newTrigger.className = newTrigger.GetType().AssemblyQualifiedName;
  113. triggers.Add(newTrigger);
  114. newTrigger = null;
  115. }
  116. }
  117. #endregion
  118. //[LabelText("Behaviour")]
  119. //public GameActionTriggerBehaviour triggerBehaviour = GameActionTriggerBehaviour.WaitForActions;
  120. }
  121. }