2025-08-25 18:24:12 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-08-26 00:11:36 +08:00
|
|
|
|
using Logging;
|
|
|
|
|
using Map;
|
2025-07-08 09:42:27 +08:00
|
|
|
|
using UnityEngine;
|
2025-08-26 00:11:36 +08:00
|
|
|
|
using Utils;
|
2025-07-08 09:42:27 +08:00
|
|
|
|
|
2025-08-25 18:24:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Program 类作为单例模式的核心管理器,负责维护和管理游戏或应用中的维度(Dimension)实例和焦点状态。
|
|
|
|
|
/// 它提供了维度注册、注销、获取以及焦点维度设置的功能。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Program : Singleton<Program>
|
2025-07-08 09:42:27 +08:00
|
|
|
|
{
|
2025-08-26 00:11:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 指示是否需要加载数据的标志,例如在游戏启动或场景切换时。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool needLoad = true;
|
|
|
|
|
|
2025-08-25 18:24:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前聚焦的实体对象,可能为空。
|
|
|
|
|
/// </summary>
|
2025-08-23 16:43:28 +08:00
|
|
|
|
public Entity.Entity focusedEntity = null;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前活跃焦点的维度唯一标识符,可能为空。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string focuseDimensionId = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前聚焦的维度对象实例。当 <see cref="focuseDimensionId"/> 不为空时,此属性指向对应的维度实例。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Dimension FocusedDimension { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 维护所有已注册的维度实例的字典,键是维度的唯一标识符 (ID)。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Dictionary<string, Dimension> _registeredDimensions = new Dictionary<string, Dimension>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有已注册的维度。返回一个只读字典副本,防止外部直接修改。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyDictionary<string, Dimension> RegisteredDimensions => _registeredDimensions;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 注册一个维度实例到 Program。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dimension">要注册的维度实例。</param>
|
|
|
|
|
public void RegisterDimension(Dimension dimension)
|
|
|
|
|
{
|
|
|
|
|
if (dimension == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("[Program] Attempted to register a null Dimension.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string id = dimension.DimensionId;
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"[Program] Attempted to register a Dimension with an empty or null ID: {dimension.name}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_registeredDimensions.ContainsKey(id))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[Program] Dimension with ID '{id}' is already registered. Skipping duplicate registration for {dimension.name}.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_registeredDimensions.Add(id, dimension);
|
|
|
|
|
|
2025-08-26 00:11:36 +08:00
|
|
|
|
// 如果注册的维度是当前焦点维度,则更新 FocusedDimension 属性
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (focuseDimensionId == id)
|
|
|
|
|
{
|
|
|
|
|
FocusedDimension = dimension;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从 Program 注销一个维度实例。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dimension">要注销的维度实例。</param>
|
|
|
|
|
public void UnregisterDimension(Dimension dimension)
|
|
|
|
|
{
|
|
|
|
|
if (dimension == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("[Program] Attempted to unregister a null Dimension. Skipping.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string id = dimension.DimensionId;
|
|
|
|
|
if (_registeredDimensions.Remove(id))
|
|
|
|
|
{
|
2025-08-26 00:11:36 +08:00
|
|
|
|
// 如果注销的维度是当前焦点维度,则清除焦点
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (focuseDimensionId == id)
|
|
|
|
|
{
|
|
|
|
|
focuseDimensionId = null;
|
|
|
|
|
FocusedDimension = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[Program] Attempted to unregister Dimension '{id}' which was not found in the registered list.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据ID获取一个已注册的维度。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dimensionId">维度的唯一标识符。</param>
|
|
|
|
|
/// <returns>对应的Dimension实例,如果未找到则返回null。</returns>
|
|
|
|
|
public Dimension GetDimension(string dimensionId)
|
|
|
|
|
{
|
|
|
|
|
_registeredDimensions.TryGetValue(dimensionId, out Dimension dimension);
|
|
|
|
|
return dimension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置当前聚焦的维度。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dimensionId">要设置为焦点的维度的唯一标识符。如果传入null或空字符串,将清除当前焦点维度。</param>
|
|
|
|
|
public void SetFocusedDimension(string dimensionId)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(dimensionId))
|
|
|
|
|
{
|
|
|
|
|
focuseDimensionId = null;
|
|
|
|
|
FocusedDimension = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_registeredDimensions.TryGetValue(dimensionId, out Dimension newFocusedDimension))
|
|
|
|
|
{
|
|
|
|
|
focuseDimensionId = dimensionId;
|
|
|
|
|
FocusedDimension = newFocusedDimension;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[Program] Attempted to set focus to unregistered dimension ID: '{dimensionId}'. Focus not changed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|