123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 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<GameActionTrigger> triggers = new List<GameActionTrigger>();
- [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<string> serializedTriggers = new List<string>();
- [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer();
- private void SerializeTriggers()
- {
- serializedTriggers = new List<string>();
- 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<GameActionTriggerBase>(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<GameActionTrigger>();
- private void GetCompatibleVariablenames()
- {
- possibleTriggers = ReflectiveEnumerator.GetEnumerableOfType<GameActionTrigger>()
- .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;
- }
- }
|