139 lines
4.2 KiB
C#
139 lines
4.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using BlueWater.Audios;
|
|
using BlueWater.Interfaces;
|
|
using BlueWater.Uis;
|
|
using BlueWater.Utility;
|
|
using UnityEngine;
|
|
|
|
namespace BlueWater.Players
|
|
{
|
|
public class PlayerHealthPoint : MonoBehaviour, IDamageable
|
|
{
|
|
// Components
|
|
private SpriteRenderer _spriteRenderer;
|
|
|
|
// Variables
|
|
[field: SerializeField]
|
|
public int MaxHealthPoint { get; private set; }
|
|
|
|
[field: SerializeField]
|
|
public int CurrentHealthPoint { get; private set; }
|
|
|
|
[SerializeField]
|
|
private float _damageInterval = 0.5f;
|
|
|
|
private bool _enableTakeDamage = true;
|
|
private WaitForSeconds _flashWhiteWaitTime;
|
|
private Coroutine _flashWhiteCoroutine;
|
|
private Coroutine _damageIntervalCoroutine;
|
|
|
|
// Hashes
|
|
private static readonly int _isHitHash = Shader.PropertyToID("_IsHit");
|
|
|
|
// Events
|
|
public event Action<int> OnHealthChanged;
|
|
|
|
// Unity events
|
|
private void Start()
|
|
{
|
|
OnHealthChanged += CombatUiManager.Instance.PlayerHealthPointUi.SetCurrentHealthPoint;
|
|
|
|
_flashWhiteWaitTime = new WaitForSeconds(_damageInterval * 0.1f);
|
|
SetCurrentHealthPoint(MaxHealthPoint);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (CombatUiManager.Instance && CombatUiManager.Instance.PlayerHealthPointUi)
|
|
{
|
|
OnHealthChanged -= CombatUiManager.Instance.PlayerHealthPointUi.SetCurrentHealthPoint;
|
|
}
|
|
}
|
|
|
|
// Initialize methods
|
|
public void InitializeComponents(SpriteRenderer spriteRenderer)
|
|
{
|
|
_spriteRenderer = spriteRenderer;
|
|
}
|
|
|
|
// Events methods
|
|
public void HandleEnableTakeDamage() => _enableTakeDamage = true;
|
|
public void HandleDisableTakeDamage() => _enableTakeDamage = false;
|
|
|
|
// Methods
|
|
public void SetCurrentHealthPoint(int changedHealthPoint)
|
|
{
|
|
CurrentHealthPoint = changedHealthPoint;
|
|
OnHealthChanged?.Invoke(changedHealthPoint);
|
|
|
|
if (CurrentHealthPoint <= 2)
|
|
{
|
|
CombatCameraManager.Instance.LowHpVignette();
|
|
}
|
|
else
|
|
{
|
|
CombatCameraManager.Instance.DefaultHpVignette();
|
|
}
|
|
}
|
|
|
|
public bool CanDamage()
|
|
{
|
|
return _enableTakeDamage;
|
|
}
|
|
|
|
public void TakeDamage(int damageAmount)
|
|
{
|
|
HandleDisableTakeDamage();
|
|
var changeHp = Mathf.Max(CurrentHealthPoint - damageAmount, 0);
|
|
SetCurrentHealthPoint(changeHp);
|
|
AudioManager.Instance.PlaySfx("CombatPlayerAttacked");
|
|
|
|
// 죽었는지 체크
|
|
if (changeHp == 0f)
|
|
{
|
|
Die();
|
|
return;
|
|
}
|
|
|
|
if (_spriteRenderer.material.HasInt(_isHitHash))
|
|
{
|
|
Utils.StartUniqueCoroutine(this, ref _flashWhiteCoroutine, FlashWhiteCoroutine());
|
|
}
|
|
Utils.StartUniqueCoroutine(this, ref _damageIntervalCoroutine, Utils.CoolDownCoroutine(_damageInterval, EndDamageIntervalCoroutine));
|
|
}
|
|
|
|
public void TryTakeDamage(int damageAmount)
|
|
{
|
|
if (!CanDamage()) return;
|
|
|
|
TakeDamage(damageAmount);
|
|
}
|
|
|
|
public void Die()
|
|
{
|
|
// TODO : 죽는걸 확실히 표현한 후 오브젝트 파괴하기
|
|
CombatUiManager.Instance.GameOverPopupUi.Open();
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private IEnumerator FlashWhiteCoroutine()
|
|
{
|
|
for (var i = 0; i < 5; i++)
|
|
{
|
|
_spriteRenderer.material.SetInt(_isHitHash, 1);
|
|
yield return _flashWhiteWaitTime;
|
|
_spriteRenderer.material.SetInt(_isHitHash, 0);
|
|
yield return _flashWhiteWaitTime;
|
|
}
|
|
|
|
Utils.EndUniqueCoroutine(this, ref _flashWhiteCoroutine);
|
|
}
|
|
|
|
private void EndDamageIntervalCoroutine()
|
|
{
|
|
Utils.EndUniqueCoroutine(this, ref _damageIntervalCoroutine);
|
|
HandleEnableTakeDamage();
|
|
}
|
|
}
|
|
} |