123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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<string>();
- [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<StatsController>(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<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 StatChangeValueGameAction JSONToStatChangeValueGameAction(string data)
- {
- return JsonUtility.FromJson<StatChangeValueGameAction>(data);
- }
- public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { }
- public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { }
- #endregion
- }
- }
|