2025-07-04 03:19:24 +08:00

199 lines
7.1 KiB
C#

using Logic;
using RuntimeData;
using UnityEngine;
using Animancer;
using Logic.Multilingual;
using TMPro;
namespace TH1Renderer
{
/// <summary>
/// 负责一个 HintPrefab 的生命周期控制(展示提示气泡、点击展开文字、关闭提示)
/// </summary>
public class HintRenderer
{
private MapData _map;
private Main _main;
private HintData _hintData;
private uint _gridId;
private GridData _gridData;
public GameObject ROHint; // 实例化的 Hint GameObject
public bool Finished; // 是否完成显示并关闭
private GameObject _bubble; // 点击用的气泡按钮
private GameObject _title;
private GameObject _text; // 显示文字的文本对象
private GameObject _closeButton; // 关闭按钮
private GameObject _textPanel; // 包含文字和关闭按钮的面板
/// <summary>
/// 构造函数:初始化渲染器、创建实体、绑定数据
/// </summary>
public HintRenderer(Main main, MapData mapData, GameObject prefab, Transform father, HintData hintData)
{
Finished = false;
_map = mapData;
_main = main;
_gridId = hintData.GridId;
_hintData = hintData;
mapData.GridMap.GetGridDataByGid(_gridId, out _gridData);
Vector3 tpos = Table.Instance.GridToWorld(_gridData);
//ROHint = GameObject.Instantiate(prefab, tpos, Quaternion.identity, father);
ROHint = GameObject.Instantiate(prefab, father);
ROHint.transform.position = tpos;
ROHint.transform.localRotation = Quaternion.identity;
ROHint.transform.localScale = Vector3.one;
Init();
SetInfo();
}
public void SetInfo()
{
if (!Table.Instance.HintDataAssets.GetHintInfo(_hintData.HintType, out var hintInfo)) return;
MultilingualManager.Instance.SetUIText(_title.GetComponent<TextMeshProUGUI>(),hintInfo.HintName);
MultilingualManager.Instance.SetUIText(_text.GetComponent<TextMeshProUGUI>(),hintInfo.HintContent);
}
public void Update()
{
// 预留给未来的浮动动画、生命周期等逻辑
}
/// <summary>
/// 设置该提示为“已关闭”状态
/// </summary>
public void SetFinished(float waitTime = 0.25f)
{
//如果已经点开了
if (_textPanel.activeSelf)
{
var animancer = _textPanel.GetComponent<AnimancerComponent>();
if (animancer != null)
{
animancer.Play(MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide);
Timer.Instance.TimerRegister(this, () => { Finished = true; }, waitTime);
}
else
Finished = true;
}
//如果还没有点开
else if (_bubble.activeSelf)
{
var animancer = _bubble.GetComponent<AnimancerComponent>();
if (animancer != null)
{
animancer.Play(MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide);
Timer.Instance.TimerRegister(this, () => { Finished = true; }, waitTime);
}
else
Finished = true;
}
}
/// <summary>
/// 初始化 UI 元素,绑定点击事件和动画控制
/// </summary>
public void Init()
{
// 查找子节点
_bubble = ROHint.transform.Find("Bubble")?.gameObject;
_text = ROHint.transform.Find("TextPanel/Text")?.gameObject;
_title = ROHint.transform.Find("TextPanel/Title")?.gameObject;
_closeButton = ROHint.transform.Find("TextPanel/CloseButton")?.gameObject;
_textPanel = ROHint.transform.Find("TextPanel")?.gameObject;
// 默认全部隐藏
if (_bubble != null) _bubble.SetActive(false);
if (_textPanel != null) _textPanel.SetActive(false);
// 初始化气泡
if (_bubble != null)
{
// 绑定点击事件:点击气泡后显示文字面板
var btn = _bubble.GetComponent<UnityEngine.UI.Button>();
if (btn != null)
btn.onClick.AddListener(ShowTextPanel);
}
// 初始化关闭按钮
if (_closeButton != null)
{
var btn = _closeButton.GetComponent<UnityEngine.UI.Button>();
if (btn != null)
{
btn.onClick.AddListener(HideTextPanel);
}
}
}
/// <summary>
/// 外部主动触发:显示气泡并播放动画
/// </summary>
public void StartBubble()
{
if (_bubble != null)
{
_bubble.SetActive(true);
var animancer = _bubble.GetComponent<AnimancerComponent>();
if (animancer != null)
{
animancer.Play(MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleShow);
Timer.Instance.TimerRegister(this, () =>
{
animancer.Play(MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleIdle);
},0.25f);
}
}
}
public void ShowTextPanel()
{
HideBubble();
_textPanel.SetActive(true);
var animancer = _textPanel.GetComponent<AnimancerComponent>();
if (animancer != null)
{
animancer.Play(MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleShow);
}
}
public void HideBubble()
{
var animancer = _bubble.GetComponent<AnimancerComponent>();
animancer.Play(MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide);
var timeToWait = 0f;
if (MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide != null)
timeToWait = MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide.length;
Timer.Instance.TimerRegister(this, () =>
{
_bubble.SetActive(false);
},timeToWait);
}
public void HideTextPanel()
{
var timeToWait = 0f;
if (MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide != null)
timeToWait = MapRenderer.Instance.ResourceCache.AnimCache.HintBubbleHide.length;
SetFinished(timeToWait);
}
}
}