GameActionContext.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  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]
  11. public class GameActionContext : ISerializationCallbackReceiver
  12. {
  13. [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = false, Expanded = false, ShowPaging = false, ShowItemCount = true)]
  14. [ShowInInspector]
  15. public List<GameActionContextVariable> variables = new List<GameActionContextVariable>();
  16. public bool HasVariable(string variableName)
  17. {
  18. for (int i = 0; i < variables.Count; i++)
  19. {
  20. if(variables[i].name == variableName) return true;
  21. }
  22. return false;
  23. }
  24. public GameActionContextVariable GetVariable(string title)
  25. {
  26. foreach (var variable in variables)
  27. {
  28. if(variable.name == title) return variable;
  29. }
  30. return null;
  31. }
  32. public void AddVariable(GameActionContextVariable variable)
  33. {
  34. for (int i = 0; i < variables.Count; i++)
  35. {
  36. if(variables[i].name == variable.name)
  37. {
  38. variables[i] = variable;
  39. return;
  40. }
  41. }
  42. variables.Add(variable);
  43. }
  44. #region Serialization
  45. [SerializeField, HideInInspector] private List<string> serializedVariables = new List<string>();
  46. [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer();
  47. private void SerializeActions()
  48. {
  49. serializedVariables = new List<string>();
  50. objectSerializer.ClearGameObjects();
  51. foreach (var variable in variables)
  52. {
  53. variable.className = variable.GetType().AssemblyQualifiedName;
  54. variable.typeName = variable.GetTypeName();
  55. variable.OnBeforeSerialize(objectSerializer);
  56. string data = JsonUtility.ToJson(variable);
  57. serializedVariables.Add(data);
  58. }
  59. }
  60. private void DeserializeActions()
  61. {
  62. if(serializedVariables == null) return;
  63. foreach (var data in serializedVariables)
  64. {
  65. GameActionContextVariable variable = JsonUtility.FromJson<GameActionContextVariableBase>(data);
  66. variable = GameActionContextVariable.InvokeStringMethod(variable.className, variable.typeName, data);
  67. variable.OnBeforeDeserialize(objectSerializer);
  68. AddVariable(variable);
  69. }
  70. objectSerializer.ClearGameObjects();
  71. }
  72. public void OnBeforeSerialize() => SerializeActions();
  73. public void OnAfterDeserialize() => DeserializeActions();
  74. #endregion
  75. #region NewVariable
  76. [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewVariable")]
  77. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  78. [LabelText("Add New Variable")]
  79. [NonSerialized]
  80. public GameActionContextVariable newVariable;
  81. [NonSerialized] private IEnumerable possibleVariables = new ValueDropdownList<GameActionContextVariable>();
  82. private void GetCompatibleVariablenames()
  83. {
  84. possibleVariables = ReflectiveEnumerator.GetEnumerableOfType<GameActionContextVariable>()
  85. .Where(x => x.GetTypeName() != "GameActionContextVariable")
  86. .Where(x => x.GetTypeName() != "GameActionContextVariableBase")
  87. .Select(x => new ValueDropdownItem(x.GetVariableName(), x));
  88. }
  89. private void AddNewVariable()
  90. {
  91. if(newVariable != null)
  92. {
  93. newVariable.className = newVariable.GetType().AssemblyQualifiedName;
  94. variables.Add(newVariable);
  95. newVariable = null;
  96. }
  97. }
  98. #endregion
  99. #region Utlilities
  100. public GameActionContext Duplicate()
  101. {
  102. GameActionContext newContext = new GameActionContext();
  103. var newSerializedVariables = new List<string>();
  104. newContext.objectSerializer = new GameActionObjectSerializer();
  105. foreach (var variable in variables)
  106. {
  107. variable.className = variable.GetType().AssemblyQualifiedName;
  108. variable.typeName = variable.GetTypeName();
  109. variable.OnBeforeSerialize(newContext.objectSerializer);
  110. string data = JsonUtility.ToJson(variable);
  111. newSerializedVariables.Add(data);
  112. }
  113. foreach (var data in newSerializedVariables)
  114. {
  115. GameActionContextVariable variable = JsonUtility.FromJson<GameActionContextVariableBase>(data);
  116. variable = GameActionContextVariable.InvokeStringMethod(variable.className, variable.typeName, data);
  117. variable.OnBeforeDeserialize(newContext.objectSerializer);
  118. newContext.AddVariable(variable);
  119. }
  120. newContext.objectSerializer.ClearGameObjects();
  121. return newContext;
  122. }
  123. #endregion
  124. }
  125. }