HealthBarController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.CharacterSystem
  6. {
  7. public class HealthBarController : MonoBehaviour
  8. {
  9. public GameObject healthBarUiPrefab;
  10. public Transform canvasParent;
  11. private Dictionary<DamageController, HealthBarUi> healthBars;
  12. private List<HealthBarUi> unusedHealthBars;
  13. void OnEnable()
  14. {
  15. DamageEvents.OnDamage += OnDamage;
  16. DamageEvents.OnHeal += OnHeal;
  17. }
  18. void OnDisable()
  19. {
  20. DamageEvents.OnDamage -= OnDamage;
  21. DamageEvents.OnHeal -= OnHeal;
  22. }
  23. void Start()
  24. {
  25. healthBars = new Dictionary<DamageController, HealthBarUi>();
  26. unusedHealthBars = new List<HealthBarUi>();
  27. unusedHealthBars.Add(CreateHealthBar());
  28. }
  29. void Update()
  30. {
  31. RemoveUnused();
  32. }
  33. private void OnDamage(DamageData damageData)
  34. {
  35. //Debug.Log("Showing Health Bar for " + damageController.character.unique_name);
  36. HealthBarUi healthBar = GetHealthBarFor(damageData.damageController);
  37. healthBar.Setup(damageData.damageController);
  38. healthBar.gameObject.SetActive(true);
  39. }
  40. private void OnHeal(DamageController damageController, int damage)
  41. {
  42. //Debug.Log("Showing Health Bar for " + damageController.character.unique_name);
  43. HealthBarUi healthBar = GetHealthBarFor(damageController);
  44. healthBar.Setup(damageController);
  45. healthBar.gameObject.SetActive(true);
  46. }
  47. private HealthBarUi CreateHealthBar()
  48. {
  49. GameObject obj = Instantiate(healthBarUiPrefab, canvasParent);
  50. obj.SetActive(false);
  51. return obj.GetComponent<HealthBarUi>();
  52. }
  53. private HealthBarUi GetHealthBarFor(DamageController damageController)
  54. {
  55. // Find Health Bar in use
  56. HealthBarUi healthBar;
  57. if(healthBars.TryGetValue(damageController, out healthBar))
  58. {
  59. if(healthBar != null) return healthBar;
  60. }
  61. // Look for Health Bar in unused
  62. if(unusedHealthBars.Count > 0)
  63. {
  64. healthBar = unusedHealthBars[unusedHealthBars.Count - 1];
  65. unusedHealthBars.RemoveAt(unusedHealthBars.Count - 1);
  66. }
  67. else
  68. {
  69. // Create new Health Bar
  70. healthBar = CreateHealthBar();
  71. }
  72. healthBars.Add(damageController, healthBar);
  73. return healthBar;
  74. }
  75. private void RemoveUnused()
  76. {
  77. List<DamageController> removeList = new List<DamageController>();
  78. List<HealthBarUi> moveList = new List<HealthBarUi>();
  79. foreach(KeyValuePair<DamageController, HealthBarUi> healthBar in healthBars)
  80. {
  81. if(healthBar.Value.gameObject.activeSelf == false)
  82. {
  83. removeList.Add(healthBar.Key);
  84. moveList.Add(healthBar.Value);
  85. }
  86. }
  87. for (int i = 0; i < removeList.Count; i++)
  88. {
  89. healthBars.Remove(removeList[i]);
  90. unusedHealthBars.Add(moveList[i]);
  91. }
  92. }
  93. }
  94. }