using System; using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core.GameActions; using KairoEngine.Core; namespace KairoEngine.Stats.GameActions { [System.Serializable, HideReferenceObjectPicker] public class StatusEffectOperationsGameAction : GameActionBase { #region DefaultVariables public override string name { get => $"{operationType.ToString()} StatusEffect \'{GetStatuseffectName()}\'"; } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "StatusEffectOperationsGameAction"; } } public override string GetTypeName() => "StatusEffectOperationsGameAction"; public override string GetActionName() => "Stats/Status Effect Operation"; #endregion #region ActionVariables [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Stats/Editor/Icons/Fire.png")] public StatusEffectTemplate statusEffect; [IconFoldoutGroup("@name"), LabelText("Operation")] public StatOperationType operationType = StatOperationType.Add; [ValueDropdown("statsControllers", IsUniqueList = false), OnInspectorInit("GetCompatibleVariablenames"), LabelText("Target StatsController")] [IconFoldoutGroup("@name")] public string statsControllerVariable; [IconFoldoutGroup("@name"), ValueDropdown("sourcesList", IsUniqueList = false), OnInspectorInit("GetCompatibleGameObjects"), LabelText("Source")] public string sourceVariableName; private IEnumerable statsControllers = new ValueDropdownList(); private IEnumerable sourcesList = new ValueDropdownList(); [IconFoldoutGroup("@name")] public bool debugWarnings = false; #endregion #region ActionFunctions public StatusEffectOperationsGameAction(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)); } private void GetCompatibleGameObjects() { sourcesList = _controller.context.variables .Where(x => x.GetVariableName() == "GameObject") .Select(x => new ValueDropdownItem(x.name, x.name)); } public string GetStatuseffectName() { if(statusEffect != null) return statusEffect.title; else return ""; } #endregion #region Flow public override void Start() { StatsController stats = GetVariable(statsControllerVariable, null); if(stats != null) { if(statusEffect != null) { GameObject source = GetVariable(sourceVariableName, null); Transform sourceTransform = null; if (source != null) sourceTransform = source.transform; stats.statusEffects.Add(statusEffect, sourceTransform); } else if(debugWarnings) Debug.LogWarning($"StatusEffect is missing in StatusEffectOperation GameAction."); } 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 StatusEffectOperationsGameAction Duplicate(GameActionsController controller = null) { StatusEffectOperationsGameAction action = new StatusEffectOperationsGameAction(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 StatusEffectOperationsGameAction JSONToStatusEffectOperationsGameAction(string data) { return JsonUtility.FromJson(data); } public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { serializer.SerializeScriptableObject($"{depth}-{n}-statusEffect", statusEffect); } public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { statusEffect = (StatusEffectTemplate)serializer.DeserializeScriptableObject($"{depth}-{n}-statusEffect"); } #endregion } }