StatusEffectsController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using UniRx;
  6. namespace KairoEngine.Stats
  7. {
  8. [System.Serializable]
  9. public class StatusEffectsController
  10. {
  11. [System.NonSerialized] public StatsController controller;
  12. public void SetStatsController(StatsController controller) => this.controller = controller;
  13. [ListDrawerSettings(HideAddButton = true, HideRemoveButton = true, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = false)]
  14. public List<StatusEffect> statusEffects = new List<StatusEffect>();
  15. /// <summary>The target Transform to pass on to the GameActionsContext for doing things like spawning particles.</summary>
  16. [HideInInspector] public Transform target;
  17. public StatusEffectsController(StatsController controller, List<StatusEffect> statusEffects = null)
  18. {
  19. this.controller = controller;
  20. if(statusEffects == null) statusEffects = new List<StatusEffect>();
  21. this.statusEffects = statusEffects;
  22. }
  23. #region DatabaseOperations
  24. public void Add(StatusEffectTemplate statusEffectTemplate, Transform source)
  25. {
  26. if(statusEffectTemplate == null)
  27. {
  28. Debug.LogError("statusEffect cannot be null");
  29. return;
  30. }
  31. StatusEffect statusEffect = GetStatusEffect(statusEffectTemplate.title);
  32. if(statusEffect == null)
  33. {
  34. if(this.controller == null) Debug.LogError("Missing statsController");
  35. statusEffect = new StatusEffect(statusEffectTemplate, target, source, this.controller);
  36. statusEffects.Add(statusEffect);
  37. }
  38. else
  39. {
  40. if(statusEffect.template.maxStack > statusEffect.stack)
  41. {
  42. statusEffect.AddToStack();
  43. }
  44. else { } // The statusEffect is already added and cannot be stacked
  45. }
  46. }
  47. public StatusEffect GetStatusEffect(string title)
  48. {
  49. for (int i = 0; i < statusEffects.Count; i++)
  50. {
  51. if(statusEffects[i].template.title == title) return statusEffects[i];
  52. }
  53. return null;
  54. }
  55. public void Remove(StatusEffect statusEffect)
  56. {
  57. statusEffect.OnRemove();
  58. statusEffects.Remove(statusEffect);
  59. }
  60. #endregion
  61. #region Flow
  62. private CompositeDisposable cancel;
  63. /// <summary>Let the StatusController flow in time</summary>
  64. public void Start()
  65. {
  66. var observable = Observable.EveryUpdate();
  67. cancel = new CompositeDisposable();
  68. observable.Subscribe(xs => UpdateStatusEffects()).AddTo(cancel);
  69. }
  70. /// <summary>Stop this controller.</summary>
  71. public void Stop()
  72. {
  73. if(cancel != null) cancel.Dispose();
  74. }
  75. private void UpdateStatusEffects()
  76. {
  77. for (int i = 0; i < statusEffects.Count; i++)
  78. {
  79. statusEffects[i].Update(Time.deltaTime);
  80. }
  81. for (int i = 0; i < statusEffects.Count; i++)
  82. {
  83. if(statusEffects[i].stack == 0)
  84. {
  85. statusEffects.RemoveAt(i);
  86. return;
  87. }
  88. }
  89. }
  90. #endregion
  91. }
  92. }