using System; using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core.GameActions; namespace KairoEngine.Stats.GameActions { [System.Serializable, HideReferenceObjectPicker] public class StatChangeValueGameAction : GameActionBase { #region DefaultVariables public override string name { get => $"Change stat \'{statName}\' value by {statValueChange}"; } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "StatChangeValueGameAction"; } } public override string GetTypeName() => "StatChangeValueGameAction"; public override string GetActionName() => "Stats/Change Stat Value"; #endregion #region ActionVariables [FoldoutGroup("@name")] public string statName = ""; [FoldoutGroup("@name")] public int statValueChange = +1; [ValueDropdown("statsControllers", IsUniqueList = false), OnInspectorInit("GetCompatibleVariablenames"), LabelText("Target StatsController")] [FoldoutGroup("@name")] public string statsControllerVariable; private IEnumerable statsControllers = new ValueDropdownList(); [FoldoutGroup("@name")] public bool debugWarnings = false; #endregion #region ActionFunctions public StatChangeValueGameAction(GameActionsController controller) : base(controller) { this.controller = controller; className = this.GetType().AssemblyQualifiedName; } private void GetCompatibleVariablenames() { statsControllers = _controller.context.variables .Where(x => x.GetVariableName() == "StatsController") .Select(x => new ValueDropdownItem(x.name, x.name)); } #endregion #region Flow public override void Start() { StatsController stats = GetVariable(statsControllerVariable, null); if(stats != null) { Stat stat = stats.GetStat(statName); if(stat != null) { stat.value += statValueChange; } else if(debugWarnings) Debug.LogWarning($"Stat \'{statName}\' could not be found in \'{statsControllerVariable}\'."); } else if(debugWarnings) Debug.LogWarning($"Could not find StatsController with GameActionContextVariable named \'{statsControllerVariable}\'."); _done = true; _started = true; } public override void Update() { } public override void Restart() { _done = false; _started = false; } #endregion #region utilities private StatChangeValueGameAction Duplicate(GameActionsController controller = null) { StatChangeValueGameAction action = new StatChangeValueGameAction(controller == null ? this.controller : controller); action.controller = controller; return action; } private T GetVariable(string title, T defaultValue) { for (int i = 0; i < controller.context.variables.Count; i++) { if(controller.context.variables[i].name == title) { return controller.context.variables[i].GetValue(defaultValue); } } return defaultValue; } #endregion #region Serialization public static StatChangeValueGameAction JSONToStatChangeValueGameAction(string data) { return JsonUtility.FromJson(data); } public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { } public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { } #endregion } }