CapersProject/Assets/02.Scripts/Character/Player/PlayerHealthPoint.cs

181 lines
5.7 KiB
C#
Raw Normal View History

2024-06-03 18:26:03 +00:00
using System.Collections;
using BlueWater.Audios;
using BlueWater.Interfaces;
using BlueWater.Utility;
using Sirenix.OdinInspector;
2024-06-03 18:26:03 +00:00
using UnityEngine;
namespace BlueWater.Players
{
public class PlayerHealthPoint : MonoBehaviour, IDamageable
{
// Components
private IDashable _dashable;
private ISkillHandler _skillHandler;
2024-06-03 18:26:03 +00:00
// Variables
[field: SerializeField]
public int MaxHealthPoint { get; private set; }
[field: SerializeField]
public int CurrentHealthPoint { get; private set; }
[field: SerializeField]
public float InvincibilityDuration { get; private set; } = 0.5f;
2024-06-03 18:26:03 +00:00
[field: SerializeField]
public bool IsInvincible { get; private set; }
2024-11-17 15:36:08 +00:00
2024-12-19 14:27:01 +00:00
[SerializeField]
private float _flashTime = 0.3f;
2024-12-06 13:20:10 +00:00
[SerializeField]
private bool _isShaking;
[SerializeField, ShowIf("@_isShaking")]
private float _shakingPower = 0.5f;
[SerializeField, ShowIf("@_isShaking")]
private float _shakingDuration = 0.1f;
[SerializeField]
private string attackedSfxName = "CombatPlayerAttacked";
2024-11-17 15:36:08 +00:00
[SerializeField]
private string heartRecoverySfxName;
2024-12-06 13:20:10 +00:00
private Material _materialInstance;
2024-06-03 18:26:03 +00:00
private WaitForSeconds _flashWhiteWaitTime;
private Coroutine _flashWhiteCoroutine;
private Coroutine _damageIntervalCoroutine;
// Hashes
2024-09-23 02:00:21 +00:00
private static readonly int IsHitHash = Shader.PropertyToID("_IsHit");
2024-06-03 18:26:03 +00:00
// Unity events
private void Awake()
{
InitializeComponents();
}
2024-06-03 18:26:03 +00:00
private void Start()
{
2024-12-19 14:27:01 +00:00
_flashWhiteWaitTime = new WaitForSeconds(_flashTime);
2024-06-03 18:26:03 +00:00
SetCurrentHealthPoint(MaxHealthPoint);
}
// Initialize methods
[Button("컴포넌트 초기화")]
private void InitializeComponents()
2024-06-03 18:26:03 +00:00
{
_dashable = GetComponent<IDashable>();
_skillHandler = GetComponent<ISkillHandler>();
2024-06-03 18:26:03 +00:00
}
// Methods
2024-09-23 02:00:21 +00:00
public void SetMaxHealthPoint(int changedHealthPoint)
{
2024-10-10 05:44:37 +00:00
var previousMaxHealthPoint = MaxHealthPoint;
2024-09-23 02:00:21 +00:00
var newChangedHealthPoint = Mathf.Clamp(changedHealthPoint, 0, 10);
MaxHealthPoint = newChangedHealthPoint;
2024-10-10 09:32:18 +00:00
EventManager.InvokeMaxHealthChanged(previousMaxHealthPoint, newChangedHealthPoint);
2024-09-23 02:00:21 +00:00
}
2024-06-03 18:26:03 +00:00
public void SetCurrentHealthPoint(int changedHealthPoint)
{
2024-11-17 15:36:08 +00:00
int newChangedHealthPoint = Mathf.Clamp(changedHealthPoint, 0, MaxHealthPoint);
int addedHealthPoint = newChangedHealthPoint - CurrentHealthPoint;
CurrentHealthPoint = newChangedHealthPoint;
2024-11-17 15:36:08 +00:00
if (addedHealthPoint > 0)
{
if (!string.IsNullOrEmpty(heartRecoverySfxName))
{
AudioManager.Instance.PlaySfx(heartRecoverySfxName);
}
}
2024-10-10 09:32:18 +00:00
EventManager.InvokeHealthChanged(newChangedHealthPoint);
2024-06-03 18:26:03 +00:00
if (CurrentHealthPoint <= 2)
{
PostProcessingManager.Instance.LowHpVignette();
2024-06-03 18:26:03 +00:00
}
else
{
PostProcessingManager.Instance.DefaultHpVignette();
2024-06-03 18:26:03 +00:00
}
}
public bool CanDamage()
{
if (IsInvincible) return false;
var isDashing = _dashable?.IsDashing ?? false;
var isActivatingSkill = _skillHandler?.IsActivatingSkill ?? false;
return !isDashing && !isActivatingSkill;
2024-06-03 18:26:03 +00:00
}
2024-12-06 13:20:10 +00:00
[Button("데미지 테스트")]
2024-06-03 18:26:03 +00:00
public void TakeDamage(int damageAmount)
{
2024-12-06 13:20:10 +00:00
AudioManager.Instance.PlaySfx(attackedSfxName);
IsInvincible = true;
2024-06-03 18:26:03 +00:00
var changeHp = Mathf.Max(CurrentHealthPoint - damageAmount, 0);
SetCurrentHealthPoint(changeHp);
2024-12-06 13:20:10 +00:00
if (_isShaking)
{
VisualFeedbackManager.Instance.CameraShake(TycoonCameraManager.Instance.BaseCamera, _shakingPower, _shakingDuration);
}
2024-06-03 18:26:03 +00:00
// 죽었는지 체크
if (changeHp == 0f)
{
Die();
return;
}
2024-12-06 13:20:10 +00:00
if (_materialInstance.HasInt(IsHitHash))
2024-06-03 18:26:03 +00:00
{
Utils.StartUniqueCoroutine(this, ref _flashWhiteCoroutine, FlashWhiteCoroutine());
}
Utils.StartUniqueCoroutine(this, ref _damageIntervalCoroutine, Utils.CoolDownCoroutine(InvincibilityDuration, EndDamageIntervalCoroutine));
2024-06-03 18:26:03 +00:00
}
public void TryTakeDamage(int damageAmount)
{
if (!CanDamage()) return;
TakeDamage(damageAmount);
}
public void Die()
{
2024-10-10 09:32:18 +00:00
EventManager.InvokeDead();
2024-06-03 18:26:03 +00:00
}
private IEnumerator FlashWhiteCoroutine()
{
for (var i = 0; i < 5; i++)
{
2024-12-06 13:20:10 +00:00
_materialInstance.SetInt(IsHitHash, 1);
2024-06-03 18:26:03 +00:00
yield return _flashWhiteWaitTime;
2024-12-06 13:20:10 +00:00
_materialInstance.SetInt(IsHitHash, 0);
2024-06-03 18:26:03 +00:00
yield return _flashWhiteWaitTime;
}
Utils.EndUniqueCoroutine(this, ref _flashWhiteCoroutine);
}
private void EndDamageIntervalCoroutine()
{
IsInvincible = false;
2024-06-03 18:26:03 +00:00
Utils.EndUniqueCoroutine(this, ref _damageIntervalCoroutine);
}
public void ActivateInvincibility() => IsInvincible = true;
public void DeactivateInvincibility() => IsInvincible = false;
2024-12-06 13:20:10 +00:00
public void SetMaterialInstance(Material materialInstance) => _materialInstance = materialInstance;
2024-06-03 18:26:03 +00:00
}
}