/* * @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 NodeCanvas.Tasks.Actions; using TH1_Core.Events; using TH1_Core.Managers; using TH1_Logic.Core; using TH1_UI.Controller.Info; 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; //如果PresentationManager正在播放,则不能交互 if (PresentationManager.Busy) { //Debug.Log($"BusyType is {PresentationManager.BusyType}"); return; } OnGridInfoAction(); 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); } OnBottomBarNextButton(); if (Input.GetKeyDown(KeyCode.BackQuote)) { #if UNITY_EDITOR || USE_INPUT Main.MapData.PlayerMap.SelfPlayerData.AddCoin(1000); #endif } if (Input.GetKeyDown(KeyCode.F9)) { #if UNITY_EDITOR || USE_INPUT Main.PlayerLogic.GetAllSight(); #endif } if (Input.GetKeyDown(KeyCode.F8)) { #if UNITY_EDITOR || USE_INPUT DebugCenter.Instance.DebugHideCenterMessage = !DebugCenter.Instance.DebugHideCenterMessage; #endif } OnTechTreeAction(); OnHeroAction(); /* 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(); } private void OnGridInfoAction() { // 检查 Main.MapData 是否为 null if (Main.MapData == null) { Debug.LogError("OnGridInfoAction Error: Main.MapData is null"); return; } // 检查 PlayerMap 和 SelfPlayerData 是否为 null if (Main.MapData.PlayerMap?.SelfPlayerData == null) { Debug.LogError("OnGridInfoAction Error: Main.MapData.PlayerMap?.SelfPlayerData is null"); return; } if (Main.MapData.CurPlayer == null || Main.MapData.CurPlayer != Main.MapData.PlayerMap.SelfPlayerData) return; // 检查 UIManager.Instance 和 UIInfoManager 是否为 null if (UIManager.Instance?.UIInfoManager == null) { Debug.LogError("OnGridInfoAction Error: UIManager.Instance?.UIInfoManager is null"); return; } // 检测数字键1-9的按下,执行对应的操作 if (UIManager.Instance.UIInfoManager.GetCurTaskType() != typeof(UIInfoGridInfoController)) return; for (int i = 1; i <= 9; i++) { if (Input.GetKeyDown(KeyCode.Alpha1 + i - 1)) { EventManager.Publish(new ExecuteUIInfoGridInfo(){idx = (uint)(i - 1)}); //UIManager.Instance.BottomInfoUI.ExecuteActionButtonByIndex(i - 1); break; } } } private void OnBottomBarNextButton() { // 检测E的按下,执行对应的操作 if (Main.MapData?.PlayerMap?.SelfPlayerData == null || Main.MapData.CurPlayer == null) return; if (Main.MapData.CurPlayer != Main.MapData.PlayerMap.SelfPlayerData) return; if (Input.GetKeyDown(KeyCode.E)) { EventManager.Publish(new ExecuteUIBottomBottomBarNextButtonClick()); //SimulateButtonClick(UIManager.Instance.BottomBarUI.NextTurnButton); } } private void OnTechTreeAction() { if (Main.MapData?.PlayerMap?.SelfPlayerData == null || Main.MapData.CurPlayer == null) return; if (Main.MapData.CurPlayer != Main.MapData.PlayerMap.SelfPlayerData) return; if (Input.GetKeyDown(KeyCode.T)) { if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { #if UNITY_EDITOR || USE_INPUT Main.MapData.PlayerMap.SelfPlayerData.AddCoin(1000); Table.Instance.PlayerDataAssets.GetPlayerInfo(Main.MapData.PlayerMap.SelfPlayerData, out var info); if (info?.TechPool != null) { foreach (TechType techType in info.TechPool) Main.PlayerLogic.ResearchTech(Main.MapData, Main.MapData.PlayerMap.SelfPlayerData,techType,0); } #endif } else { if (UIManager.Instance?.UIInfoManager == null) return; if(UIManager.Instance.UIInfoManager.GetCurTaskType() == typeof(UIInfoTechTreeController)) EventManager.Publish(new HideUIInfoTechTree(){}); else EventManager.Publish(new ShowUIInfoTechTree(){}); /* 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(); } */ } } } private void OnHeroAction() { if (Main.MapData?.PlayerMap?.SelfPlayerData == null || Main.MapData.CurPlayer == null) return; if (Main.MapData.CurPlayer != Main.MapData.PlayerMap.SelfPlayerData) return; if (Input.GetKeyDown(KeyCode.C)) { if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { } else { if (UIManager.Instance?.UIInfoManager == null) return; if(UIManager.Instance.UIInfoManager.GetCurTaskType() == typeof(UIInfoHeroController)) EventManager.Publish(new HideUIInfoHero(){}); else EventManager.Publish(new ShowUIInfoHero(){}); } } } } }