using Core.Input;
using TowerDefense.Level;
using TowerDefense.Towers;
using TowerDefense.UI.HUD;
using UnityEngine;
using UnityInput = UnityEngine.Input;
using State = TowerDefense.UI.HUD.GameUI.State;
namespace TowerDefense.Input
{
[RequireComponent(typeof(GameUI))]
public class TowerDefenseKeyboardMouseInput : KeyboardMouseInput
{
///
/// Cached eference to gameUI
///
GameUI m_GameUI;
///
/// 是否处于拖动状态。
///
protected bool isInDragState = false;
///
/// Register input events
///
protected override void OnEnable()
{
base.OnEnable();
m_GameUI = GetComponent();
if (InputController.instanceExists)
{
InputController controller = InputController.instance;
controller.tapped += OnTap;
controller.mouseMoved += OnMouseMoved;
// River Add for Mouse Start Drag & Draged.
controller.startedDrag += OnStartDrag;
controller.dragged += this.OnDrag;
controller.released += this.OnEndDrag;
}
}
///
/// Deregister input events
///
protected override void OnDisable()
{
if (!InputController.instanceExists)
{
return;
}
InputController controller = InputController.instance;
controller.tapped -= OnTap;
controller.mouseMoved -= OnMouseMoved;
controller.startedDrag -= this.OnStartDrag;
controller.dragged -= this.OnDrag;
controller.released -= this.OnEndDrag;
}
///
/// Handle camera panning behaviour
///
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;
// 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);
}
}
}
///
/// Ghost follows pointer
///
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;
}
///
/// Select towers or position ghosts
///
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.ElfId);
}
}
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);
}
}
}
}
}
}