StatusEffect.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core.GameActions;
  6. using KairoEngine.Stats.GameActions;
  7. namespace KairoEngine.Stats
  8. {
  9. [System.Serializable]
  10. public class StatusEffect
  11. {
  12. [SerializeField, HideInInspector] private string name = "";
  13. [FoldoutGroup("@name")] public StatusEffectTemplate template;
  14. [FoldoutGroup("@name")] public Transform target;
  15. [FoldoutGroup("@name")] public Transform source;
  16. [FoldoutGroup("@name")] public int stack = 1;
  17. [SerializeField, HideInInspector] private List<GameActionsController> actionsControllerList;
  18. [SerializeField, HideInInspector] private GameActionContext context;
  19. [SerializeField, HideInInspector] private List<float> time;
  20. public StatusEffect(StatusEffectTemplate template, Transform target = null,
  21. Transform source = null, StatsController statsController = null)
  22. {
  23. this.stack = 1;
  24. this.name = $"{template.title} ({stack})";
  25. this.time = new List<float>();
  26. this.time.Add(0);
  27. this.target = target;
  28. this.source = source;
  29. this.template = template;
  30. this.actionsControllerList = new List<GameActionsController>();
  31. this.actionsControllerList.Add(template.onAddController.Duplicate());
  32. this.actionsControllerList.Add(template.onStackController.Duplicate());
  33. this.actionsControllerList.Add(template.onWhileController.Duplicate());
  34. this.actionsControllerList.Add(template.onDestackController.Duplicate());
  35. this.actionsControllerList.Add(template.onRemoveController.Duplicate());
  36. this.context = template.context.Duplicate();
  37. foreach (var ctrl in actionsControllerList) ctrl.context = this.context;
  38. // Set Game Action Variables
  39. var targetVar = context.GetVariable("Target GameObject");
  40. if(targetVar != null) ((GameActionContextGameObject)targetVar).value = this.target.gameObject;
  41. var statsVar = context.GetVariable("Stats");
  42. if(statsVar != null) ((GameActionContextStatsController)statsVar).value = statsController;
  43. actionsControllerList[0].Start(); // Run the OnAdd actions
  44. }
  45. public void AddToStack()
  46. {
  47. if(template.maxStack <= stack) return;
  48. this.stack += 1;
  49. this.name = $"{template.title} ({stack})";
  50. this.time.Add(0);
  51. this.actionsControllerList[1].Restart();
  52. this.actionsControllerList[1].Start(); // Run the OnStack actions
  53. }
  54. public void OnRemove()
  55. {
  56. this.actionsControllerList[4].Start(); // Run OnRemove actions
  57. }
  58. /// <summary>Update times for each unit in the stack. If a unit has reached the template duration,
  59. /// it is removed from the stack and the OnDestack actions are run. </summary>
  60. /// <param name="elapsed">How much time has passed since the last update. Just pass Time.deltaTime.</param>
  61. public void Update(float elapsed)
  62. {
  63. if(this.template.hasDuration == false) return;
  64. for (int i = 0; i < this.time.Count; i++) this.time[i] += elapsed;
  65. for (int i = 0; i < this.time.Count; i++)
  66. {
  67. if(this.time[i] > template.duration)
  68. {
  69. this.time.RemoveAt(i);
  70. this.stack -= 1;
  71. this.name = $"{template.title} ({stack})";
  72. if(stack > 0)
  73. {
  74. this.actionsControllerList[3].Restart();
  75. this.actionsControllerList[3].Start(); // Tun OnDestack actions;
  76. }
  77. else OnRemove();
  78. }
  79. }
  80. if(this.actionsControllerList[2].isDone || !this.actionsControllerList[2].started)
  81. {
  82. this.actionsControllerList[2].Restart();
  83. this.actionsControllerList[2].Start(); // Run OnWhile actions
  84. }
  85. }
  86. }
  87. }