55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
public class PlayerHitHandler {
|
|
|
|
}
|
|
|
|
public class PlayerHealth {
|
|
public int MaxHealth { get; private set; }
|
|
public int CurrentHealth { get; private set; }
|
|
public bool IsDead => CurrentHealth <= 0;
|
|
|
|
public bool IsInvincible => Time.time < invincibleUntil;
|
|
private float invincibleUntil = 0f;
|
|
|
|
private float invincibilityDuration = 1f; // seconds
|
|
|
|
public event Action<int, Vector2> OnTakeDamage;
|
|
public event Action OnDeath;
|
|
public event Action<int> OnHeal;
|
|
|
|
public PlayerHealth(int maxHealth, float invincibilityDuration = 1f) {
|
|
MaxHealth = maxHealth;
|
|
CurrentHealth = maxHealth;
|
|
this.invincibilityDuration = invincibilityDuration;
|
|
}
|
|
|
|
public void TakeDamage(int amount, Vector2 hitDirection) {
|
|
if (IsDead || IsInvincible) return;
|
|
|
|
CurrentHealth -= amount;
|
|
CurrentHealth = Mathf.Max(CurrentHealth, 0);
|
|
invincibleUntil = Time.time + invincibilityDuration;
|
|
|
|
OnTakeDamage?.Invoke(amount, hitDirection);
|
|
|
|
if (CurrentHealth == 0) {
|
|
OnDeath?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void Heal(int amount) {
|
|
if (IsDead) return;
|
|
|
|
CurrentHealth += amount;
|
|
CurrentHealth = Mathf.Min(CurrentHealth, MaxHealth);
|
|
OnHeal?.Invoke(amount);
|
|
}
|
|
|
|
public void Reset() {
|
|
CurrentHealth = MaxHealth;
|
|
invincibleUntil = 0f;
|
|
}
|
|
}
|