69 lines
1.6 KiB
C#
69 lines
1.6 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class Player : MonoBehaviour
|
|
{
|
|
public event Action<int> DiamondCountUpdated;
|
|
public event Action<int> HealthUpdated;
|
|
|
|
[SerializeField] private int maxHealth;
|
|
[SerializeField] private int diamonds;
|
|
[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()
|
|
{
|
|
DiamondCountUpdated?.Invoke(diamonds);
|
|
HealthUpdated?.Invoke(_healthIndex);
|
|
}
|
|
|
|
private void HandleDiamondAndLifeAbilitySelected()
|
|
{
|
|
Heal(1);
|
|
AddDiamonds(1);
|
|
}
|
|
}
|