PuzzleGame/PuzzleGameProject/Assets/Scripts/Player.cs
2025-02-25 10:41:33 +01:00

68 lines
1.7 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
public class Player : MonoBehaviour
{
[SerializeField] private int maxHealth;
[SerializeField] private int diamonds;
[FormerlySerializedAs("healthGameObject")] [SerializeField] private GameObject healthGO;
[FormerlySerializedAs("DiamondsGO")] [SerializeField] private GameObject diamondsGO;
[SerializeField] private GameObject rooms;
private int _healthIndex = 0;
private readonly int[] _healthBar = {0, 0, -1, -2, -4, -6, -9, -12, -16, -20};
private void OnEnable()
{
RoomRewards.DiamondsRewarded += AddDiamonds;
RoomRewards.DamageDealt += TakeDamage;
ChestRewardSelection.DiamondAndLifeSelected += HandleDiamondAndLifeAbilitySelected;
}
private void OnDisable()
{
RoomRewards.DiamondsRewarded -= AddDiamonds;
RoomRewards.DamageDealt -= TakeDamage;
}
private void Start() {
UpdateGUI();
}
public void AddDiamonds(int count)
{
diamonds += count;
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()
{
healthGO.GetComponent<TextMeshProUGUI>().text = _healthBar[_healthIndex].ToString();
diamondsGO.GetComponent<TextMeshProUGUI>().text = diamonds.ToString();
}
private void HandleDiamondAndLifeAbilitySelected()
{
Heal(1);
AddDiamonds(1);
}
}