63 lines
1.6 KiB
C#
63 lines
1.6 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class HealthManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _maxHealth = 100f;
|
|
[SerializeField] private float _currentHealth = 100f;
|
|
[SerializeField] private TMP_Text _healthText;
|
|
|
|
public float CurrentHealth { get => _currentHealth;}
|
|
public float MaxHealth { get => _maxHealth; }
|
|
|
|
private void Start()
|
|
{
|
|
if(_healthText != null)
|
|
_healthText.text = _currentHealth.ToString() + "/" + _maxHealth.ToString();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_healthText != null)
|
|
_healthText.text = _currentHealth.ToString() + "/" + _maxHealth.ToString();
|
|
|
|
|
|
// These are only for debugging!!!!
|
|
if(Input.GetKeyDown(KeyCode.K))
|
|
if(transform.tag == "Player")
|
|
ModifyHealth(-10);
|
|
|
|
if(Input.GetKeyDown(KeyCode.L))
|
|
ModifyHealth(10);
|
|
|
|
if (Input.GetKeyDown(KeyCode.M))
|
|
if(transform.tag == "Enemy")
|
|
ModifyHealth(-50);
|
|
}
|
|
|
|
public void ModifyHealth(float amount)
|
|
{
|
|
if (_currentHealth + amount > _maxHealth)
|
|
{
|
|
_currentHealth = _maxHealth;
|
|
return;
|
|
}
|
|
if (_currentHealth + amount <= 0)
|
|
{
|
|
_currentHealth = 0;
|
|
if (transform.tag == "Player")
|
|
Debug.Log("Game over bastarde");
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
Debug.Log("Character named " + gameObject.name + " died");
|
|
}
|
|
return;
|
|
}
|
|
_currentHealth += amount;
|
|
|
|
Debug.Log("Health of " + gameObject.name + ": " + _currentHealth);
|
|
}
|
|
|
|
}
|