123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- namespace KairoEngine.Core.GameActions
- {
- [System.Serializable]
- public class GameActionContext : ISerializationCallbackReceiver
- {
- [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = false, Expanded = false, ShowPaging = false, ShowItemCount = true)]
- [ShowInInspector]
- public List<GameActionContextVariable> variables = new List<GameActionContextVariable>();
- public bool HasVariable(string variableName)
- {
- for (int i = 0; i < variables.Count; i++)
- {
- if(variables[i].name == variableName) return true;
- }
- return false;
- }
- public GameActionContextVariable GetVariable(string title)
- {
- foreach (var variable in variables)
- {
- if(variable.name == title) return variable;
- }
- return null;
- }
- public void AddVariable(GameActionContextVariable variable)
- {
- for (int i = 0; i < variables.Count; i++)
- {
- if(variables[i].name == variable.name)
- {
- variables[i] = variable;
- return;
- }
- }
- variables.Add(variable);
- }
- #region Serialization
- [SerializeField, HideInInspector] private List<string> serializedVariables = new List<string>();
- [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer();
- private void SerializeActions()
- {
- serializedVariables = new List<string>();
- objectSerializer.ClearGameObjects();
- foreach (var variable in variables)
- {
- variable.className = variable.GetType().AssemblyQualifiedName;
- variable.typeName = variable.GetTypeName();
- variable.OnBeforeSerialize(objectSerializer);
- string data = JsonUtility.ToJson(variable);
- serializedVariables.Add(data);
- }
- }
- private void DeserializeActions()
- {
- if(serializedVariables == null) return;
- foreach (var data in serializedVariables)
- {
- GameActionContextVariable variable = JsonUtility.FromJson<GameActionContextVariableBase>(data);
- variable = GameActionContextVariable.InvokeStringMethod(variable.className, variable.typeName, data);
- variable.OnBeforeDeserialize(objectSerializer);
- AddVariable(variable);
- }
- objectSerializer.ClearGameObjects();
- }
- public void OnBeforeSerialize() => SerializeActions();
- public void OnAfterDeserialize() => DeserializeActions();
- #endregion
- #region NewVariable
- [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewVariable")]
- [ValueDropdown("possibleVariables", IsUniqueList = false)]
- [LabelText("Add New Variable")]
- [NonSerialized]
- public GameActionContextVariable newVariable;
- [NonSerialized] private IEnumerable possibleVariables = new ValueDropdownList<GameActionContextVariable>();
- private void GetCompatibleVariablenames()
- {
- possibleVariables = ReflectiveEnumerator.GetEnumerableOfType<GameActionContextVariable>()
- .Where(x => x.GetTypeName() != "GameActionContextVariable")
- .Where(x => x.GetTypeName() != "GameActionContextVariableBase")
- .Select(x => new ValueDropdownItem(x.GetVariableName(), x));
- }
- private void AddNewVariable()
- {
- if(newVariable != null)
- {
- newVariable.className = newVariable.GetType().AssemblyQualifiedName;
- variables.Add(newVariable);
- newVariable = null;
- }
- }
- #endregion
- #region Utlilities
- public GameActionContext Duplicate()
- {
- GameActionContext newContext = new GameActionContext();
- var newSerializedVariables = new List<string>();
- newContext.objectSerializer = new GameActionObjectSerializer();
- foreach (var variable in variables)
- {
- variable.className = variable.GetType().AssemblyQualifiedName;
- variable.typeName = variable.GetTypeName();
- variable.OnBeforeSerialize(newContext.objectSerializer);
- string data = JsonUtility.ToJson(variable);
- newSerializedVariables.Add(data);
- }
- foreach (var data in newSerializedVariables)
- {
- GameActionContextVariable variable = JsonUtility.FromJson<GameActionContextVariableBase>(data);
- variable = GameActionContextVariable.InvokeStringMethod(variable.className, variable.typeName, data);
- variable.OnBeforeDeserialize(newContext.objectSerializer);
- newContext.AddVariable(variable);
- }
- newContext.objectSerializer.ClearGameObjects();
- return newContext;
- }
- #endregion
- }
- }
|