chenxin
2020-11-12 278c5a9068bbe31be577d3d239f2a10867b46dd6
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
using TowerDefense.Level;
using TowerDefense.Towers;
using UnityEngine;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// UI component that displays towers that can be built on this level.
    /// </summary>
    public class BuildSidebar : MonoBehaviour
    {
        /// <summary>
        /// The prefab spawned for each button
        /// </summary>
        public TowerSpawnButton towerSpawnButton;
 
        /// <summary>
        /// Initialize the tower spawn buttons
        /// </summary>
        protected virtual void Start()
        {
            if (!LevelManager.instanceExists)
            {
                Debug.LogError("[UI] No level manager for tower list");
            }
            foreach (Tower tower in LevelManager.instance.towerLibrary)
            {
                TowerSpawnButton button = Instantiate(towerSpawnButton, transform);
                button.InitializeButton(tower);
                button.buttonTapped += OnButtonTapped;
                button.draggedOff += OnButtonDraggedOff;
            }
        }
 
        /// <summary>
        /// Sets the GameUI to build mode with the <see cref="towerData"/>
        /// </summary>
        /// <param name="towerData"></param>
        void OnButtonTapped(Tower towerData)
        {
            var gameUI = GameUI.instance;
            if (gameUI.isBuilding)
            {
                gameUI.CancelGhostPlacement();
            }
            //gameUI.SetToBuildMode(towerData);
            // River: 随机找一个空白位置放置塔防。
            gameUI.RandomPlaceTower(towerData);
        }
 
        /// <summary>
        /// Sets the GameUI to build mode with the <see cref="towerData"/> 
        /// </summary>
        /// <param name="towerData"></param>
        void OnButtonDraggedOff(Tower towerData)
        {
            if (!GameUI.instance.isBuilding)
            {
                GameUI.instance.SetToDragMode(towerData);
            }
        }
 
        /// <summary>
        /// Unsubscribes from all the tower spawn buttons
        /// </summary>
        void OnDestroy()
        {
            TowerSpawnButton[] childButtons = GetComponentsInChildren<TowerSpawnButton>();
 
            foreach (TowerSpawnButton towerButton in childButtons)
            {
                towerButton.buttonTapped -= OnButtonTapped;
                towerButton.draggedOff -= OnButtonDraggedOff;
            }
        }
 
        /// <summary>
        /// Called by start wave button in scene
        /// </summary>
        public void StartWaveButtonPressed()
        {
            if (LevelManager.instanceExists)
            {
                LevelManager.instance.BuildingCompleted();
            }
        }
 
        /// <summary>
        /// Debug button to add currency
        /// </summary>
        /// <param name="amount">How much to add</param>
        public void AddCurrency(int amount)
        {
            if (LevelManager.instanceExists)
            {
                LevelManager.instance.currency.AddCurrency(amount);
            }
        }
    }
}