StatusEffectOperationsGameAction.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.GameActions;
  8. using KairoEngine.Core;
  9. namespace KairoEngine.Stats.GameActions
  10. {
  11. [System.Serializable, HideReferenceObjectPicker]
  12. public class StatusEffectOperationsGameAction : GameActionBase
  13. {
  14. #region DefaultVariables
  15. public override string name { get => $"{operationType.ToString()} StatusEffect \'{GetStatuseffectName()}\'"; }
  16. public override GameActionsController controller {
  17. get => _controller;
  18. set
  19. {
  20. _controller = value;
  21. typeName = "StatusEffectOperationsGameAction";
  22. }
  23. }
  24. public override string GetTypeName() => "StatusEffectOperationsGameAction";
  25. public override string GetActionName() => "Stats/Status Effect Operation";
  26. #endregion
  27. #region ActionVariables
  28. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Stats/Editor/Icons/Fire.png")] public StatusEffectTemplate statusEffect;
  29. [IconFoldoutGroup("@name"), LabelText("Operation")] public StatOperationType operationType = StatOperationType.Add;
  30. [ValueDropdown("statsControllers", IsUniqueList = false), OnInspectorInit("GetCompatibleVariablenames"), LabelText("Target StatsController")]
  31. [IconFoldoutGroup("@name")]
  32. public string statsControllerVariable;
  33. [IconFoldoutGroup("@name"), ValueDropdown("sourcesList", IsUniqueList = false), OnInspectorInit("GetCompatibleGameObjects"), LabelText("Source")]
  34. public string sourceVariableName;
  35. private IEnumerable statsControllers = new ValueDropdownList<string>();
  36. private IEnumerable sourcesList = new ValueDropdownList<string>();
  37. [IconFoldoutGroup("@name")] public bool debugWarnings = false;
  38. #endregion
  39. #region ActionFunctions
  40. public StatusEffectOperationsGameAction(GameActionsController controller) : base(controller)
  41. {
  42. this.controller = controller;
  43. className = this.GetType().AssemblyQualifiedName;
  44. }
  45. private void GetCompatibleVariablenames()
  46. {
  47. statsControllers = _controller.context.variables
  48. .Where(x => x.GetVariableName() == "StatsController")
  49. .Select(x => new ValueDropdownItem(x.name, x.name));
  50. }
  51. private void GetCompatibleGameObjects()
  52. {
  53. sourcesList = _controller.context.variables
  54. .Where(x => x.GetVariableName() == "GameObject")
  55. .Select(x => new ValueDropdownItem(x.name, x.name));
  56. }
  57. public string GetStatuseffectName()
  58. {
  59. if(statusEffect != null) return statusEffect.title;
  60. else return "";
  61. }
  62. #endregion
  63. #region Flow
  64. public override void Start()
  65. {
  66. StatsController stats = GetVariable<StatsController>(statsControllerVariable, null);
  67. if(stats != null)
  68. {
  69. if(statusEffect != null)
  70. {
  71. GameObject source = GetVariable<GameObject>(sourceVariableName, null);
  72. Transform sourceTransform = null;
  73. if (source != null) sourceTransform = source.transform;
  74. stats.statusEffects.Add(statusEffect, sourceTransform);
  75. }
  76. else if(debugWarnings) Debug.LogWarning($"StatusEffect is missing in StatusEffectOperation GameAction.");
  77. }
  78. else if(debugWarnings) Debug.LogWarning($"Could not find StatsController with GameActionContextVariable named \'{statsControllerVariable}\'.");
  79. _done = true;
  80. _started = true;
  81. }
  82. public override void Update() { }
  83. public override void Restart()
  84. {
  85. _done = false;
  86. _started = false;
  87. }
  88. #endregion
  89. #region utilities
  90. private StatusEffectOperationsGameAction Duplicate(GameActionsController controller = null)
  91. {
  92. StatusEffectOperationsGameAction action = new StatusEffectOperationsGameAction(controller == null ? this.controller : controller);
  93. action.controller = controller;
  94. return action;
  95. }
  96. private T GetVariable<T>(string title, T defaultValue)
  97. {
  98. for (int i = 0; i < controller.context.variables.Count; i++)
  99. {
  100. if(controller.context.variables[i].name == title)
  101. {
  102. return controller.context.variables[i].GetValue<T>(defaultValue);
  103. }
  104. }
  105. return defaultValue;
  106. }
  107. #endregion
  108. #region Serialization
  109. public static StatusEffectOperationsGameAction JSONToStatusEffectOperationsGameAction(string data)
  110. {
  111. return JsonUtility.FromJson<StatusEffectOperationsGameAction>(data);
  112. }
  113. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
  114. {
  115. serializer.SerializeScriptableObject($"{depth}-{n}-statusEffect", statusEffect);
  116. }
  117. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
  118. {
  119. statusEffect = (StatusEffectTemplate)serializer.DeserializeScriptableObject($"{depth}-{n}-statusEffect");
  120. }
  121. #endregion
  122. }
  123. }