wangguan
2020-12-26 d66fdae8a885ff6ab23bedd423f6eade1779365c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using Core.Economy;
using TowerDefense.Level;
using TowerDefense.Towers;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// A button controller for spawning towers
    /// </summary>
    [RequireComponent(typeof(RectTransform))]
    public class TowerSpawnButton : MonoBehaviour, IDragHandler
    {
        /// <summary>
        /// The text attached to the button
        /// </summary>
        public Text buttonText;
 
        public Image towerIcon;
 
        public Button buyButton;
 
        public Image energyIcon;
 
        public Color energyDefaultColor;
 
        public Color energyInvalidColor;
 
        /// <summary>
        /// Fires when the button is tapped
        /// </summary>
        public event Action<Tower> buttonTapped;
 
        /// <summary>
        /// Fires when the pointer is outside of the button bounds
        /// and still down
        /// </summary>
        public event Action<Tower> draggedOff;
 
        /// <summary>
        /// The tower controller that defines the button
        /// </summary>
        Tower m_Tower;
 
        /// <summary>
        /// Cached reference to level currency
        /// </summary>
        Currency m_Currency;
 
        /// <summary>
        /// The attached rect transform
        /// </summary>
        RectTransform m_RectTransform;
 
        /// <summary>
        /// Checks if the pointer is out of bounds
        /// and then fires the draggedOff event
        /// </summary>
        public virtual void OnDrag(PointerEventData eventData)
        {
            if (!RectTransformUtility.RectangleContainsScreenPoint(m_RectTransform, eventData.position))
            {
                if (draggedOff != null)
                {
                    draggedOff(m_Tower);
                }
            }
        }
 
        /// <summary>
        /// Define the button information for the tower
        /// </summary>
        /// <param name="towerData">
        /// The tower to initialize the button with
        /// </param>
        public void InitializeButton(Tower towerData)
        {
            m_Tower = towerData;
 
            TowerLevel firstTower = towerData.CurrentTowerLevel;
 
            if (LevelManager.instanceExists)
            {
                m_Currency = LevelManager.instance.currency;
                m_Currency.currencyChanged += UpdateButton;
            }
            else
            {
                Debug.LogWarning("[Tower Spawn Button] No level manager to get currency object");
            }
            UpdateButton();
        }
 
        /// <summary>
        /// Cache the rect transform
        /// </summary>
        protected virtual void Awake()
        {
            m_RectTransform = (RectTransform)transform;
        }
 
        /// <summary>
        /// Unsubscribe from events
        /// </summary>
        protected virtual void OnDestroy()
        {
            if (m_Currency != null)
            {
                m_Currency.currencyChanged -= UpdateButton;
            }
        }
 
        /// <summary>
        /// The click for when the button is tapped
        /// </summary>
        public void OnClick()
        {
            if (buttonTapped != null)
            {
                buttonTapped(m_Tower);
            }
        }
 
        /// <summary>
        /// Update the button's button state based on cost
        /// </summary>
        void UpdateButton()
        {
            if (m_Currency == null)
            {
                return;
            }
 
            // // Enable button
            // if (m_Currency.CanAfford(m_Tower.purchaseCost) && !buyButton.interactable)
            // {
            //     buyButton.interactable = true;
            //     energyIcon.color = energyDefaultColor;
            // }
            // else if (!m_Currency.CanAfford(m_Tower.purchaseCost) && buyButton.interactable)
            // {
            //     buyButton.interactable = false;
            //     energyIcon.color = energyInvalidColor;
            // }
        }
    }
}