Starter
This commit is contained in:
@@ -4,56 +4,72 @@ using Arena.Combat;
|
||||
|
||||
namespace Arena.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Displays the ammo of a launcher as "current / max".
|
||||
/// </summary>
|
||||
public class AmmoView : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct References
|
||||
{
|
||||
public TMP_Text Label;
|
||||
}
|
||||
/// <summary>
|
||||
/// Displays the ammo of a launcher as "current / max".
|
||||
/// </summary>
|
||||
public class AmmoView : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
private struct References
|
||||
{
|
||||
public TMP_Text Label;
|
||||
}
|
||||
|
||||
#region Fields
|
||||
#region FIELDS
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private References _references;
|
||||
|
||||
private ProjectileLauncher _launcher;
|
||||
private ProjectileLauncher _launcher;
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
#region UNITY LIFECYCLES
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (_launcher != null)
|
||||
_launcher.OnAmmoChange -= HandleAmmoChange;
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
Unbind();
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
#region PUBLIC METHODS
|
||||
|
||||
/// <summary>
|
||||
/// Starts displaying the ammo of a launcher. Called by the UiManager.
|
||||
/// </summary>
|
||||
public void Bind(ProjectileLauncher launcher)
|
||||
{
|
||||
_launcher = launcher;
|
||||
_launcher.OnAmmoChange += HandleAmmoChange;
|
||||
HandleAmmoChange(_launcher.State.Ammo, _launcher.MaxAmmo);
|
||||
}
|
||||
/// <summary>
|
||||
/// Starts displaying the ammo of a launcher, instead of the previous one. Called by the UiManager.
|
||||
/// </summary>
|
||||
public void Bind(ProjectileLauncher launcher)
|
||||
{
|
||||
Unbind();
|
||||
|
||||
#endregion
|
||||
_launcher = launcher;
|
||||
|
||||
#region Private Methods
|
||||
if (!_launcher)
|
||||
{
|
||||
_references.Label.SetText("-");
|
||||
return;
|
||||
}
|
||||
|
||||
private void HandleAmmoChange(int current, int max)
|
||||
{
|
||||
_references.Label.SetText("{0} / {1}", current, max);
|
||||
}
|
||||
_launcher.OnAmmoChange += HandleAmmoChange;
|
||||
HandleAmmoChange(_launcher.State.Ammo, _launcher.MaxAmmo);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private void Unbind()
|
||||
{
|
||||
if (_launcher)
|
||||
_launcher.OnAmmoChange -= HandleAmmoChange;
|
||||
|
||||
_launcher = null;
|
||||
}
|
||||
|
||||
private void HandleAmmoChange(int current, int max)
|
||||
{
|
||||
_references.Label.SetText("{0} / {1}", current, max);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,31 +3,31 @@ using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Arena.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Game over screen. Its Restart button reloads the scene.
|
||||
/// </summary>
|
||||
public class GameOverView : MonoBehaviour
|
||||
{
|
||||
#region Public Methods
|
||||
/// <summary>
|
||||
/// Game over screen. Its Restart button reloads the scene.
|
||||
/// </summary>
|
||||
public class GameOverView : MonoBehaviour
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
public void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
public void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by the Restart button (OnClick in the Inspector).
|
||||
/// </summary>
|
||||
public void Restart()
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
/// <summary>
|
||||
/// Called by the Restart button (OnClick in the Inspector).
|
||||
/// </summary>
|
||||
public void Restart()
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,57 +4,66 @@ 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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Displays a Health as "current / max".
|
||||
/// </summary>
|
||||
public class HealthView : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
private struct References
|
||||
{
|
||||
public TMP_Text Label;
|
||||
}
|
||||
|
||||
#region Fields
|
||||
#region FIELDS
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private References _references;
|
||||
|
||||
private Health _health;
|
||||
private Health _health;
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
#region UNITY LIFECYCLES
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Every subscription has its unsubscription
|
||||
if (_health != null)
|
||||
_health.OnChange -= HandleHealthChange;
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
Unbind();
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
#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);
|
||||
}
|
||||
/// <summary>
|
||||
/// Starts displaying a Health, instead of the previous one. Called by the UiManager.
|
||||
/// </summary>
|
||||
public void Bind(Health health)
|
||||
{
|
||||
Unbind();
|
||||
|
||||
#endregion
|
||||
_health = health;
|
||||
_health.OnChange += HandleHealthChange;
|
||||
HandleHealthChange(_health.Current, _health.Max);
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
#endregion
|
||||
|
||||
private void HandleHealthChange(int current, int max)
|
||||
{
|
||||
_references.Label.SetText("{0} / {1}", current, max);
|
||||
}
|
||||
#region PRIVATE METHODS
|
||||
|
||||
#endregion
|
||||
}
|
||||
private void Unbind()
|
||||
{
|
||||
// Every subscription has its unsubscription
|
||||
if (_health != null)
|
||||
_health.OnChange -= HandleHealthChange;
|
||||
|
||||
_health = null;
|
||||
}
|
||||
|
||||
private void HandleHealthChange(int current, int max)
|
||||
{
|
||||
_references.Label.SetText("{0} / {1}", current, max);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
68
Assets/_Content/Scripts/UI/UiManager.cs
Normal file
68
Assets/_Content/Scripts/UI/UiManager.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using UnityEngine;
|
||||
using Arena.Characters;
|
||||
|
||||
namespace Arena.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Single entry point of the UI: connects the views to the player and switches screens.
|
||||
/// </summary>
|
||||
public class UiManager : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
private struct References
|
||||
{
|
||||
[Tooltip("In-game screen: health, ammo.")]
|
||||
public GameObject Hud;
|
||||
public HealthView HealthView;
|
||||
public AmmoView AmmoView;
|
||||
public GameOverView GameOverView;
|
||||
}
|
||||
|
||||
#region FIELDS
|
||||
|
||||
[SerializeField] private References _references;
|
||||
|
||||
private Player _player;
|
||||
|
||||
#endregion
|
||||
|
||||
#region UNITY LIFECYCLES
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Start: every Awake has run, so Player.Instance is set
|
||||
_player = Player.Instance;
|
||||
|
||||
if (!_player)
|
||||
{
|
||||
Debug.LogError("UiManager: no Player in the scene.", this);
|
||||
return;
|
||||
}
|
||||
|
||||
_references.HealthView.Bind(_player.Health);
|
||||
_references.AmmoView.Bind(_player.Launcher);
|
||||
_player.Health.OnDied += HandlePlayerDied;
|
||||
|
||||
_references.Hud.SetActive(true);
|
||||
_references.GameOverView.Hide();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (_player)
|
||||
_player.Health.OnDied -= HandlePlayerDied;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private void HandlePlayerDied()
|
||||
{
|
||||
_references.Hud.SetActive(false);
|
||||
_references.GameOverView.Show();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/UI/UiManager.cs.meta
Normal file
2
Assets/_Content/Scripts/UI/UiManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c86da6486be14fc40bd26d838f2e8762
|
||||
Reference in New Issue
Block a user