using System.Globalization;
using Core.Health;
using TowerDefense.Level;
using UnityEngine;
using UnityEngine.UI;
namespace TowerDefense.UI.HUD
{
///
/// A simple implementation of UI for player base health
///
public class PlayerBaseHealth : MonoBehaviour
{
///
/// The text element to display information on
///
public Text display;
///
/// The highest health that the base can go to
///
protected float m_MaxHealth;
///
/// Get the max health of the player base
///
protected virtual void Start()
{
LevelManager levelManager = LevelManager.instance;
if (levelManager == null)
{
return;
}
if (levelManager.numberOfHomeBases > 0)
{
Damageable baseConfig = levelManager.playerHomeBases[0].configuration;
baseConfig.damaged += OnBaseDamaged;
float currentHealth = baseConfig.currentHealth;
float noramlisedHealth = baseConfig.normalisedHealth;
m_MaxHealth = currentHealth / noramlisedHealth;
}
UpdateDisplay();
}
///
/// Subscribes to the player base health died event
///
///
/// The associated health change information
///
protected virtual void OnBaseDamaged(HealthChangeInfo info)
{
UpdateDisplay();
}
///
/// Get the current health of the home base and display it on m_Display
///
protected void UpdateDisplay()
{
LevelManager levelManager = LevelManager.instance;
if (levelManager == null)
{
return;
}
float currentHealth = levelManager.GetAllHomeBasesHealth();
display.text = currentHealth.ToString(CultureInfo.InvariantCulture);
}
}
}