2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

5 years ago
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. namespace Unity.FPS.Game
  4. {
  5. public class Health : MonoBehaviour
  6. {
  7. [Tooltip("Maximum amount of health")] public float MaxHealth = 10f;
  8. [Tooltip("Health ratio at which the critical health vignette starts appearing")]
  9. public float CriticalHealthRatio = 0.3f;
  10. public UnityAction<float, GameObject> OnDamaged;
  11. public UnityAction<float> OnHealed;
  12. public UnityAction OnDie;
  13. public float CurrentHealth { get; set; }
  14. public bool Invincible { get; set; }
  15. public bool CanPickup() => CurrentHealth < MaxHealth;
  16. public float GetRatio() => CurrentHealth / MaxHealth;
  17. public bool IsCritical() => GetRatio() <= CriticalHealthRatio;
  18. bool m_IsDead;
  19. void Start()
  20. {
  21. CurrentHealth = MaxHealth;
  22. }
  23. public void Heal(float healAmount)
  24. {
  25. float healthBefore = CurrentHealth;
  26. CurrentHealth += healAmount;
  27. CurrentHealth = Mathf.Clamp(CurrentHealth, 0f, MaxHealth);
  28. // call OnHeal action
  29. float trueHealAmount = CurrentHealth - healthBefore;
  30. if (trueHealAmount > 0f)
  31. {
  32. OnHealed?.Invoke(trueHealAmount);
  33. }
  34. }
  35. public void TakeDamage(float damage, GameObject damageSource)
  36. {
  37. if (Invincible)
  38. return;
  39. float healthBefore = CurrentHealth;
  40. CurrentHealth -= damage;
  41. CurrentHealth = Mathf.Clamp(CurrentHealth, 0f, MaxHealth);
  42. // call OnDamage action
  43. float trueDamageAmount = healthBefore - CurrentHealth;
  44. if (trueDamageAmount > 0f)
  45. {
  46. OnDamaged?.Invoke(trueDamageAmount, damageSource);
  47. }
  48. HandleDeath();
  49. }
  50. public void Kill()
  51. {
  52. CurrentHealth = 0f;
  53. // call OnDamage action
  54. OnDamaged?.Invoke(MaxHealth, null);
  55. HandleDeath();
  56. }
  57. void HandleDeath()
  58. {
  59. if (m_IsDead)
  60. return;
  61. // call OnDie action
  62. if (CurrentHealth <= 0f)
  63. {
  64. m_IsDead = true;
  65. OnDie?.Invoke();
  66. }
  67. }
  68. }
  69. }