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