123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- 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<GameActionCondition> conditions = new List<GameActionCondition>();
- #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<GameActionCondition>();
- private void GetCompatibleVariablenames()
- {
- possibleConditions = ReflectiveEnumerator.GetEnumerableOfType<GameActionCondition>()
- .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<ConditionalGameAction>(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<GameAction>();
- 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<GameAction>();
- 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<GameActionCondition>();
- }
- [SerializeField, HideInInspector] private List<string> serializedConditions = new List<string>();
- [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<string>();
- 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<GameActionCondition>();
- for (int i = 0; i < serializedConditions.Count; i++)
- {
- GameActionCondition condition = JsonUtility.FromJson<GameActionConditionBase>(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<GameActionsController>(serializedIfController);
- ifController.objectSerializer = serializer;
- //ifController.OnAfterDeserialize();
- ifController.context = controller.context;
- elseController = new GameActionsController();
- elseController = JsonUtility.FromJson<GameActionsController>(serializedElseController);
- elseController.objectSerializer = serializer;
- //elseController.OnAfterDeserialize();
- elseController.context = controller.context;
- InitializeGameActionControllers();
- }
- }
- }
|