using System; using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; using KairoEngine.Core.GameActions; namespace KairoEngine.Stats.GameActions { public enum StatOperationType { Add, Remove } public enum StatModifierContextType { New, Variable } [System.Serializable, HideReferenceObjectPicker] public class StatModifierOperationsGameAction : GameActionBase { #region DefaultVariables public override string name { get => $"{operationType.ToString()} StatModifier \'{GetStatModifierText()}\'"; } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "StatModifierOperationsGameAction"; } } public override string GetTypeName() => "StatModifierOperationsGameAction"; public override string GetActionName() => "Stats/ Stat Modifier Operation"; #endregion #region ActionVariables [LabelText("Context"), HideInInspector] public StatModifierContextType statModifierContext = StatModifierContextType.New; [InlineProperty, HideLabel, HideReferenceObjectPicker, ShowIf("@statModifierContext == StatModifierContextType.New")] [IconFoldoutGroup("@name", "Assets/Packages/Simple Vector Icons/PNG icons/Game PNG/Diamonds.png")] public StatModifier statModifier = new StatModifier(); [IconFoldoutGroup("@name"), LabelText("operation")] public StatOperationType operationType = StatOperationType.Add; [ValueDropdown("statsControllers", IsUniqueList = false), OnInspectorInit("GetCompatibleVariablenames"), LabelText("Target StatsController")] [IconFoldoutGroup("@name")] public string statsControllerVariable; private IEnumerable statsControllers = new ValueDropdownList(); [IconFoldoutGroup("@name")] public bool debugWarnings = false; #endregion #region ActionFunctions public StatModifierOperationsGameAction(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)); } public string GetStatModifierText() { if(statModifier == null) return ""; else return statModifier.text; } #endregion #region Flow public override void Start() { StatsController stats = GetVariable(statsControllerVariable, null); if(stats != null) { if(statModifier != null) { Stat stat = stats.GetStat(statModifier.statName); if(stat == null && debugWarnings) Debug.LogWarning($"Could not find Stat \'{statModifier.statName}\' in StatsController."); else { statModifier.enabled = true; if(operationType == StatOperationType.Add) stat.modifiers.Add(statModifier); else stat.modifiers.Remove(statModifier); } } else if(debugWarnings) Debug.LogWarning($"StatModifier is empty in StatModifier operations Game Action."); } 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 StatModifierOperationsGameAction Duplicate(GameActionsController controller = null) { StatModifierOperationsGameAction action = new StatModifierOperationsGameAction(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 StatModifierOperationsGameAction JSONToStatModifierOperationsGameAction(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 } }