wangguan
2020-12-23 1e192494412a34d17548834a6aff639202cdfdda
Assets/Scripts/TowerDefense/UI/HUD/TowerSpawnButton.cs
@@ -8,152 +8,143 @@
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;
    /// <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 Image towerIcon;
      public Button buyButton;
        public Button buyButton;
      public Image energyIcon;
        public Image energyIcon;
      public Color energyDefaultColor;
      public Color energyInvalidColor;
        public Color energyDefaultColor;
      /// <summary>
      /// Fires when the button is tapped
      /// </summary>
      public event Action<Tower> buttonTapped;
        public Color energyInvalidColor;
      /// <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>
        /// Fires when the button is tapped
        /// </summary>
        public event Action<Tower> buttonTapped;
      /// <summary>
      /// Cached reference to level currency
      /// </summary>
      Currency m_Currency;
        /// <summary>
        /// Fires when the pointer is outside of the button bounds
        /// and still down
        /// </summary>
        public event Action<Tower> draggedOff;
      /// <summary>
      /// The attached rect transform
      /// </summary>
      RectTransform m_RectTransform;
        /// <summary>
        /// The tower controller that defines the button
        /// </summary>
        Tower m_Tower;
      /// <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>
        /// Cached reference to level currency
        /// </summary>
        Currency m_Currency;
      /// <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;
        /// <summary>
        /// The attached rect transform
        /// </summary>
        RectTransform m_RectTransform;
         if (towerData.levels.Length > 0)
         {
            TowerLevel firstTower = towerData.levels[0];
            buttonText.text = firstTower.cost.ToString();
            towerIcon.sprite = firstTower.levelData.icon;
         }
         else
         {
            Debug.LogWarning("[Tower Spawn Button] No level data for tower");
         }
        /// <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);
                }
            }
        }
         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>
        /// 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;
      /// <summary>
      /// Cache the rect transform
      /// </summary>
      protected virtual void Awake()
      {
         m_RectTransform = (RectTransform) transform;
      }
            TowerLevel firstTower = towerData.CurrentTowerLevel;
      /// <summary>
      /// Unsubscribe from events
      /// </summary>
      protected virtual void OnDestroy()
      {
         if (m_Currency != null)
         {
            m_Currency.currencyChanged -= UpdateButton;
         }
      }
            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>
      /// The click for when the button is tapped
      /// </summary>
      public void OnClick()
      {
         if (buttonTapped != null)
         {
            buttonTapped(m_Tower);
         }
      }
        /// <summary>
        /// Cache the rect transform
        /// </summary>
        protected virtual void Awake()
        {
            m_RectTransform = (RectTransform)transform;
        }
      /// <summary>
      /// Update the button's button state based on cost
      /// </summary>
      void UpdateButton()
      {
         if (m_Currency == null)
         {
            return;
         }
        /// <summary>
        /// Unsubscribe from events
        /// </summary>
        protected virtual void OnDestroy()
        {
            if (m_Currency != null)
            {
                m_Currency.currencyChanged -= UpdateButton;
            }
        }
         // 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;
         }
      }
   }
        /// <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;
            // }
        }
    }
}