chenxin
2020-12-08 28eccd0c0b741b38b2d904fe95b8ed54c9dcf44e
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Core.Game;
using TowerDefense.Game;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
namespace TowerDefense.UI
{
    /// <summary>
    /// The button for selecting a level
    /// </summary>
    [RequireComponent(typeof(Button))]
    public class LevelSelectButton : MonoBehaviour, ISelectHandler
    {
        /// <summary>
        /// Reference to the required button component
        /// </summary>
        protected Button m_Button;
 
        /// <summary>
        /// The UI text element that displays the name of the level
        /// </summary>
        public Text titleDisplay;
        
        public Text description;
 
        public Sprite starAchieved;
 
        public Image[] stars;
 
        protected MouseScroll m_MouseScroll;
 
        /// <summary>
        /// The data concerning the level this button displays
        /// </summary>
        protected LevelItem m_Item;
 
        /// <summary>
        /// When the user clicks the button, change the scene
        /// </summary>
        public void ButtonClicked()
        {
            ChangeScenes();
        }
 
        /// <summary>
        /// A method for assigning the data from item to the button
        /// </summary>
        /// <param name="item">
        /// The data with the information concerning the level
        /// </param>
        public void Initialize(LevelItem item, MouseScroll mouseScroll)
        {
            LazyLoad();
            if (titleDisplay == null)
            {
                return;
            }
            m_Item = item;
            titleDisplay.text = item.name;
            description.text = item.description;
            HasPlayedState();
            m_MouseScroll = mouseScroll;
        }
 
        /// <summary>
        /// Configures the feedback concerning if the player has played
        /// </summary>
        protected void HasPlayedState()
        {
            GameManager gameManager = GameManager.instance;
            if (gameManager == null)
            {
                return;
            }
            int starsForLevel = gameManager.GetStarsForLevel(m_Item.id);
            for (int i = 0; i < starsForLevel; i++)
            {
                stars[i].sprite = starAchieved;
            }
        }
 
        /// <summary>
        /// Changes the scene to the scene name provided by m_Item
        /// </summary>
        protected void ChangeScenes()
        {
            SceneManager.LoadScene(m_Item.sceneName);
        }
 
        /// <summary>
        /// Ensure <see cref="m_Button"/> is not null
        /// </summary>
        protected void LazyLoad()
        {
            if (m_Button == null)
            {
                m_Button = GetComponent<Button>();
            }
        }
 
        /// <summary>
        /// Remove all listeners on the button before destruction
        /// </summary>
        protected void OnDestroy()
        {
            if (m_Button != null)
            {
                m_Button.onClick.RemoveAllListeners();
            }
        }
 
        /// <summary>
        /// Implementation of ISelectHandler
        /// </summary>
        /// <param name="eventData">Select event data</param>
        public void OnSelect(BaseEventData eventData)
        {
            m_MouseScroll.SelectChild(this);
        }
    }
}