wangguan
2020-11-06 fc40dea934140005aeb62ac1e4ec1e613cc44a0c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using TowerDefense.Towers;
using UnityEngine;
using UnityEngine.UI;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// Used to display infomation about a tower using Unity UI
    /// </summary>
    public class TowerInfoDisplay : MonoBehaviour
    {
        /// <summary>
        /// The text component for the name
        /// </summary>
        public Text towerName;
 
        /// <summary>
        /// The text component for the description
        /// </summary>
        public Text description;
 
        /// <summary>
        /// The text component for the description
        /// </summary>
        public Text dps;
 
        /// <summary>
        /// The text component for the level
        /// </summary>
        public Text level;
 
        /// <summary>
        /// The text component for the health
        /// </summary>
        public Text health;
 
        /// <summary>
        /// The text component for the dimensions
        /// </summary>
        public Text dimensions;
 
        /// <summary>
        /// The text component for the dimensions
        /// </summary>
        public Text upgradeCost;
 
        /// <summary>
        /// The text component for the dimensions
        /// </summary>
        public Text sellPrice;
 
        /// <summary>
        /// Draws the tower data on to the canvas, if the relevant text components are populated
        /// </summary>
        /// <param name="tower">
        /// The tower to gain info from
        /// </param>
        public void Show(Tower tower)
        {
            int levelOfTower = tower.currentLevel;
            Show(tower, levelOfTower);
        }
 
        /// <summary>
        /// Draws the tower data on to the canvas, if the relevant text components are populated
        /// </summary>
        /// <param name="tower">The tower to gain info from</param>
        /// <param name="levelOfTower">The level of the tower</param>
        public void Show(Tower tower, int levelOfTower)
        {
            if (levelOfTower >= tower.levels.Length)
            {
                return;
            }
            TowerLevel towerLevel = tower.levels[levelOfTower];
            DisplayText(towerName, tower.towerName);
            DisplayText(description, towerLevel.description);
            DisplayText(dps, towerLevel.GetTowerDps().ToString("f2"));
            DisplayText(health, string.Format("{0}/{1}", tower.configuration.currentHealth, towerLevel.maxHealth));
            DisplayText(level, (levelOfTower + 1).ToString());
            DisplayText(dimensions, string.Format("{0}, {1}", tower.dimensions.x, tower.dimensions.y));
            if (levelOfTower + 1 < tower.levels.Length)
            {
                DisplayText(upgradeCost, tower.levels[levelOfTower + 1].cost.ToString());
            }
 
            int sellValue = tower.GetSellLevel(levelOfTower);
            DisplayText(sellPrice, sellValue.ToString());
        }
 
        /// <summary>
        /// Draws the text if the text component is populated
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="text"></param>
        static void DisplayText(Text textBox, string text)
        {
            if (textBox != null)
            {
                textBox.text = text;
            }
        }
    }
}