70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
|
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;
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|