+ 플레이어가 죽었을 때, grayscale을 이용한 이펙트 추가 + ItemLootInfoUi rectTransform 참조 오류 수정 + MoveTitleScene() 실행될 때, grayscale 비활성화 + 기존의 Vignette기능 카메라매니저 -> 포스트프로세싱매니저 + JumpSlam null 오류들 수정
138 lines
4.1 KiB
C#
138 lines
4.1 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;
|
|
public event Action OnDead;
|
|
|
|
// 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)
|
|
{
|
|
PostProcessingManager.Instance.LowHpVignette();
|
|
}
|
|
else
|
|
{
|
|
PostProcessingManager.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()
|
|
{
|
|
OnDead?.Invoke();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
} |