123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using UniRx;
- namespace KairoEngine.Stats
- {
- [System.Serializable]
- public class StatusEffectsController
- {
- [System.NonSerialized] public StatsController controller;
- public void SetStatsController(StatsController controller) => this.controller = controller;
-
- [ListDrawerSettings(HideAddButton = true, HideRemoveButton = true, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = false)]
- public List<StatusEffect> statusEffects = new List<StatusEffect>();
- /// <summary>The target Transform to pass on to the GameActionsContext for doing things like spawning particles.</summary>
- [HideInInspector] public Transform target;
- public StatusEffectsController(StatsController controller, List<StatusEffect> statusEffects = null)
- {
- this.controller = controller;
- if(statusEffects == null) statusEffects = new List<StatusEffect>();
- this.statusEffects = statusEffects;
- }
- #region DatabaseOperations
- public void Add(StatusEffectTemplate statusEffectTemplate, Transform source)
- {
- if(statusEffectTemplate == null)
- {
- Debug.LogError("statusEffect cannot be null");
- return;
- }
- StatusEffect statusEffect = GetStatusEffect(statusEffectTemplate.title);
- if(statusEffect == null)
- {
- if(this.controller == null) Debug.LogError("Missing statsController");
- statusEffect = new StatusEffect(statusEffectTemplate, target, source, this.controller);
- statusEffects.Add(statusEffect);
- }
- else
- {
- if(statusEffect.template.maxStack > statusEffect.stack)
- {
- statusEffect.AddToStack();
- }
- else { } // The statusEffect is already added and cannot be stacked
- }
- }
- public StatusEffect GetStatusEffect(string title)
- {
- for (int i = 0; i < statusEffects.Count; i++)
- {
- if(statusEffects[i].template.title == title) return statusEffects[i];
- }
- return null;
- }
- public void Remove(StatusEffect statusEffect)
- {
- statusEffect.OnRemove();
- statusEffects.Remove(statusEffect);
- }
- #endregion
- #region Flow
- private CompositeDisposable cancel;
- /// <summary>Let the StatusController flow in time</summary>
- public void Start()
- {
- var observable = Observable.EveryUpdate();
- cancel = new CompositeDisposable();
- observable.Subscribe(xs => UpdateStatusEffects()).AddTo(cancel);
- }
- /// <summary>Stop this controller.</summary>
- public void Stop()
- {
- if(cancel != null) cancel.Dispose();
- }
- private void UpdateStatusEffects()
- {
- for (int i = 0; i < statusEffects.Count; i++)
- {
- statusEffects[i].Update(Time.deltaTime);
- }
- for (int i = 0; i < statusEffects.Count; i++)
- {
- if(statusEffects[i].stack == 0)
- {
- statusEffects.RemoveAt(i);
- return;
- }
- }
- }
- #endregion
- }
- }
|