Files
HacknSlash/Assets/Scripts/PlayerHealth.cs

41 lines
1.1 KiB
C#
Raw Normal View History

2026-06-18 16:37:02 +01:00
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
private int currentHealth;
void Start()
{
currentHealth = maxHealth;
// Force the UI to update on frame 1
UIManager.Instance.UpdatePlayerHealth(currentHealth, maxHealth);
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
currentHealth = Mathf.Max(0, currentHealth); // Prevent negative health
UIManager.Instance.UpdatePlayerHealth(currentHealth, maxHealth);
if (currentHealth <= 0)
{
Die();
}
}
private void Die()
{
Debug.Log("<color=red>Player has fallen! The horde wins.</color>");
// TODO: Trigger player death animation and game over screen
}
public void Heal(int amount)
{
currentHealth += amount;
currentHealth = Mathf.Min(currentHealth, maxHealth); // Prevent overhealing
UIManager.Instance.UpdatePlayerHealth(currentHealth, maxHealth);
Debug.Log($"<color=green>Player Healed for {amount}!</color>");
}
}