123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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<string>();
- private IEnumerable sourcesList = new ValueDropdownList<string>();
- [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<StatsController>(statsControllerVariable, null);
- if(stats != null)
- {
- if(statusEffect != null)
- {
- GameObject source = GetVariable<GameObject>(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<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 StatusEffectOperationsGameAction JSONToStatusEffectOperationsGameAction(string data)
- {
- return JsonUtility.FromJson<StatusEffectOperationsGameAction>(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
- }
- }
|