294 lines
11 KiB
C#
294 lines
11 KiB
C#
/*
|
||
* @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坐标
|
||
|
||
bool inputLock = false;
|
||
|
||
public InputLogic(Main main, MapData mapData)
|
||
{
|
||
_main = main;
|
||
_mapData = mapData;
|
||
}
|
||
|
||
private bool ShouldIgnoreClick(GameObject clickedUI)
|
||
{
|
||
// 检查当前对象及其所有祖先对象
|
||
Transform currentTransform = clickedUI.transform;
|
||
while (currentTransform != null)
|
||
{
|
||
Debug.Log(currentTransform.name);
|
||
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 都使用了这个模块
|
||
private void BottomInfoUIHandleHover()
|
||
{
|
||
if (!_main.UIManager.BottomInfoUI.ROBottomInfoUI.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>();
|
||
if (actionClickedEvent != 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 (_main.UIManager.BottomInfoUI.ROBottomInfoUI.activeSelf)
|
||
{
|
||
for (int i = 1; i <= 9; i++)
|
||
{
|
||
if (Input.GetKeyDown(KeyCode.Alpha1 + i - 1))
|
||
{
|
||
_main.UIManager.BottomInfoUI.ExecuteActionButtonByIndex(i - 1);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (Input.GetMouseButtonUp(0)) // 检测鼠标点击
|
||
{
|
||
//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(_main.UIManager.BottomBarUI.NextTurnButton);
|
||
}
|
||
|
||
if (Input.GetKeyDown(KeyCode.BackQuote))
|
||
{
|
||
_main.MapData.PlayerMap.SelfPlayerData.PlayerWealth += 1000;
|
||
}
|
||
|
||
if (Input.GetKeyDown(KeyCode.T))
|
||
{
|
||
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
|
||
{
|
||
_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);
|
||
_main.UIManager.TechTreeUI.TechTreeUIDataRenderMark = true;
|
||
}
|
||
else
|
||
{
|
||
if (_main.UIManager.TechTreeUI.ROTechTreeUI.activeSelf)
|
||
{
|
||
_main.UIManager.TechTreeUI.SetTechTreeShowHide(false);
|
||
_main.UIManager.BottomBarUI.SetBottomBarShowHide(true);
|
||
}
|
||
else
|
||
{
|
||
_main.UIManager.TechTreeUI.SetTechTreeShowHide(true);
|
||
_main.UIManager.BottomBarUI.SetBottomBarShowHide(false);
|
||
_main.UIManager.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 (_main.UIManager.CenterMessageUI.ROCenterMessageUI.activeSelf)
|
||
{
|
||
_main.UIManager.CenterMessageUI.Hide();
|
||
}
|
||
|
||
}
|
||
|
||
BottomInfoUIHandleHover();
|
||
}
|
||
|
||
private void ShowHint(GameObject target)
|
||
{
|
||
if (!_main.UIManager.BottomInfoUI._hintWindow.activeSelf)
|
||
_main.UIManager.BottomInfoUI._hintWindow.SetActive(true);
|
||
|
||
RectTransform hintRt = _main.UIManager.BottomInfoUI._hintWindow.GetComponent<RectTransform>();
|
||
Vector3[] corners = new Vector3[4];
|
||
target.GetComponent<RectTransform>().GetWorldCorners(corners);
|
||
Vector3 topRight = corners[2]; // 取右上角
|
||
|
||
// 位置设定到按钮右边偏移一点
|
||
hintRt.position = topRight + new Vector3(20f, 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
|
||
{
|
||
_main.UIManager.BottomInfoUI._hintWindow.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
// 文本内容,这里可以更细分,比如根据按钮绑定的ActionId来取不同的文字
|
||
_main.UIManager.BottomInfoUI._hintWindow.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");
|
||
_main.UIManager.BottomInfoUI._hintWindow.transform.Find("Text").GetComponent<TextMeshProUGUI>().text
|
||
= skillDescFinal;
|
||
|
||
if(skillDescFinal == "")
|
||
{
|
||
_main.UIManager.BottomInfoUI._hintWindow.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
}
|
||
|
||
_main.UIManager.BottomInfoUI._hintWindow.GetComponent<CanvasGroup>().alpha = 1;
|
||
}
|
||
|
||
private void HideHint()
|
||
{
|
||
if (_main.UIManager.BottomInfoUI._hintWindow.activeSelf)
|
||
{
|
||
_main.UIManager.BottomInfoUI._hintWindow.GetComponent<CanvasGroup>().alpha = 0;
|
||
_main.UIManager.BottomInfoUI._hintWindow.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();
|
||
}
|
||
}
|
||
|
||
|
||
}
|