PuzzleGame/PuzzleGameProject/Assets/Scripts/Player.cs

39 lines
914 B
C#

using System;
using Microsoft.Unity.VisualStudio.Editor;
using TMPro;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private int maxHealth;
[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 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();
}
}