웅냥
This commit is contained in:
@@ -5,21 +5,18 @@ namespace Unicorn
|
||||
{
|
||||
public partial class LivingEntity : MonoBehaviour
|
||||
{
|
||||
public float maxHealth = 20.0f;
|
||||
public float health = 20.0f;
|
||||
public GameObject hpBarPrefab;
|
||||
private Rigidbody2D Rb { get; set; }
|
||||
private bool _invulnerable = false;
|
||||
private HpBar _hpBar;
|
||||
|
||||
private void Start()
|
||||
protected void Start()
|
||||
{
|
||||
Rb = TryGetComponent(out Rigidbody2D rb) ? rb : null;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (Rb == null) return;
|
||||
var transformPosition = other.transform.position - transform.position;
|
||||
Rb.linearVelocity = Vector2.zero;
|
||||
Rb.AddForce(transformPosition.normalized * health, ForceMode2D.Impulse);
|
||||
var hpBar = Instantiate(hpBarPrefab, transform);
|
||||
hpBar.TryGetComponent(out _hpBar);
|
||||
}
|
||||
|
||||
public void Damage(DamageInfo info)
|
||||
@@ -27,6 +24,7 @@ namespace Unicorn
|
||||
if (_invulnerable) return;
|
||||
health -= info.Damage;
|
||||
OnDamaged(info);
|
||||
OnKnockback(info);
|
||||
if (health <= 0.0f) OnDeath();
|
||||
}
|
||||
|
||||
@@ -34,10 +32,14 @@ namespace Unicorn
|
||||
protected virtual void OnDeath() { }
|
||||
|
||||
|
||||
protected virtual void OnDamaged(DamageInfo info) { }
|
||||
protected virtual void OnDamaged(DamageInfo info)
|
||||
{
|
||||
if (_hpBar) _hpBar.UpdateHealth();
|
||||
}
|
||||
|
||||
protected virtual void OnKnockback(DamageInfo info)
|
||||
{
|
||||
|
||||
var dir = transform.position - info.HitPoint;
|
||||
Rb.AddForce(dir.normalized * info.KnockbackForce, ForceMode2D.Impulse);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user