ConditionalGameAction.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.Core.GameActions
  9. {
  10. [System.Serializable, HideReferenceObjectPicker]
  11. public class ConditionalGameAction : GameActionBase
  12. {
  13. public override string name
  14. {
  15. get
  16. {
  17. return "Conditional";
  18. }
  19. }
  20. public override GameActionsController controller {
  21. get => _controller;
  22. set
  23. {
  24. _controller = value;
  25. typeName = "ConditionalGameAction";
  26. }
  27. }
  28. public override string GetTypeName() => "ConditionalGameAction";
  29. public override string GetActionName() => "Core/Conditional Logic";
  30. [IconFoldoutGroup("Contidional", "Assets/Plugins/KairoEngine/Core/Editor/Icons/ConditionalGameAcionIcon.png")]
  31. [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = true, Expanded = false, ShowPaging = false, ShowItemCount = true)]
  32. [BoxGroup("Contidional/If")]
  33. [PropertySpace(4,0)]
  34. [NonSerialized, ShowInInspector]
  35. public List<GameActionCondition> conditions = new List<GameActionCondition>();
  36. #region NewCondition
  37. [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewCondition")]
  38. [ValueDropdown("possibleConditions", IsUniqueList = false)]
  39. [LabelText("Add New Condition")]
  40. [BoxGroup("Contidional/If")]
  41. [NonSerialized]
  42. public GameActionCondition newCondition;
  43. private IEnumerable possibleConditions = new ValueDropdownList<GameActionCondition>();
  44. private void GetCompatibleVariablenames()
  45. {
  46. possibleConditions = ReflectiveEnumerator.GetEnumerableOfType<GameActionCondition>()
  47. .Where(x => x.GetTypeName() != "GameActionConditionBase")
  48. .Where(x => x.GetTypeName() != "GameActionCondition")
  49. .Select(x => new ValueDropdownItem(x.GetActionName(), x));
  50. }
  51. private void AddNewCondition()
  52. {
  53. if(newCondition != null)
  54. {
  55. newCondition.controller = this.controller;
  56. newCondition.className = newCondition.GetType().AssemblyQualifiedName;
  57. conditions.Add(newCondition);
  58. newCondition = null;
  59. }
  60. }
  61. #endregion
  62. [IconFoldoutGroup("Contidional")]
  63. [HideLabel, InlineProperty, HideReferenceObjectPicker]
  64. [BoxGroup("Contidional/If")]
  65. [OnInspectorInit("InitializeGameActionControllers")]
  66. [NonSerialized, ShowInInspector]
  67. public GameActionsController ifController;
  68. [IconFoldoutGroup("Contidional")]
  69. [HideLabel, InlineProperty, HideReferenceObjectPicker]
  70. [BoxGroup("Contidional/Else")]
  71. [NonSerialized, ShowInInspector]
  72. public GameActionsController elseController;
  73. private bool isTrue = true;
  74. public ConditionalGameAction(GameActionsController controller = null) : base(controller)
  75. {
  76. this.controller = controller;
  77. className = this.GetType().AssemblyQualifiedName;
  78. if(this.controller != null) InitializeGameActionControllers();
  79. }
  80. public override void Start()
  81. {
  82. _started = true;
  83. isTrue = true;
  84. for (int i = 0; i < conditions.Count; i++)
  85. {
  86. if(conditions[i].Evaluate() == false) isTrue = false;
  87. }
  88. if(isTrue)
  89. {
  90. ifController.Start();
  91. if(ifController.isDone) _done = true;
  92. }
  93. else
  94. {
  95. elseController.Start();
  96. if(elseController.isDone) _done = true;
  97. }
  98. }
  99. public override void Update()
  100. {
  101. if(isTrue && ifController.isDone) _done = true;
  102. if(!isTrue && elseController.isDone) _done = true;
  103. }
  104. public override void Restart()
  105. {
  106. _done = false;
  107. _started = false;
  108. isTrue = true;
  109. }
  110. public static ConditionalGameAction JSONToConditionalGameAction(string data)
  111. {
  112. return JsonUtility.FromJson<ConditionalGameAction>(data);
  113. }
  114. private ConditionalGameAction Duplicate(GameActionsController controller = null)
  115. {
  116. ConditionalGameAction action = new ConditionalGameAction(controller == null ? this.controller : controller);
  117. action.controller = controller;
  118. return action;
  119. }
  120. private void InitializeGameActionControllers()
  121. {
  122. if(ifController == null)
  123. {
  124. ifController = new GameActionsController();
  125. ifController.actions = new List<GameAction>();
  126. ifController.depth = controller.depth + 1;
  127. }
  128. ifController.context = controller.context;
  129. for (int i = 0; i < ifController.actions.Count; i++) ifController.actions[i].controller = ifController;
  130. if(elseController == null)
  131. {
  132. elseController = new GameActionsController();
  133. elseController.actions = new List<GameAction>();
  134. elseController.depth = controller.depth + 1;
  135. }
  136. elseController.context = controller.context;
  137. for (int i = 0; i < elseController.actions.Count; i++) elseController.actions[i].controller = elseController;
  138. if(conditions == null) conditions = new List<GameActionCondition>();
  139. }
  140. [SerializeField, HideInInspector] private List<string> serializedConditions = new List<string>();
  141. [SerializeField, HideInInspector] private string serializedIfController = "";
  142. [SerializeField, HideInInspector] private string serializedElseController = "";
  143. [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer();
  144. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
  145. {
  146. // Serialize Conditions
  147. if(conditions == null) return;
  148. serializedConditions = new List<string>();
  149. objectSerializer.ClearGameObjects();
  150. objectSerializer.ClearScriptableObjects();
  151. for (int i = 0; i < conditions.Count; i++)
  152. {
  153. conditions[i].OnBeforeSerialize(objectSerializer, i, depth);
  154. string data = JsonUtility.ToJson(conditions[i]);
  155. serializedConditions.Add(data);
  156. }
  157. // Serialize Game Action Controllers
  158. if(ifController != null)
  159. {
  160. ifController.objectSerializer = serializer;
  161. ifController.OnBeforeSerialize();
  162. ifController.context = null;
  163. serializedIfController = JsonUtility.ToJson(ifController);
  164. //ifController = null;
  165. }
  166. if(elseController != null)
  167. {
  168. elseController.objectSerializer = serializer;
  169. elseController.OnBeforeSerialize();
  170. elseController.context = null;
  171. serializedElseController = JsonUtility.ToJson(elseController);
  172. //elseController = null;
  173. }
  174. }
  175. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
  176. {
  177. // Deserialize conditions
  178. if(serializedConditions == null) return;
  179. if(conditions == null) conditions = new List<GameActionCondition>();
  180. for (int i = 0; i < serializedConditions.Count; i++)
  181. {
  182. GameActionCondition condition = JsonUtility.FromJson<GameActionConditionBase>(serializedConditions[i]);
  183. condition.controller = null;
  184. condition = GameActionCondition.InvokeStringMethod(condition.className, condition.typeName, serializedConditions[i]);
  185. condition.controller = this.controller;
  186. condition.OnBeforeDeserialize(objectSerializer, i, depth);
  187. conditions.Add(condition);
  188. }
  189. objectSerializer.ClearGameObjects();
  190. objectSerializer.ClearScriptableObjects();
  191. // Deserialize Game Action Controllers
  192. ifController = new GameActionsController();
  193. ifController = JsonUtility.FromJson<GameActionsController>(serializedIfController);
  194. ifController.objectSerializer = serializer;
  195. //ifController.OnAfterDeserialize();
  196. ifController.context = controller.context;
  197. elseController = new GameActionsController();
  198. elseController = JsonUtility.FromJson<GameActionsController>(serializedElseController);
  199. elseController.objectSerializer = serializer;
  200. //elseController.OnAfterDeserialize();
  201. elseController.context = controller.context;
  202. InitializeGameActionControllers();
  203. }
  204. }
  205. }