/* * @Author: 白哉 * @Description: 输入逻辑 * @Date: 2025年04月01日 星期二 11:04:30 * @Modify: */ using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using RuntimeData; using TMPro; using Logic.Multilingual; using TH1_Core.Managers; using TH1_Logic.Core; using UI; using UnityEngine.UI; namespace Logic { public class InputLogic { Main _main; MapData _mapData; public int highlightCurrentState = 0; //0无1unit2land private Vector3 mouseLastDownPosition; private Vector2Int lastSelectedTile = new Vector2Int(-1, -1); // 上次点击的land坐标 private Vector2Int lastSelectedUnit = new Vector2Int(-1, -1); // 上次点击的unit坐标 //避免打包的时候吧TooltipTrigger代码剥离删掉的临时变量 private TooltipTrigger onozukakomachi; bool inputLock = false; public bool WASDMapMover = false; public Vector3 WASDMapMoverVector = Vector3.zero; public InputLogic(Main main, MapData mapData) { _main = main; _mapData = mapData; onozukakomachi = null; } private bool ShouldIgnoreClick(GameObject clickedUI) { // 检查当前对象及其所有祖先对象 Transform currentTransform = clickedUI.transform; while (currentTransform != null) { if (currentTransform.name == "UICanvas" || currentTransform.name == "HintMap" ) { return true; } currentTransform = currentTransform.parent; } return false; } public void LockInput() { inputLock = true; } public void UnlockInput() { inputLock = false; } public void Update() { //如果锁了input 但是DebugMode不会锁定input if (inputLock &&!DebugCenter.Instance.DebugMode) return; // 检测数字键1-9的按下,执行对应的操作 if (UIManager.Instance.BottomInfoUI.ROBottomInfoUI.activeSelf) { for (int i = 1; i <= 9; i++) { if (Input.GetKeyDown(KeyCode.Alpha1 + i - 1)) { UIManager.Instance.BottomInfoUI.ExecuteActionButtonByIndex(i - 1); break; } } } if (UIBlockCameraDrag.DownUpEvent) // 检测鼠标点击 { UIBlockCameraDrag.DownUpEvent = false; Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2Int cellPosition = Table.Instance.WorldToGrid(mousePosition); if (cellPosition.x >= Main.MapData.MapConfig.Width || cellPosition.x < 0 || cellPosition.y < 0 || cellPosition.y >= Main.MapData.MapConfig.Height) return; // 获取点击的地块类型 Main.MapData.GridMap.GetGridDataByPos(cellPosition.x, cellPosition.y, out var clickedTerrain); // 根据点击的地形类型执行某些操作(例如,显示消息) _main.MapInteractionLogic.OnTileClicked(Main.MapData,clickedTerrain); } if (Input.GetKeyDown(KeyCode.E)) { SimulateButtonClick(UIManager.Instance.BottomBarUI.NextTurnButton); } if (Input.GetKeyDown(KeyCode.BackQuote)) { #if UNITY_EDITOR Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth += 1000; #endif } if (Input.GetKeyDown(KeyCode.T)) { if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { #if UNITY_EDITOR Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth += 1000; foreach (TechType techType in System.Enum.GetValues(typeof(TechType))) Main.PlayerLogic.ResearchTech(Main.MapData, Main.MapData.PlayerMap.SelfPlayerData,techType,0); UIManager.Instance.TechTreeUI.TechTreeUIDataRenderMark = true; #endif } else { if (UIManager.Instance.TechTreeUI.ROTechTreeUI.activeSelf) { UIManager.Instance.TechTreeUI.SetTechTreeShowHide(false); UIManager.Instance.BottomBarUI.SetBottomBarShowHide(true); } else { UIManager.Instance.TechTreeUI.SetTechTreeShowHide(true); UIManager.Instance.BottomBarUI.SetBottomBarShowHide(false); UIManager.Instance.BottomInfoUI.SetBottomInfoHide(); } } } if (Input.GetKeyDown(KeyCode.Q)) { if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { foreach (WonderTypeEnum wonder in System.Enum.GetValues(typeof(WonderTypeEnum))) { _mapData.PlayerMap.SelfPlayerData.Wonder.SetWonderState(wonder,WonderState.FINISH_NOT_BUILD); } } } //这里处理WASD移动 float horizontalInput = Input.GetAxisRaw("Horizontal"); // verticalInput 会在 -1 (S键) 到 +1 (W键) 之间变化 float verticalInput = Input.GetAxisRaw("Vertical"); // 2. 构建移动方向向量 // 我们用获取到的输入值来构建一个方向 Vector3 direction = new Vector3(horizontalInput, verticalInput, 0); // **重要优化**: 如果不进行归一化,斜向移动(同时按W和D)会比直线移动更快 // 因为 Vector3(1, 1, 0).magnitude ≈ 1.414. 归一化后长度始终为1。 // 我们只在有输入的时候进行移动,避免不必要的计算 if (direction.magnitude > 0.1f) // magnitude > 0.1f 是为了处理浮点数精度和摇杆的微小偏移 { // 归一化向量,使其长度为1 direction.Normalize(); // 3. 计算本帧的移动量 // 位移 = 方向 * 速度 * 时间 // 乘以 Time.deltaTime 可以保证移动速度在任何帧率的电脑上都保持一致 WASDMapMoverVector = direction * 30 * Time.deltaTime; WASDMapMover = true; // 4. 应用移动 // 使用 transform.Translate 在当前位置的基础上进行移动 } /* if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) ||Input.GetKey(KeyCode.D) ) { WASDMapMover = true; WASDMapMoverVector = Vector3.zero; if (Input.GetKey(KeyCode.W)) WASDMapMoverVector += new Vector3(0, 20 / (1f / _deltaTime), 0); if (Input.GetKey(KeyCode.S)) WASDMapMoverVector += new Vector3(0, -20 / (1f / _deltaTime), 0); if (Input.GetKey(KeyCode.A)) WASDMapMoverVector += new Vector3(-20 / (1f / _deltaTime), 0 , 0); if (Input.GetKey(KeyCode.D)) WASDMapMoverVector += new Vector3(20 / (1f / _deltaTime), 0 , 0); };*/ if (Input.GetKeyDown(KeyCode.Escape)) { } //HintWindowUIHandleHover(); } public void SimulateButtonClick(Button button) { var pointer = new PointerEventData(EventSystem.current); var go = button.gameObject; ExecuteEvents.Execute(go, pointer, ExecuteEvents.pointerDownHandler); ExecuteEvents.Execute(go, pointer, ExecuteEvents.pointerUpHandler); button.onClick.Invoke(); } } }