//using Boo.Lang; using TowerDefense.Level; using TowerDefense.Towers; using UnityEngine; using UnityEngine.UI; namespace TowerDefense.UI.HUD { /// /// Controls the UI objects that draw the tower data /// [RequireComponent(typeof(Canvas))] public class TowerUI : MonoBehaviour { /// /// The text object for the name /// public Text towerName; /// /// The text object for the description /// public Text description; public Text upgradeDescription; /// /// The attached sell button /// public Button sellButton; /// /// The attached upgrade button /// public Button upgradeButton; /// /// Component to display the relevant information of the tower /// public TowerInfoDisplay towerInfoDisplay; public RectTransform panelRectTransform; public GameObject[] confirmationButtons; /// /// The main game camera /// protected Camera m_GameCamera; /// /// The current tower to draw /// protected Tower m_Tower; /// /// The canvas attached to the gameObject /// protected Canvas m_Canvas; /// /// Draws the tower data on to the canvas,在界面上显示塔防信息,显示操作和信息界面。 /// 继续修改,确保其它方面的内容处理。 /// /// /// The tower to gain info from /// public virtual void Show(Tower towerToShow) { if (towerToShow == null) { return; } m_Tower = towerToShow; AdjustPosition(); int sellValue = m_Tower.GetSellLevel(); /// // TEST COED TO DELETE: if( sellButton != null) { // TEST CODE TO DELETE: if( m_Tower != null ) GameUI.instance.startDragTower(m_Tower); /** 以下代码用于测试合成: if((!m_Tower.isAtMaxLevel) && GameUI.instance.deleteSameLvlTower( m_Tower)) { GameUI.instance.UpgradeSelectedTower(); }*/ return; } else { if (GameUI.instance.towerInList(m_Tower) ) return; } m_Canvas.enabled = true; /// if (sellButton != null) { sellButton.gameObject.SetActive(sellValue > 0); } if (upgradeButton != null) { upgradeButton.interactable = LevelManager.instance.currency.CanAfford(m_Tower.GetCostForNextLevel()); bool maxLevel = m_Tower.isAtMaxLevel; upgradeButton.gameObject.SetActive(!maxLevel); if (!maxLevel) { upgradeDescription.text = m_Tower.levels[m_Tower.currentLevel + 1].upgradeDescription.ToUpper(); } } LevelManager.instance.currency.currencyChanged += OnCurrencyChanged; towerInfoDisplay.Show(towerToShow); foreach (var button in confirmationButtons) { button.SetActive(false); } } /// /// Hides the tower info UI and the radius visualizer /// public virtual void Hide() { m_Tower = null; if (GameUI.instanceExists) { GameUI.instance.HideRadiusVisualizer(); } m_Canvas.enabled = false; LevelManager.instance.currency.currencyChanged -= OnCurrencyChanged; } /// /// Upgrades the tower through /// public void UpgradeButtonClick() { GameUI.instance.UpgradeSelectedTower(); } /// /// Sells the tower through /// public void SellButtonClick() { GameUI.instance.SellSelectedTower(); } /// /// Get the text attached to the buttons /// protected virtual void Awake() { m_Canvas = GetComponent(); } /// /// Fires when tower is selected/deselected /// /// protected virtual void OnUISelectionChanged(Tower newTower) { if (newTower != null) { Show(newTower); } else { Hide(); } } /// /// Subscribe to mouse button action /// protected virtual void Start() { m_GameCamera = Camera.main; m_Canvas.enabled = false; if (GameUI.instanceExists) { GameUI.instance.selectionChanged += OnUISelectionChanged; GameUI.instance.stateChanged += OnGameUIStateChanged; } } /// /// Adjust position when the camera moves /// protected virtual void Update() { AdjustPosition(); } /// /// Unsubscribe from currencyChanged /// protected virtual void OnDisable() { if (LevelManager.instanceExists) { LevelManager.instance.currency.currencyChanged -= OnCurrencyChanged; } } /// /// Adjust the position of the UI /// protected void AdjustPosition() { if (m_Tower == null) { return; } Vector3 point = m_GameCamera.WorldToScreenPoint(m_Tower.position); point.z = 0; panelRectTransform.transform.position = point; } /// /// Fired when the state changes /// If the new state is we need to hide the /// /// The previous state /// The state to transition to protected void OnGameUIStateChanged(GameUI.State oldState, GameUI.State newState) { if (newState == GameUI.State.GameOver) { Hide(); } } /// /// Check if player can afford upgrade on currency changed /// void OnCurrencyChanged() { if (m_Tower != null && upgradeButton != null) { upgradeButton.interactable = LevelManager.instance.currency.CanAfford(m_Tower.GetCostForNextLevel()); } } /// /// Unsubscribe from GameUI selectionChanged and stateChanged /// void OnDestroy() { if (GameUI.instanceExists) { GameUI.instance.selectionChanged -= OnUISelectionChanged; GameUI.instance.stateChanged -= OnGameUIStateChanged; } } } }