using System; using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { [System.Serializable, HideReferenceObjectPicker] public class ConditionalGameAction : GameActionBase { public override string name { get { return "Conditional"; } } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "ConditionalGameAction"; } } public override string GetTypeName() => "ConditionalGameAction"; public override string GetActionName() => "Core/Conditional Logic"; [IconFoldoutGroup("Contidional", "Assets/Plugins/KairoEngine/Core/Editor/Icons/ConditionalGameAcionIcon.png")] [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = true, Expanded = false, ShowPaging = false, ShowItemCount = true)] [BoxGroup("Contidional/If")] [PropertySpace(4,0)] [NonSerialized, ShowInInspector] public List conditions = new List(); #region NewCondition [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewCondition")] [ValueDropdown("possibleConditions", IsUniqueList = false)] [LabelText("Add New Condition")] [BoxGroup("Contidional/If")] [NonSerialized] public GameActionCondition newCondition; private IEnumerable possibleConditions = new ValueDropdownList(); private void GetCompatibleVariablenames() { possibleConditions = ReflectiveEnumerator.GetEnumerableOfType() .Where(x => x.GetTypeName() != "GameActionConditionBase") .Where(x => x.GetTypeName() != "GameActionCondition") .Select(x => new ValueDropdownItem(x.GetActionName(), x)); } private void AddNewCondition() { if(newCondition != null) { newCondition.controller = this.controller; newCondition.className = newCondition.GetType().AssemblyQualifiedName; conditions.Add(newCondition); newCondition = null; } } #endregion [IconFoldoutGroup("Contidional")] [HideLabel, InlineProperty, HideReferenceObjectPicker] [BoxGroup("Contidional/If")] [OnInspectorInit("InitializeGameActionControllers")] [NonSerialized, ShowInInspector] public GameActionsController ifController; [IconFoldoutGroup("Contidional")] [HideLabel, InlineProperty, HideReferenceObjectPicker] [BoxGroup("Contidional/Else")] [NonSerialized, ShowInInspector] public GameActionsController elseController; private bool isTrue = true; public ConditionalGameAction(GameActionsController controller = null) : base(controller) { this.controller = controller; className = this.GetType().AssemblyQualifiedName; if(this.controller != null) InitializeGameActionControllers(); } public override void Start() { _started = true; isTrue = true; for (int i = 0; i < conditions.Count; i++) { if(conditions[i].Evaluate() == false) isTrue = false; } if(isTrue) { ifController.Start(); if(ifController.isDone) _done = true; } else { elseController.Start(); if(elseController.isDone) _done = true; } } public override void Update() { if(isTrue && ifController.isDone) _done = true; if(!isTrue && elseController.isDone) _done = true; } public override void Restart() { _done = false; _started = false; isTrue = true; } public static ConditionalGameAction JSONToConditionalGameAction(string data) { return JsonUtility.FromJson(data); } private ConditionalGameAction Duplicate(GameActionsController controller = null) { ConditionalGameAction action = new ConditionalGameAction(controller == null ? this.controller : controller); action.controller = controller; return action; } private void InitializeGameActionControllers() { if(ifController == null) { ifController = new GameActionsController(); ifController.actions = new List(); ifController.depth = controller.depth + 1; } ifController.context = controller.context; for (int i = 0; i < ifController.actions.Count; i++) ifController.actions[i].controller = ifController; if(elseController == null) { elseController = new GameActionsController(); elseController.actions = new List(); elseController.depth = controller.depth + 1; } elseController.context = controller.context; for (int i = 0; i < elseController.actions.Count; i++) elseController.actions[i].controller = elseController; if(conditions == null) conditions = new List(); } [SerializeField, HideInInspector] private List serializedConditions = new List(); [SerializeField, HideInInspector] private string serializedIfController = ""; [SerializeField, HideInInspector] private string serializedElseController = ""; [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer(); public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { // Serialize Conditions if(conditions == null) return; serializedConditions = new List(); objectSerializer.ClearGameObjects(); objectSerializer.ClearScriptableObjects(); for (int i = 0; i < conditions.Count; i++) { conditions[i].OnBeforeSerialize(objectSerializer, i, depth); string data = JsonUtility.ToJson(conditions[i]); serializedConditions.Add(data); } // Serialize Game Action Controllers if(ifController != null) { ifController.objectSerializer = serializer; ifController.OnBeforeSerialize(); ifController.context = null; serializedIfController = JsonUtility.ToJson(ifController); //ifController = null; } if(elseController != null) { elseController.objectSerializer = serializer; elseController.OnBeforeSerialize(); elseController.context = null; serializedElseController = JsonUtility.ToJson(elseController); //elseController = null; } } public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { // Deserialize conditions if(serializedConditions == null) return; if(conditions == null) conditions = new List(); for (int i = 0; i < serializedConditions.Count; i++) { GameActionCondition condition = JsonUtility.FromJson(serializedConditions[i]); condition.controller = null; condition = GameActionCondition.InvokeStringMethod(condition.className, condition.typeName, serializedConditions[i]); condition.controller = this.controller; condition.OnBeforeDeserialize(objectSerializer, i, depth); conditions.Add(condition); } objectSerializer.ClearGameObjects(); objectSerializer.ClearScriptableObjects(); // Deserialize Game Action Controllers ifController = new GameActionsController(); ifController = JsonUtility.FromJson(serializedIfController); ifController.objectSerializer = serializer; //ifController.OnAfterDeserialize(); ifController.context = controller.context; elseController = new GameActionsController(); elseController = JsonUtility.FromJson(serializedElseController); elseController.objectSerializer = serializer; //elseController.OnAfterDeserialize(); elseController.context = controller.context; InitializeGameActionControllers(); } } }