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.

123 lines
4.3 KiB

5 years ago
  1. using Unity.FPS.Game;
  2. using Unity.FPS.Gameplay;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Unity.FPS.UI
  6. {
  7. public class FeedbackFlashHUD : MonoBehaviour
  8. {
  9. [Header("References")] [Tooltip("Image component of the flash")]
  10. public Image FlashImage;
  11. [Tooltip("CanvasGroup to fade the damage flash, used when recieving damage end healing")]
  12. public CanvasGroup FlashCanvasGroup;
  13. [Tooltip("CanvasGroup to fade the critical health vignette")]
  14. public CanvasGroup VignetteCanvasGroup;
  15. [Header("Damage")] [Tooltip("Color of the damage flash")]
  16. public Color DamageFlashColor;
  17. [Tooltip("Duration of the damage flash")]
  18. public float DamageFlashDuration;
  19. [Tooltip("Max alpha of the damage flash")]
  20. public float DamageFlashMaxAlpha = 1f;
  21. [Header("Critical health")] [Tooltip("Max alpha of the critical vignette")]
  22. public float CriticaHealthVignetteMaxAlpha = .8f;
  23. [Tooltip("Frequency at which the vignette will pulse when at critical health")]
  24. public float PulsatingVignetteFrequency = 4f;
  25. [Header("Heal")] [Tooltip("Color of the heal flash")]
  26. public Color HealFlashColor;
  27. [Tooltip("Duration of the heal flash")]
  28. public float HealFlashDuration;
  29. [Tooltip("Max alpha of the heal flash")]
  30. public float HealFlashMaxAlpha = 1f;
  31. bool m_FlashActive;
  32. float m_LastTimeFlashStarted = Mathf.NegativeInfinity;
  33. Health m_PlayerHealth;
  34. GameFlowManager m_GameFlowManager;
  35. void Start()
  36. {
  37. // Subscribe to player damage events
  38. PlayerCharacterController playerCharacterController = FindObjectOfType<PlayerCharacterController>();
  39. DebugUtility.HandleErrorIfNullFindObject<PlayerCharacterController, FeedbackFlashHUD>(
  40. playerCharacterController, this);
  41. m_PlayerHealth = playerCharacterController.GetComponent<Health>();
  42. DebugUtility.HandleErrorIfNullGetComponent<Health, FeedbackFlashHUD>(m_PlayerHealth, this,
  43. playerCharacterController.gameObject);
  44. m_GameFlowManager = FindObjectOfType<GameFlowManager>();
  45. DebugUtility.HandleErrorIfNullFindObject<GameFlowManager, FeedbackFlashHUD>(m_GameFlowManager, this);
  46. m_PlayerHealth.OnDamaged += OnTakeDamage;
  47. m_PlayerHealth.OnHealed += OnHealed;
  48. }
  49. void Update()
  50. {
  51. if (m_PlayerHealth.IsCritical())
  52. {
  53. VignetteCanvasGroup.gameObject.SetActive(true);
  54. float vignetteAlpha =
  55. (1 - (m_PlayerHealth.CurrentHealth / m_PlayerHealth.MaxHealth /
  56. m_PlayerHealth.CriticalHealthRatio)) * CriticaHealthVignetteMaxAlpha;
  57. if (m_GameFlowManager.GameIsEnding)
  58. VignetteCanvasGroup.alpha = vignetteAlpha;
  59. else
  60. VignetteCanvasGroup.alpha =
  61. ((Mathf.Sin(Time.time * PulsatingVignetteFrequency) / 2) + 0.5f) * vignetteAlpha;
  62. }
  63. else
  64. {
  65. VignetteCanvasGroup.gameObject.SetActive(false);
  66. }
  67. if (m_FlashActive)
  68. {
  69. float normalizedTimeSinceDamage = (Time.time - m_LastTimeFlashStarted) / DamageFlashDuration;
  70. if (normalizedTimeSinceDamage < 1f)
  71. {
  72. float flashAmount = DamageFlashMaxAlpha * (1f - normalizedTimeSinceDamage);
  73. FlashCanvasGroup.alpha = flashAmount;
  74. }
  75. else
  76. {
  77. FlashCanvasGroup.gameObject.SetActive(false);
  78. m_FlashActive = false;
  79. }
  80. }
  81. }
  82. void ResetFlash()
  83. {
  84. m_LastTimeFlashStarted = Time.time;
  85. m_FlashActive = true;
  86. FlashCanvasGroup.alpha = 0f;
  87. FlashCanvasGroup.gameObject.SetActive(true);
  88. }
  89. void OnTakeDamage(float dmg, GameObject damageSource)
  90. {
  91. ResetFlash();
  92. FlashImage.color = DamageFlashColor;
  93. }
  94. void OnHealed(float amount)
  95. {
  96. ResetFlash();
  97. FlashImage.color = HealFlashColor;
  98. }
  99. }
  100. }