61 lines
1.0 KiB
C#
61 lines
1.0 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using Arena.Combat;
|
|
|
|
namespace Arena.UI
|
|
{
|
|
/// <summary>
|
|
/// Displays a Health as "current / max".
|
|
/// </summary>
|
|
public class HealthView : MonoBehaviour
|
|
{
|
|
[System.Serializable]
|
|
public struct References
|
|
{
|
|
public TMP_Text Label;
|
|
}
|
|
|
|
#region Fields
|
|
|
|
[SerializeField] private References _references;
|
|
|
|
private Health _health;
|
|
|
|
#endregion
|
|
|
|
#region Unity Lifecycle
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// Every subscription has its unsubscription
|
|
if (_health != null)
|
|
_health.OnChange -= HandleHealthChange;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Starts displaying a Health. Called by the UiManager.
|
|
/// </summary>
|
|
public void Bind(Health health)
|
|
{
|
|
_health = health;
|
|
_health.OnChange += HandleHealthChange;
|
|
HandleHealthChange(_health.Current, _health.Max);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Methods
|
|
|
|
private void HandleHealthChange(int current, int max)
|
|
{
|
|
_references.Label.SetText("{0} / {1}", current, max);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|