131 lines
5.7 KiB
C#
131 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using BaseGames.Core.Events;
|
|
|
|
namespace BaseGames.UI.HUD
|
|
{
|
|
public class HUDController : MonoBehaviour
|
|
{
|
|
[Header("HP")]
|
|
[SerializeField] private Transform _hpContainer;
|
|
[SerializeField] private GameObject _hpCellPrefab;
|
|
|
|
[Header("Gauges")]
|
|
[SerializeField] private Image _soulGaugeFill;
|
|
[SerializeField] private Image _spiritGaugeFill;
|
|
[SerializeField] private TMP_Text _geoText;
|
|
|
|
[Header("Spring Charges")]
|
|
[SerializeField] private Transform _springContainer;
|
|
[SerializeField] private GameObject _springIconPrefab;
|
|
|
|
[Header("Form")]
|
|
[SerializeField] private Image[] _formIcons;
|
|
|
|
[Header("Interact Prompt")]
|
|
[SerializeField] private TMP_Text _interactText;
|
|
[SerializeField] private GameObject _interactPromptRoot;
|
|
|
|
[Header("Event Channels - Subscribe")]
|
|
[SerializeField] private IntEventChannelSO _onHPChanged;
|
|
[SerializeField] private IntEventChannelSO _onMaxHPChanged;
|
|
[SerializeField] private IntEventChannelSO _onSoulPowerChanged;
|
|
[SerializeField] private IntEventChannelSO _onSpiritPowerChanged;
|
|
[SerializeField] private IntEventChannelSO _onGeoChanged;
|
|
[SerializeField] private IntEventChannelSO _onSpringChargesChanged;
|
|
[SerializeField] private IntEventChannelSO _onFormChanged;
|
|
[SerializeField] private StringEventChannelSO _onShowInteractPrompt;
|
|
[SerializeField] private VoidEventChannelSO _onHideInteractPrompt;
|
|
|
|
private readonly List<GameObject> _hpCells = new();
|
|
private readonly List<GameObject> _springIcons = new();
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (_onHPChanged != null) _onHPChanged.OnEventRaised += UpdateHP;
|
|
if (_onMaxHPChanged != null) _onMaxHPChanged.OnEventRaised += RebuildHPCells;
|
|
if (_onSoulPowerChanged != null) _onSoulPowerChanged.OnEventRaised += UpdateSoul;
|
|
if (_onSpiritPowerChanged != null) _onSpiritPowerChanged.OnEventRaised += UpdateSpirit;
|
|
if (_onGeoChanged != null) _onGeoChanged.OnEventRaised += UpdateGeo;
|
|
if (_onSpringChargesChanged != null) _onSpringChargesChanged.OnEventRaised += RebuildSpringIcons;
|
|
if (_onFormChanged != null) _onFormChanged.OnEventRaised += UpdateFormIcon;
|
|
if (_onShowInteractPrompt != null) _onShowInteractPrompt.OnEventRaised += ShowInteractPrompt;
|
|
if (_onHideInteractPrompt != null) _onHideInteractPrompt.OnEventRaised += HideInteractPrompt;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (_onHPChanged != null) _onHPChanged.OnEventRaised -= UpdateHP;
|
|
if (_onMaxHPChanged != null) _onMaxHPChanged.OnEventRaised -= RebuildHPCells;
|
|
if (_onSoulPowerChanged != null) _onSoulPowerChanged.OnEventRaised -= UpdateSoul;
|
|
if (_onSpiritPowerChanged != null) _onSpiritPowerChanged.OnEventRaised -= UpdateSpirit;
|
|
if (_onGeoChanged != null) _onGeoChanged.OnEventRaised -= UpdateGeo;
|
|
if (_onSpringChargesChanged != null) _onSpringChargesChanged.OnEventRaised -= RebuildSpringIcons;
|
|
if (_onFormChanged != null) _onFormChanged.OnEventRaised -= UpdateFormIcon;
|
|
if (_onShowInteractPrompt != null) _onShowInteractPrompt.OnEventRaised -= ShowInteractPrompt;
|
|
if (_onHideInteractPrompt != null) _onHideInteractPrompt.OnEventRaised -= HideInteractPrompt;
|
|
}
|
|
|
|
private void UpdateHP(int current)
|
|
{
|
|
for (int i = 0; i < _hpCells.Count; i++)
|
|
if (_hpCells[i] != null) _hpCells[i].SetActive(i < current);
|
|
}
|
|
|
|
private void RebuildHPCells(int max)
|
|
{
|
|
foreach (var cell in _hpCells)
|
|
if (cell != null) Destroy(cell);
|
|
_hpCells.Clear();
|
|
if (_hpContainer == null || _hpCellPrefab == null) return;
|
|
for (int i = 0; i < max; i++)
|
|
_hpCells.Add(Instantiate(_hpCellPrefab, _hpContainer));
|
|
}
|
|
|
|
private void UpdateSoul(int val)
|
|
{
|
|
if (_soulGaugeFill != null) _soulGaugeFill.fillAmount = val / 100f;
|
|
}
|
|
|
|
private void UpdateSpirit(int val)
|
|
{
|
|
if (_spiritGaugeFill != null) _spiritGaugeFill.fillAmount = val / 100f;
|
|
}
|
|
|
|
private void UpdateGeo(int val)
|
|
{
|
|
if (_geoText != null) _geoText.text = val.ToString();
|
|
}
|
|
|
|
private void RebuildSpringIcons(int charges)
|
|
{
|
|
foreach (var icon in _springIcons)
|
|
if (icon != null) Destroy(icon);
|
|
_springIcons.Clear();
|
|
if (_springContainer == null || _springIconPrefab == null) return;
|
|
for (int i = 0; i < charges; i++)
|
|
_springIcons.Add(Instantiate(_springIconPrefab, _springContainer));
|
|
}
|
|
|
|
private void UpdateFormIcon(int formIndex)
|
|
{
|
|
if (_formIcons == null) return;
|
|
for (int i = 0; i < _formIcons.Length; i++)
|
|
if (_formIcons[i] != null) _formIcons[i].enabled = (i == formIndex);
|
|
}
|
|
|
|
private void ShowInteractPrompt(string text)
|
|
{
|
|
if (_interactText != null) _interactText.text = text;
|
|
if (_interactPromptRoot != null) _interactPromptRoot.SetActive(true);
|
|
}
|
|
|
|
private void HideInteractPrompt()
|
|
{
|
|
if (_interactPromptRoot != null) _interactPromptRoot.SetActive(false);
|
|
}
|
|
}
|
|
}
|