wangguan
2020-12-09 0690876fea4792f47b18a8e9d3133b8be0cfba7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Globalization;
using Core.Health;
using TowerDefense.Level;
using UnityEngine;
using UnityEngine.UI;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// A simple implementation of UI for player base health
    /// </summary>
    public class PlayerBaseHealth : MonoBehaviour
    {
        /// <summary>
        /// The text element to display information on
        /// </summary>
        public Text display;
 
        /// <summary>
        /// The highest health that the base can go to
        /// </summary>
        protected float m_MaxHealth;
 
        /// <summary>
        /// Get the max health of the player base
        /// </summary>
        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();
        }
 
        /// <summary>
        /// Subscribes to the player base health died event
        /// </summary>
        /// <param name="info">
        /// The associated health change information
        /// </param>
        protected virtual void OnBaseDamaged(HealthChangeInfo info)
        {
            UpdateDisplay();
        }
 
        /// <summary>
        /// Get the current health of the home base and display it on m_Display
        /// </summary>
        protected void UpdateDisplay()
        {
            LevelManager levelManager = LevelManager.instance;
            if (levelManager == null)
            {
                return;
            }
            float currentHealth = levelManager.GetAllHomeBasesHealth();
            display.text = currentHealth.ToString(CultureInfo.InvariantCulture);
        }
    }
}