Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Scripts / Systems / HealthSystem.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. //using UnityEditor.PackageManager;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. public class HealthSystem : MonoBehaviour
  7. {
  8. private StatusEffects statusEffects;
  9. private float hp;
  10. private bool dead = false;
  11. [SerializeField]
  12. private List<string> bannedTags;
  13. [SerializeField]
  14. private UnityEvent OnDamageTaken;
  15. [SerializeField]
  16. private UnityEvent OnRegenerate;
  17. [SerializeField]
  18. private UnityEvent OnDeath;
  19. private UpdateManager updateManager;
  20. private IUpdatable[] updatables;
  21. public bool immortal = false;
  22. void Start()
  23. {
  24. statusEffects = GetComponent<StatusEffects>();
  25. updatables = GetComponents<IUpdatable>();
  26. if (statusEffects == null)
  27. {
  28. Debug.LogWarning("No status efffect script in this object!");
  29. return;
  30. }
  31. hp = statusEffects.maxHp;
  32. GameObject t = GameObject.Find("UpdateManager");
  33. if (t)
  34. {
  35. updateManager = t.GetComponent<UpdateManager>();
  36. }
  37. }
  38. public float GetHealth() { return hp; }
  39. public void EditHealth(float value)
  40. {
  41. hp = value;
  42. }
  43. public void TakeDamage(float value, GameObject source)
  44. {
  45. if(value < 0)
  46. {
  47. Debug.LogError("Take damage only accepts positive values");
  48. return;
  49. }
  50. if (immortal) return;
  51. foreach(string t in bannedTags)
  52. {
  53. if (source)
  54. {
  55. if (t == source.tag) return;
  56. }
  57. }
  58. if(statusEffects.shield > 0)
  59. {
  60. if(statusEffects.shield >= value )
  61. statusEffects.shield -= value;
  62. else
  63. {
  64. hp += statusEffects.shield - value;
  65. statusEffects.shield = 0;
  66. }
  67. }
  68. else { hp -= value; }
  69. if(hp <= 0)
  70. {
  71. Die();
  72. }
  73. OnDamageTaken.Invoke();
  74. }
  75. public void Die()
  76. {
  77. dead = true;
  78. //Debug.Log("The player went to sleep... Forever.");
  79. OnDeath.Invoke();
  80. }
  81. public void Destroy()
  82. {
  83. if (updateManager != null)
  84. {
  85. foreach (IUpdatable i in updatables)
  86. {
  87. updateManager.RemoveFromList(i);
  88. }
  89. }
  90. Destroy(gameObject);
  91. }
  92. public void Regenerate(float value)
  93. {
  94. if (value < 0)
  95. {
  96. Debug.LogError("Regenerate only accepts positive values");
  97. return;
  98. }
  99. hp += value;
  100. OnRegenerate.Invoke();
  101. }
  102. public float GetHealthPercentage()
  103. {
  104. return (hp / statusEffects.maxHp);
  105. }
  106. public float GetMaxHealth()
  107. {
  108. return statusEffects.maxHp;
  109. }
  110. }