Files
UnicornElimination/Assets/Scripts/Unicorn/LivingEntity.cs

48 lines
1.2 KiB
C#
Raw Normal View History

using Unicorn.Hitbox;
using UnityEngine;
namespace Unicorn
{
public partial class LivingEntity : MonoBehaviour
{
2026-05-10 02:13:54 +09:00
public float maxHealth = 20.0f;
public float health = 20.0f;
2026-05-10 02:13:54 +09:00
public GameObject hpBarPrefab;
private Rigidbody2D Rb { get; set; }
private bool _invulnerable = false;
2026-05-10 02:13:54 +09:00
private HpBar _hpBar;
2026-05-10 02:13:54 +09:00
protected void Start()
{
Rb = TryGetComponent(out Rigidbody2D rb) ? rb : null;
2026-05-10 02:13:54 +09:00
var hpBar = Instantiate(hpBarPrefab, transform);
hpBar.TryGetComponent(out _hpBar);
}
public void Damage(DamageInfo info)
{
if (_invulnerable) return;
health -= info.Damage;
OnDamaged(info);
2026-05-10 02:13:54 +09:00
OnKnockback(info);
if (health <= 0.0f) OnDeath();
}
protected virtual void OnDeath() { }
2026-05-10 02:13:54 +09:00
protected virtual void OnDamaged(DamageInfo info)
{
if (_hpBar) _hpBar.UpdateHealth();
}
protected virtual void OnKnockback(DamageInfo info)
{
2026-05-10 02:13:54 +09:00
var dir = transform.position - info.HitPoint;
Rb.AddForce(dir.normalized * info.KnockbackForce, ForceMode2D.Impulse);
}
}
}