2025-08-26 16:00:58 +08:00
|
|
|
|
using System; // 添加引用,用于 Action 委托
|
2025-08-25 18:24:12 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-08-26 00:11:36 +08:00
|
|
|
|
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>
|
|
|
|
|
/// 当前活跃焦点的维度唯一标识符,可能为空。
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// 变更为属性,并私有化setter,确保通过 SetFocusedDimension 方法集中管理其更新。
|
2025-08-25 18:24:12 +08:00
|
|
|
|
/// </summary>
|
2025-08-26 16:00:58 +08:00
|
|
|
|
public string focuseDimensionId { get; private set; } = null;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当焦点维度发生变化时触发的事件。
|
|
|
|
|
/// 事件参数为新的焦点 Dimension 实例,如果焦点被清除则为 null。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<Dimension> OnFocusedDimensionChanged;
|
|
|
|
|
|
2025-08-25 18:24:12 +08:00
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2025-08-26 16:00:58 +08:00
|
|
|
|
var id = dimension.DimensionId;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
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 16:00:58 +08:00
|
|
|
|
// 修改理由:确保任何对焦点的潜在更新都通过 SetFocusedDimension 进行,
|
|
|
|
|
// 从而集中管理焦点状态的同步和事件的触发。
|
|
|
|
|
// 如果注册的维度恰好是当前 focuseDimensionId, SetFocusedDimension(id) 将负责更新 FocusedDimension 并触发事件。
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (focuseDimensionId == id)
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
SetFocusedDimension(id);
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2025-08-26 16:00:58 +08:00
|
|
|
|
var id = dimension.DimensionId;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (_registeredDimensions.Remove(id))
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
// 修改理由:确保任何对焦点的潜在更新都通过 SetFocusedDimension 进行,
|
|
|
|
|
// 从而集中管理焦点状态的同步和事件的触发。
|
|
|
|
|
// 如果注销的维度是当前焦点维度,则调用 SetFocusedDimension(null) 清除焦点并触发事件。
|
2025-08-25 18:24:12 +08:00
|
|
|
|
if (focuseDimensionId == id)
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
SetFocusedDimension(null); // 清除焦点
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
_registeredDimensions.TryGetValue(dimensionId, out var dimension);
|
2025-08-25 18:24:12 +08:00
|
|
|
|
return dimension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置当前聚焦的维度。
|
2025-08-26 16:00:58 +08:00
|
|
|
|
/// 这是更改焦点维度的唯一官方入口。
|
2025-08-25 18:24:12 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dimensionId">要设置为焦点的维度的唯一标识符。如果传入null或空字符串,将清除当前焦点维度。</param>
|
|
|
|
|
public void SetFocusedDimension(string dimensionId)
|
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
// 1. 确定新的焦点维度及其ID
|
|
|
|
|
Dimension newFocusedDimension = null; // 默认为清除焦点
|
|
|
|
|
string newFocuseDimensionId = null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(dimensionId))
|
2025-08-25 18:24:12 +08:00
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
if (_registeredDimensions.TryGetValue(dimensionId, out var foundDimension))
|
|
|
|
|
{
|
|
|
|
|
newFocuseDimensionId = dimensionId;
|
|
|
|
|
newFocusedDimension = foundDimension;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 如果尝试设置未注册的维度,警告并直接返回,不改变当前焦点状态。
|
|
|
|
|
Debug.LogWarning($"[Program] Attempted to set focus to unregistered dimension ID: '{dimensionId}'. Focus not changed.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
2025-08-26 16:00:58 +08:00
|
|
|
|
// 如果 dimensionId 为 null 或空, newFocusedDimension 和 newFocuseDimensionId 将保持为 null,表示清除焦点。
|
|
|
|
|
|
|
|
|
|
// 2. 优化:检查是否实际发生变化,避免不必要的更新和事件触发
|
|
|
|
|
// 只有当新的ID或新的维度对象引用与当前的不一致时,才执行更新。
|
|
|
|
|
// 例如,如果 focuseDimensionId 已经是 "someId",FocusedDimension 已经是 "someDimensionObject",
|
|
|
|
|
// 再次调用 SetFocusedDimension("someId") 不会触发事件。
|
|
|
|
|
// 但如果 focuseDimensionId 是 "someId",FocusedDimension 是 null (因为维度还未注册),
|
|
|
|
|
// 而此时注册了 "someId" 对应的维度并调用 SetFocusedDimension("someId"),FocusedDimension 将从 null 变为实际对象,
|
|
|
|
|
// 这算作一次变化,会触发事件。
|
|
|
|
|
if (focuseDimensionId == newFocuseDimensionId && FocusedDimension == newFocusedDimension)
|
2025-08-25 18:24:12 +08:00
|
|
|
|
{
|
2025-08-26 16:00:58 +08:00
|
|
|
|
// Debug.Log($"[Program] SetFocusedDimension: ID '{dimensionId}' already current. No change needed.");
|
|
|
|
|
return;
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
2025-08-26 16:00:58 +08:00
|
|
|
|
|
|
|
|
|
// 3. 更新内部状态
|
|
|
|
|
focuseDimensionId = newFocuseDimensionId;
|
|
|
|
|
FocusedDimension = newFocusedDimension;
|
|
|
|
|
|
|
|
|
|
// 4. 触发事件
|
|
|
|
|
OnFocusedDimensionChanged?.Invoke(FocusedDimension);
|
2025-08-25 18:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|