chenxin
2020-12-01 944950c86ab3be21c22b7c15039cf2e572efdc90
Assets/Scripts/TowerDefense/Input/TowerDefenseKeyboardMouseInput.cs
@@ -8,213 +8,210 @@
namespace TowerDefense.Input
{
   [RequireComponent(typeof(GameUI))]
   public class TowerDefenseKeyboardMouseInput : KeyboardMouseInput
   {
      /// <summary>
      /// Cached eference to gameUI
      /// </summary>
      GameUI m_GameUI;
    [RequireComponent(typeof(GameUI))]
    public class TowerDefenseKeyboardMouseInput : KeyboardMouseInput
    {
        /// <summary>
        /// Cached eference to gameUI
        /// </summary>
        GameUI m_GameUI;
      /// <summary>
      /// 是否处于拖动状态。
      /// </summary>
      protected bool isInDragState = false;
        /// <summary>
        /// 是否处于拖动状态。
        /// </summary>
        protected bool isInDragState = false;
      /// <summary>
      /// Register input events
      /// </summary>
      protected override void OnEnable()
      {
         base.OnEnable();
         m_GameUI = GetComponent<GameUI>();
        /// <summary>
        /// Register input events
        /// </summary>
        protected override void OnEnable()
        {
            base.OnEnable();
         if (InputController.instanceExists)
         {
            InputController controller = InputController.instance;
            m_GameUI = GetComponent<GameUI>();
            controller.tapped += OnTap;
            controller.mouseMoved += OnMouseMoved;
            if (InputController.instanceExists)
            {
                InputController controller = InputController.instance;
            // River Add for Mouse Start Drag & Draged.
            controller.startedDrag += OnStartDrag;
            controller.dragged += this.OnDrag;
            controller.released += this.OnEndDrag;
         }
      }
                controller.tapped += OnTap;
                controller.mouseMoved += OnMouseMoved;
                // River Add for Mouse Start Drag & Draged.
                controller.startedDrag += OnStartDrag;
                controller.dragged += this.OnDrag;
                controller.released += this.OnEndDrag;
            }
        }
      /// <summary>
      /// Deregister input events
      /// </summary>
      protected override void OnDisable()
      {
         if (!InputController.instanceExists)
         {
            return;
         }
        /// <summary>
        /// Deregister input events
        /// </summary>
        protected override void OnDisable()
        {
            if (!InputController.instanceExists)
            {
                return;
            }
         InputController controller = InputController.instance;
            InputController controller = InputController.instance;
         controller.tapped -= OnTap;
         controller.mouseMoved -= OnMouseMoved;
            controller.tapped -= OnTap;
            controller.mouseMoved -= OnMouseMoved;
         controller.startedDrag -= this.OnStartDrag;
         controller.dragged -= this.OnDrag;
         controller.released -= this.OnEndDrag;
      }
            controller.startedDrag -= this.OnStartDrag;
            controller.dragged -= this.OnDrag;
            controller.released -= this.OnEndDrag;
        }
      /// <summary>
      /// Handle camera panning behaviour
      /// </summary>
      protected override void Update()
      {
         base.Update();
         // Escape handling
         if (UnityInput.GetKeyDown(KeyCode.Escape))
         {
            switch (m_GameUI.state)
            {
               case State.Normal:
                  if (m_GameUI.isTowerSelected)
                  {
                     m_GameUI.DeselectTower();
                  }
                  else
                  {
                     m_GameUI.Pause();
                  }
                  break;
               case State.BuildingWithDrag:
               case State.Building:
                  m_GameUI.CancelGhostPlacement();
                  break;
            }
         }
         // place towers with keyboard numbers
         if (LevelManager.instanceExists)
         {
            int towerLibraryCount = LevelManager.instance.towerLibrary.Count;
        /// <summary>
        /// Handle camera panning behaviour
        /// </summary>
        protected override void Update()
        {
            base.Update();
            // find the lowest value between 9 (keyboard numbers)
            // and the amount of towers in the library
            int count = Mathf.Min(9, towerLibraryCount);
            KeyCode highestKey = KeyCode.Alpha1 + count;
            for (var key = KeyCode.Alpha1; key < highestKey; key++)
            {
               // add offset for the KeyCode Alpha 1 index to find correct keycodes
               if (UnityInput.GetKeyDown(key))
               {
                  Tower controller = LevelManager.instance.towerLibrary[key - KeyCode.Alpha1];
                  if (LevelManager.instance.currency.CanAfford(controller.purchaseCost))
                  {
                     if (m_GameUI.isBuilding)
                     {
                        m_GameUI.CancelGhostPlacement();
                     }
                     GameUI.instance.SetToBuildMode(controller);
                     GameUI.instance.TryMoveGhost(InputController.instance.basicMouseInfo);
                  }
                  break;
               }
            }
            // special case for 0 mapping to index 9
            if (count < 10 && UnityInput.GetKeyDown(KeyCode.Alpha0))
            {
               Tower controller = LevelManager.instance.towerLibrary[9];
               GameUI.instance.SetToBuildMode(controller);
               GameUI.instance.TryMoveGhost(InputController.instance.basicMouseInfo);
            }
         }
      }
      /// <summary>
      /// Ghost follows pointer
      /// </summary>
      void OnMouseMoved(PointerInfo pointer)
      {
         // We only respond to mouse info
         var mouseInfo = pointer as MouseCursorInfo;
         if ((mouseInfo != null) && (m_GameUI.isBuilding))
         {
            m_GameUI.TryMoveGhost(pointer, false);
         }
      }
      protected virtual void OnStartDrag(PointerActionInfo pointer)
      {
         // select towers
         m_GameUI.TrySelectTower(pointer);
         // River: 为了开始拖动后可以直接显示GhostTower
         m_GameUI.TryMoveGhost(pointer, false);
         this.isInDragState = true;
      }
      protected override void OnDrag(PointerActionInfo pointer)
      {
         if ((pointer != null) && (m_GameUI.isBuilding))
         {
            m_GameUI.TryMoveGhost(pointer, false);
         }
      }
      protected void OnEndDrag(PointerActionInfo pointer)
      {
         // EndDrag 只能放置相关的GhostTower.
         if (this.isInDragState)
            m_GameUI.onEndTowerDrag(pointer);
            //m_GameUI.TryPlaceTower(pointer);
         this.isInDragState = false;
      }
      /// <summary>
      /// Select towers or position ghosts
      /// </summary>
      void OnTap(PointerActionInfo pointer)
      {
         // We only respond to mouse info
         var mouseInfo = pointer as MouseButtonInfo;
         if (mouseInfo != null && !mouseInfo.startedOverUI)
         {
            if (m_GameUI.isBuilding)
            {
               if (mouseInfo.mouseButtonId == 0) // LMB confirms
               {
                  if( m_GameUI.TryPlaceTower(pointer))
            // Escape handling
            if (UnityInput.GetKeyDown(KeyCode.Escape))
            {
                switch (m_GameUI.state)
                {
                    case State.Normal:
                        if (m_GameUI.isTowerSelected)
                        {
                     // 开启相应的兵线:
                     Tower tw = m_GameUI.FindTowerWithGridIdx(m_GameUI.currentGrid.x, m_GameUI.currentGrid.y);
                     if ((tw != null) && (tw.bInAttackMode ))
                        LevelManager.instance.startWaveLine(m_GameUI.currentGrid.x, false, tw.attributeId);
                  }
               }
               else // RMB cancels
               {
                  m_GameUI.CancelGhostPlacement();
               }
            }
            else
            {
               if (mouseInfo.mouseButtonId == 0)
               {
                  // select towers
                  m_GameUI.TrySelectTower(pointer);
                  // River: 为了TAP后立刻显示出来相关的Ghost.
                  m_GameUI.TryMoveGhost(pointer, false);
                            m_GameUI.DeselectTower();
                        }
                        else
                        {
                            m_GameUI.Pause();
                        }
                        break;
                    case State.BuildingWithDrag:
                    case State.Building:
                        m_GameUI.CancelGhostPlacement();
                        break;
                }
            }
               }
            }
         }
      }
   }
            // place towers with keyboard numbers
            if (LevelManager.instanceExists)
            {
                int towerLibraryCount = LevelManager.instance.towerLibrary.Count;
                // find the lowest value between 9 (keyboard numbers)
                // and the amount of towers in the library
                int count = Mathf.Min(9, towerLibraryCount);
                KeyCode highestKey = KeyCode.Alpha1 + count;
                for (var key = KeyCode.Alpha1; key < highestKey; key++)
                {
                    // add offset for the KeyCode Alpha 1 index to find correct keycodes
                    if (UnityInput.GetKeyDown(key))
                    {
                        Tower controller = LevelManager.instance.towerLibrary[key - KeyCode.Alpha1];
                        if (m_GameUI.isBuilding)
                        {
                            m_GameUI.CancelGhostPlacement();
                        }
                        GameUI.instance.SetToBuildMode(controller);
                        GameUI.instance.TryMoveGhost(InputController.instance.basicMouseInfo);
                        break;
                    }
                }
                // special case for 0 mapping to index 9
                if (count < 10 && UnityInput.GetKeyDown(KeyCode.Alpha0))
                {
                    Tower controller = LevelManager.instance.towerLibrary[9];
                    GameUI.instance.SetToBuildMode(controller);
                    GameUI.instance.TryMoveGhost(InputController.instance.basicMouseInfo);
                }
            }
        }
        /// <summary>
        /// Ghost follows pointer
        /// </summary>
        void OnMouseMoved(PointerInfo pointer)
        {
            // We only respond to mouse info
            var mouseInfo = pointer as MouseCursorInfo;
            if ((mouseInfo != null) && (m_GameUI.isBuilding))
            {
                m_GameUI.TryMoveGhost(pointer, false);
            }
        }
        protected virtual void OnStartDrag(PointerActionInfo pointer)
        {
            // select towers
            m_GameUI.TrySelectTower(pointer);
            // River: 为了开始拖动后可以直接显示GhostTower
            m_GameUI.TryMoveGhost(pointer, false);
            this.isInDragState = true;
        }
        protected override void OnDrag(PointerActionInfo pointer)
        {
            if ((pointer != null) && (m_GameUI.isBuilding))
            {
                m_GameUI.TryMoveGhost(pointer, false);
            }
        }
        protected void OnEndDrag(PointerActionInfo pointer)
        {
            // EndDrag 只能放置相关的GhostTower.
            if (this.isInDragState)
                m_GameUI.onEndTowerDrag(pointer);
            //m_GameUI.TryPlaceTower(pointer);
            this.isInDragState = false;
        }
        /// <summary>
        /// Select towers or position ghosts
        /// </summary>
        void OnTap(PointerActionInfo pointer)
        {
            // We only respond to mouse info
            var mouseInfo = pointer as MouseButtonInfo;
            if (mouseInfo != null && !mouseInfo.startedOverUI)
            {
                if (m_GameUI.isBuilding)
                {
                    if (mouseInfo.mouseButtonId == 0) // LMB confirms
                    {
                        if (m_GameUI.TryPlaceTower(pointer))
                        {
                            // 开启相应的兵线:
                            Tower tw = m_GameUI.FindTowerWithGridIdx(m_GameUI.currentGrid.x, m_GameUI.currentGrid.y);
                            if ((tw != null) && (tw.bInAttackMode))
                                LevelManager.instance.startWaveLine(m_GameUI.currentGrid.x, false, tw.attributeId);
                        }
                    }
                    else // RMB cancels
                    {
                        m_GameUI.CancelGhostPlacement();
                    }
                }
                else
                {
                    if (mouseInfo.mouseButtonId == 0)
                    {
                        // select towers
                        m_GameUI.TrySelectTower(pointer);
                        // River: 为了TAP后立刻显示出来相关的Ghost.
                        m_GameUI.TryMoveGhost(pointer, false);
                    }
                }
            }
        }
    }
}