40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerHealthController : MonoBehaviour
|
|
{
|
|
public static PlayerHealthController instance; // Singleton instance for easy access
|
|
private void Awake()
|
|
{
|
|
instance = this; // Set the singleton instance
|
|
}
|
|
public float maxHealth;
|
|
private float currentHealth;
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
currentHealth = maxHealth; // Initialize current health to maximum health
|
|
UIController.instance.UpdateHealthText(currentHealth); // Update health text in the UI
|
|
}
|
|
|
|
public void TakeDamage(float damageAmount)
|
|
{
|
|
currentHealth -= damageAmount; // Reduce current health by the damage amount
|
|
if (currentHealth <= 0)
|
|
{
|
|
currentHealth = 0; // Ensure current health does not go below zero
|
|
PlayerController.instance.isDead = true; // Set player as dead if health reaches zero
|
|
UIController.instance.ShowDeathScreen(); // Show the death screen
|
|
}
|
|
UIController.instance.UpdateHealthText(currentHealth); // Update health text in the UI
|
|
}
|
|
public void Heal(float healAmount)
|
|
{
|
|
currentHealth += healAmount; // Increase current health by the heal amount
|
|
if (currentHealth > maxHealth)
|
|
{
|
|
currentHealth = maxHealth; // Ensure current health does not exceed maximum health
|
|
}
|
|
UIController.instance.UpdateHealthText(currentHealth); // Update health text in the UI
|
|
}
|
|
}
|