using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { public enum GameActionTriggerBehaviour { TriggerOnce, WaitForActions, RestartActions } [System.Serializable] public class GameActionTriggersController : ISerializationCallbackReceiver { [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = true, Expanded = false, ShowPaging = false, ShowItemCount = true)] [ShowInInspector, NonSerialized] [Space(4)] public List triggers = new List(); [NonSerialized] public GameActionsController actionsController = null; private bool actionsRunning = false; #region Flow public void TriggerActions() { if(actionsRunning) return; actionsController.Restart(); actionsController.Start(); actionsRunning = true; //foreach (var trigger in triggers) trigger.OnDisable(); } public void OnEnable() { foreach (var trigger in triggers) trigger.OnEnable(); } public void OnDisable() { foreach (var trigger in triggers) trigger.OnDisable(); } public void Update() { if(actionsController.started && actionsController.isDone) actionsRunning = false; if(!actionsRunning) foreach (var trigger in triggers) trigger.Update(); } public void AddTrigger(GameActionTrigger trigger) { for (int i = 0; i < triggers.Count; i++) { if(triggers[i].name == trigger.name) { triggers[i] = trigger; return; } } triggers.Add(trigger); } #endregion #region Serialization [SerializeField, HideInInspector] private List serializedTriggers = new List(); [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer(); private void SerializeTriggers() { serializedTriggers = new List(); objectSerializer.ClearGameObjects(); objectSerializer.ClearScriptableObjects(); for (int i = 0; i < triggers.Count; i++) { triggers[i].OnBeforeSerialize(objectSerializer, i); string data = JsonUtility.ToJson(triggers[i]); serializedTriggers.Add(data); } } private void DeserializeTriggers() { if(serializedTriggers == null) return; for (int i = 0; i < serializedTriggers.Count; i++) { GameActionTrigger trigger = JsonUtility.FromJson(serializedTriggers[i]); trigger = GameActionTrigger.InvokeStringMethod(trigger.className, trigger.typeName, serializedTriggers[i]); trigger.controller = this; trigger.OnBeforeDeserialize(objectSerializer, i); AddTrigger(trigger); } objectSerializer.ClearGameObjects(); objectSerializer.ClearScriptableObjects(); } public void OnBeforeSerialize() => SerializeTriggers(); public void OnAfterDeserialize() => DeserializeTriggers(); #endregion #region NewAction [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewTrigger")] [ValueDropdown("possibleTriggers", IsUniqueList = false)] [LabelText("Add New Trigger")] [NonSerialized] public GameActionTrigger newTrigger; private IEnumerable possibleTriggers = new ValueDropdownList(); private void GetCompatibleVariablenames() { possibleTriggers = ReflectiveEnumerator.GetEnumerableOfType() .Where(x => x.GetTypeName() != "GameActionTriggerBase") .Where(x => x.GetTypeName() != "GameActionTrigger") .Select(x => new ValueDropdownItem(x.GetTriggerName(), x)); } private void AddNewTrigger() { if(newTrigger != null) { newTrigger.controller = this; newTrigger.className = newTrigger.GetType().AssemblyQualifiedName; triggers.Add(newTrigger); newTrigger = null; } } #endregion //[LabelText("Behaviour")] //public GameActionTriggerBehaviour triggerBehaviour = GameActionTriggerBehaviour.WaitForActions; } }