TH1/Unity/Assets/Scripts/Logic/Input/InputLogic.cs
2025-07-26 18:28:52 +08:00

306 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @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 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 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;
}
//注意 bottominfo中skillinfo和actioninfo 都使用了这个模块,techtree的checktechpanel也用了这个模块
//只要组件有对应的mono数据就可以显示提示
private void HintWindowUIHandleHover()
{
//如果bottominfo没有展开且techtree没有展开 return
if (!UIManager.Instance.BottomInfoUI.ROBottomInfoUI.activeSelf
&& (!UIManager.Instance.TechTreeUI.ROTechCheckPanel.activeSelf || !UIManager.Instance.TechTreeUI.ROTechTreeUI.activeSelf))
return;
PointerEventData pointerData = new PointerEventData(EventSystem.current)
{
position = Input.mousePosition
};
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);
foreach (var result in results)
{
var actionClickedEvent = result.gameObject.GetComponent<ActionClickedEvent>();
var tooltip = result.gameObject.GetComponent<TooltipTrigger>();
if (actionClickedEvent != null || tooltip != null)
{
ShowHint(result.gameObject);
return;
}
}
HideHint();
}
public void Update()
{
//如果锁了input 但是DebugMode不会锁定input
if (inputLock &&!DebugCenter.Instance.DebugMode) return;
if (Input.GetMouseButtonDown(0))
{
mouseLastDownPosition = Input.mousePosition;
}
// 检测数字键1-5的按下执行对应的操作
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 (Input.GetMouseButtonUp(0)) // 检测鼠标点击
{
//给相机拖动系统打的补丁
UIBlockCameraDrag.ShouldBlockDrag = false;
//Debug.Log("Alive");
if (EventSystem.current.IsPointerOverGameObject())
{
//如果同时点到了其他UI要素那么判断是不是UICanvans的点击如果点到了UICancas那么return这次tile点击不生效
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
if (results.Count > 0)
{
// 获取第一个被点击的UI元素
GameObject clickedUI = results[0].gameObject;
// 如果是UICanvas的点击那么return
if (ShouldIgnoreClick(clickedUI))
return;
}
}
if (Input.mousePosition != mouseLastDownPosition)
return;
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.S))
{
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.W))
{
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);
}
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
if (UIManager.Instance.CenterMessageUI.ROCenterMessageUI.activeSelf)
{
UIManager.Instance.CenterMessageUI.Hide();
}
}
HintWindowUIHandleHover();
}
private void ShowHint(GameObject target)
{
if (!UIManager.Instance.ROHintWindow.activeSelf)
UIManager.Instance.ROHintWindow.SetActive(true);
RectTransform hintRt = UIManager.Instance.ROHintWindow.GetComponent<RectTransform>();
Vector3[] corners = new Vector3[4];
target.GetComponent<RectTransform>().GetWorldCorners(corners);
Vector3 topRight = corners[2]; // 取右上角
// 位置设定到按钮右边偏移一点
hintRt.position = topRight;// + new Vector3(-200f, 0f, 0f);
//处理action的情况
if (target.GetComponent<ActionIdMono>() != null)
{
var actionId = target.GetComponent<ActionIdMono>()?.ActionId;
if (!Table.Instance.ActionDataAssets.GetActionInfo(actionId, out var actionInfo))
return;
string actionNameFinal =
MultilingualManager.Instance.GetMultilingualText(uint.Parse(actionInfo.ActionName));
string actionDescFinal = "";
if (actionInfo.Desc != "")
actionDescFinal = MultilingualManager.Instance.GetMultilingualText(uint.Parse(actionInfo.Desc))
.Replace("\\n", "\n");
else
{
UIManager.Instance.ROHintWindow.SetActive(false);
return;
}
// 文本内容这里可以更细分比如根据按钮绑定的ActionId来取不同的文字
UIManager.Instance.ROHintWindow.transform.Find("Text").GetComponent<TextMeshProUGUI>().text
= actionNameFinal + " : " + actionDescFinal;
}
//处理skill的情况
else if(target.GetComponent<SkillIdMono>() != null)
{
var skillType = target.GetComponent<SkillIdMono>().SkillTpe;
if (!Table.Instance.SkillDataAssets.GetSkillInfo(skillType, out var skillInfo))
return;
string skillDescFinal = MultilingualManager.Instance.GetMultilingualText(uint.Parse(skillInfo.SkillDesc))
.Replace("\\n", "\n");
UIManager.Instance.ROHintWindow.transform.Find("Text").GetComponent<TextMeshProUGUI>().text
= skillDescFinal;
if(skillDescFinal == "")
{
UIManager.Instance.ROHintWindow.SetActive(false);
return;
}
}
UIManager.Instance.ROHintWindow.GetComponent<CanvasGroup>().alpha = 1;
}
private void HideHint()
{
if (UIManager.Instance.ROHintWindow.activeSelf)
{
UIManager.Instance.ROHintWindow.GetComponent<CanvasGroup>().alpha = 0;
UIManager.Instance.ROHintWindow.SetActive(false);
}
}
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();
}
}
}