feat: Player
This commit is contained in:
60
Assets/_Content/Scripts/UI/HealthView.cs
Normal file
60
Assets/_Content/Scripts/UI/HealthView.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user