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