Files
zeling_v2/Assets/_Game/Scripts/UI/Menus/MapTeleportConfirmController.cs
2026-06-05 18:41:33 +08:00

99 lines
4.3 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.
using UnityEngine;
using BaseGames.Core;
using BaseGames.Localization;
using BaseGames.World.Map;
namespace BaseGames.UI.Menus
{
/// <summary>
/// 地图传送确认控制器UI 侧桥接)。
/// <para>
/// 订阅 <see cref="MapPanel.OnTeleportStationSelected"/>:玩家在全屏地图点击一个可传送站点时,
/// 弹出 <see cref="ConfirmDialogController"/> 二次确认,确认后调用
/// <see cref="ITeleportService.RequestTeleport"/> 发起传送,并按需关闭地图面板。
/// </para>
/// <para>
/// 放在 UI 程序集(已引用 BaseGames.World.Map——MapPanel 自身不反向依赖 UI避免循环引用。
/// 这是"补全传送"闭环的最后一环:地图选点 → 确认 → 传送。
/// </para>
/// </summary>
public class MapTeleportConfirmController : MonoBehaviour
{
[Header("引用")]
[Tooltip("全屏地图面板(订阅其 OnTeleportStationSelected。")]
[SerializeField] private MapPanel _mapPanel;
[Tooltip("通用确认框;留空则点击站点后直接传送(无二次确认)。")]
[SerializeField] private ConfirmDialogController _confirmDialog;
[Header("文案(本地化键)")]
[SerializeField] private string _confirmTitleKey = "TELEPORT_CONFIRM_TITLE";
[Tooltip("确认正文前缀本地化键;后面拼接目的地房间显示名。")]
[SerializeField] private string _confirmBodyPrefixKey = "TELEPORT_CONFIRM_BODY";
[SerializeField] private string _confirmYesKey = "CONFIRM_YES";
[SerializeField] private string _confirmNoKey = "CONFIRM_NO";
[Header("行为")]
[Tooltip("确认传送后是否关闭地图面板CloseTopPanel。")]
[SerializeField] private bool _closeMapOnConfirm = true;
private void OnEnable()
{
if (_mapPanel != null)
_mapPanel.OnTeleportStationSelected += OnStationSelected;
}
private void OnDisable()
{
if (_mapPanel != null)
_mapPanel.OnTeleportStationSelected -= OnStationSelected;
}
private void OnStationSelected(string roomId)
{
if (string.IsNullOrEmpty(roomId)) return;
// 无确认框:直接传送
if (_confirmDialog == null)
{
DoTeleport(roomId);
return;
}
string destName = ResolveDestName(roomId);
string prefix = LocalizationManager.Get(_confirmBodyPrefixKey, LocalizationTable.UI);
if (string.IsNullOrEmpty(prefix) || prefix == _confirmBodyPrefixKey)
prefix = "传送到:"; // 本地化键缺失时的兜底前缀
string body = prefix + destName;
// ConfirmDialog 对 body 走 Loc 查找,未命中则原样显示——拼接串可直接呈现;
// 确认/取消按钮文案也走本地化键,随语言切换。
_confirmDialog.Show(_confirmTitleKey, body, onConfirm: () => DoTeleport(roomId),
onCancel: null, confirmKey: _confirmYesKey, cancelKey: _confirmNoKey);
}
private void DoTeleport(string roomId)
{
var teleportSvc = ServiceLocator.GetOrDefault<ITeleportService>();
if (teleportSvc == null)
{
Debug.LogWarning("[MapTeleportConfirmController] ITeleportService 未注册,无法传送。");
return;
}
teleportSvc.RequestTeleport(roomId);
if (_closeMapOnConfirm)
ServiceLocator.GetOrDefault<IUIManager>()?.CloseTopPanel();
}
/// <summary>解析目的地房间的玩家可读名DisplayName 走本地化;无则回退 RoomId。</summary>
private static string ResolveDestName(string roomId)
{
var room = ServiceLocator.GetOrDefault<IMapService>()?.Database?.GetRoom(roomId);
if (room == null || string.IsNullOrEmpty(room.DisplayName)) return roomId;
// DisplayName 为本地化 Key 时解析为译文;为普通名称时原样返回(向后兼容)。
return LocalizationManager.Get(room.DisplayName, LocalizationTable.UI);
}
}
}