PuzzleGame/PuzzleGameProject/Assets/Scripts/Player.cs
2025-02-24 17:33:00 +01:00

44 lines
993 B
C#

using System;
using TMPro;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private int maxHealth;
[SerializeField] private int diamonds;
[SerializeField] private GameObject healthGameObject;
private int _healthIndex = 0;
private readonly int[] _healthBar = {0, 0, -1, -2, -4, -6, -9, -12, -16, -20};
private void Start() {
UpdateGUI();
}
public void GetDiamonds(int count)
{
diamonds += count;
}
public void TakeDamage(int damage)
{
if (_healthIndex + damage < _healthBar.Length)
{
_healthIndex += damage;
UpdateGUI();
}
}
public void Heal(int healAmount)
{
if (_healthIndex - healAmount >= 0)
{
_healthIndex -= healAmount;
UpdateGUI();
}
}
private void UpdateGUI()
{
healthGameObject.GetComponent<TextMeshProUGUI>().text = _healthBar[_healthIndex].ToString();
}
}