123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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<string>();
- [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<StatsController>(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<T>(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<T>(defaultValue);
- }
- }
- return defaultValue;
- }
- #endregion
- #region Serialization
- public static StatModifierOperationsGameAction JSONToStatModifierOperationsGameAction(string data)
- {
- return JsonUtility.FromJson<StatModifierOperationsGameAction>(data);
- }
- public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { }
- public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { }
- #endregion
- }
- }
|