StatModifierOperationsGameAction.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. using KairoEngine.Core.GameActions;
  9. namespace KairoEngine.Stats.GameActions
  10. {
  11. public enum StatOperationType
  12. {
  13. Add,
  14. Remove
  15. }
  16. public enum StatModifierContextType
  17. {
  18. New,
  19. Variable
  20. }
  21. [System.Serializable, HideReferenceObjectPicker]
  22. public class StatModifierOperationsGameAction : GameActionBase
  23. {
  24. #region DefaultVariables
  25. public override string name { get => $"{operationType.ToString()} StatModifier \'{GetStatModifierText()}\'"; }
  26. public override GameActionsController controller {
  27. get => _controller;
  28. set
  29. {
  30. _controller = value;
  31. typeName = "StatModifierOperationsGameAction";
  32. }
  33. }
  34. public override string GetTypeName() => "StatModifierOperationsGameAction";
  35. public override string GetActionName() => "Stats/ Stat Modifier Operation";
  36. #endregion
  37. #region ActionVariables
  38. [LabelText("Context"), HideInInspector] public StatModifierContextType statModifierContext = StatModifierContextType.New;
  39. [InlineProperty, HideLabel, HideReferenceObjectPicker, ShowIf("@statModifierContext == StatModifierContextType.New")]
  40. [IconFoldoutGroup("@name", "Assets/Packages/Simple Vector Icons/PNG icons/Game PNG/Diamonds.png")]
  41. public StatModifier statModifier = new StatModifier();
  42. [IconFoldoutGroup("@name"), LabelText("operation")] public StatOperationType operationType = StatOperationType.Add;
  43. [ValueDropdown("statsControllers", IsUniqueList = false), OnInspectorInit("GetCompatibleVariablenames"), LabelText("Target StatsController")]
  44. [IconFoldoutGroup("@name")]
  45. public string statsControllerVariable;
  46. private IEnumerable statsControllers = new ValueDropdownList<string>();
  47. [IconFoldoutGroup("@name")] public bool debugWarnings = false;
  48. #endregion
  49. #region ActionFunctions
  50. public StatModifierOperationsGameAction(GameActionsController controller) : base(controller)
  51. {
  52. this.controller = controller;
  53. className = this.GetType().AssemblyQualifiedName;
  54. }
  55. private void GetCompatibleVariablenames()
  56. {
  57. statsControllers = _controller.context.variables
  58. .Where(x => x.GetVariableName() == "StatsController")
  59. .Select(x => new ValueDropdownItem(x.name, x.name));
  60. }
  61. public string GetStatModifierText()
  62. {
  63. if(statModifier == null) return "";
  64. else return statModifier.text;
  65. }
  66. #endregion
  67. #region Flow
  68. public override void Start()
  69. {
  70. StatsController stats = GetVariable<StatsController>(statsControllerVariable, null);
  71. if(stats != null)
  72. {
  73. if(statModifier != null)
  74. {
  75. Stat stat = stats.GetStat(statModifier.statName);
  76. if(stat == null && debugWarnings) Debug.LogWarning($"Could not find Stat \'{statModifier.statName}\' in StatsController.");
  77. else
  78. {
  79. statModifier.enabled = true;
  80. if(operationType == StatOperationType.Add) stat.modifiers.Add(statModifier);
  81. else stat.modifiers.Remove(statModifier);
  82. }
  83. }
  84. else if(debugWarnings) Debug.LogWarning($"StatModifier is empty in StatModifier operations Game Action.");
  85. }
  86. else if(debugWarnings) Debug.LogWarning($"Could not find StatsController with GameActionContextVariable named \'{statsControllerVariable}\'.");
  87. _done = true;
  88. _started = true;
  89. }
  90. public override void Update() { }
  91. public override void Restart()
  92. {
  93. _done = false;
  94. _started = false;
  95. }
  96. #endregion
  97. #region utilities
  98. private StatModifierOperationsGameAction Duplicate(GameActionsController controller = null)
  99. {
  100. StatModifierOperationsGameAction action = new StatModifierOperationsGameAction(controller == null ? this.controller : controller);
  101. action.controller = controller;
  102. return action;
  103. }
  104. private T GetVariable<T>(string title, T defaultValue)
  105. {
  106. for (int i = 0; i < controller.context.variables.Count; i++)
  107. {
  108. if(controller.context.variables[i].name == title)
  109. {
  110. return controller.context.variables[i].GetValue<T>(defaultValue);
  111. }
  112. }
  113. return defaultValue;
  114. }
  115. #endregion
  116. #region Serialization
  117. public static StatModifierOperationsGameAction JSONToStatModifierOperationsGameAction(string data)
  118. {
  119. return JsonUtility.FromJson<StatModifierOperationsGameAction>(data);
  120. }
  121. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { }
  122. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { }
  123. #endregion
  124. }
  125. }