CapersProject/Assets/02.Scripts/Prop/DamageableProps.cs

80 lines
2.0 KiB
C#
Raw Normal View History

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; }
public bool IsInvincible { get; private set; }
[field: SerializeField]
public float InvincibilityDuration { get; private set; }
[Title("Die 설정")]
[SerializeField]
protected string DieSfxName;
[SerializeField]
protected ParticleSystem DieParticle;
protected Transform SpawnLocation;
protected virtual void OnEnable()
{
SetCurrentHealthPoint(MaxHealthPoint);
}
public virtual void SetCurrentHealthPoint(int changedHealthPoint)
{
CurrentHealthPoint = changedHealthPoint;
}
public virtual bool CanDamage()
{
return CurrentHealthPoint > 0;
}
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);
}
}
}