StatChangeValueGameAction.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. namespace KairoEngine.Stats.GameActions
  9. {
  10. [System.Serializable, HideReferenceObjectPicker]
  11. public class StatChangeValueGameAction : GameActionBase
  12. {
  13. #region DefaultVariables
  14. public override string name { get => $"Change stat \'{statName}\' value by {statValueChange}"; }
  15. public override GameActionsController controller {
  16. get => _controller;
  17. set
  18. {
  19. _controller = value;
  20. typeName = "StatChangeValueGameAction";
  21. }
  22. }
  23. public override string GetTypeName() => "StatChangeValueGameAction";
  24. public override string GetActionName() => "Stats/Change Stat Value";
  25. #endregion
  26. #region ActionVariables
  27. [FoldoutGroup("@name")] public string statName = "";
  28. [FoldoutGroup("@name")] public int statValueChange = +1;
  29. [ValueDropdown("statsControllers", IsUniqueList = false), OnInspectorInit("GetCompatibleVariablenames"), LabelText("Target StatsController")]
  30. [FoldoutGroup("@name")]
  31. public string statsControllerVariable;
  32. private IEnumerable statsControllers = new ValueDropdownList<string>();
  33. [FoldoutGroup("@name")] public bool debugWarnings = false;
  34. #endregion
  35. #region ActionFunctions
  36. public StatChangeValueGameAction(GameActionsController controller) : base(controller)
  37. {
  38. this.controller = controller;
  39. className = this.GetType().AssemblyQualifiedName;
  40. }
  41. private void GetCompatibleVariablenames()
  42. {
  43. statsControllers = _controller.context.variables
  44. .Where(x => x.GetVariableName() == "StatsController")
  45. .Select(x => new ValueDropdownItem(x.name, x.name));
  46. }
  47. #endregion
  48. #region Flow
  49. public override void Start()
  50. {
  51. StatsController stats = GetVariable<StatsController>(statsControllerVariable, null);
  52. if(stats != null)
  53. {
  54. Stat stat = stats.GetStat(statName);
  55. if(stat != null)
  56. {
  57. stat.value += statValueChange;
  58. }
  59. else if(debugWarnings) Debug.LogWarning($"Stat \'{statName}\' could not be found in \'{statsControllerVariable}\'.");
  60. }
  61. else if(debugWarnings) Debug.LogWarning($"Could not find StatsController with GameActionContextVariable named \'{statsControllerVariable}\'.");
  62. _done = true;
  63. _started = true;
  64. }
  65. public override void Update() { }
  66. public override void Restart()
  67. {
  68. _done = false;
  69. _started = false;
  70. }
  71. #endregion
  72. #region utilities
  73. private StatChangeValueGameAction Duplicate(GameActionsController controller = null)
  74. {
  75. StatChangeValueGameAction action = new StatChangeValueGameAction(controller == null ? this.controller : controller);
  76. action.controller = controller;
  77. return action;
  78. }
  79. private T GetVariable<T>(string title, T defaultValue)
  80. {
  81. for (int i = 0; i < controller.context.variables.Count; i++)
  82. {
  83. if(controller.context.variables[i].name == title)
  84. {
  85. return controller.context.variables[i].GetValue<T>(defaultValue);
  86. }
  87. }
  88. return defaultValue;
  89. }
  90. #endregion
  91. #region Serialization
  92. public static StatChangeValueGameAction JSONToStatChangeValueGameAction(string data)
  93. {
  94. return JsonUtility.FromJson<StatChangeValueGameAction>(data);
  95. }
  96. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { }
  97. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { }
  98. #endregion
  99. }
  100. }