CapersProject/Assets/02.Scripts/Prop/DamageableProps.cs
Nam Tae Gun d76915283a #19 Rockfall 바닥 뚫는 버그 및 무한루프 버그 수정
+ DamageableProps Enable()함수에 Max체력으로 변경하는 로직 추거

Closes #19
2024-06-16 16:55:58 +09:00

76 lines
1.8 KiB
C#

using System;
using BlueWater.Audios;
using BlueWater.Interfaces;
using Sirenix.OdinInspector;
using UnityEngine;
namespace BlueWater
{
public class DamageableProps : MonoBehaviour, IDamageable
{
[field: Title("체력")]
[field: SerializeField]
public int MaxHealthPoint { get; private set; } = 1;
[field: SerializeField]
public int CurrentHealthPoint { get; private set; }
[Title("Die 설정")]
[SerializeField]
private string _dieSfxName;
[SerializeField]
private ParticleSystem _dieParticle;
protected Transform SpawnLocation;
protected virtual void OnEnable()
{
SetCurrentHealthPoint(MaxHealthPoint);
}
public virtual void SetCurrentHealthPoint(int changedHealthPoint)
{
CurrentHealthPoint = changedHealthPoint;
}
public virtual bool CanDamage()
{
return true;
}
public virtual void TakeDamage(int damageAmount)
{
var changeHp = Mathf.Max(CurrentHealthPoint - damageAmount, 0);
SetCurrentHealthPoint(changeHp);
if (changeHp == 0f)
{
Die();
return;
}
}
public virtual void TryTakeDamage(int damageAmount)
{
if (!CanDamage()) return;
TakeDamage(damageAmount);
}
public virtual void Die()
{
if (!string.IsNullOrEmpty(_dieSfxName))
{
AudioManager.Instance.PlaySfx(_dieSfxName);
}
if (_dieParticle && SpawnLocation)
{
Instantiate(_dieParticle, transform.position, Quaternion.identity, SpawnLocation);
}
Destroy(gameObject);
}
}
}