using System.Collections.Generic; using Logging; // Assuming 'Logging' is a standard or project-specific namespace. using Map; // Assuming 'Map' is a standard or project-specific namespace. using UnityEngine; using Utils; // Assuming 'Utils' is a standard or project-specific namespace. /// /// Program 类作为单例模式的核心管理器,负责维护和管理游戏或应用中的维度(Dimension)实例和焦点状态。 /// 它提供了维度注册、注销、获取以及焦点维度设置的功能。 /// public class Program : Singleton { /// /// 当前聚焦的实体对象,可能为空。 /// public Entity.Entity focusedEntity = null; /// /// 当前活跃焦点的维度唯一标识符,可能为空。 /// public string focuseDimensionId = null; /// /// 当前聚焦的维度对象实例。当 不为空时,此属性指向对应的维度实例。 /// public Dimension FocusedDimension { get; private set; } /// /// 维护所有已注册的维度实例的字典,键是维度的唯一标识符 (ID)。 /// private readonly Dictionary _registeredDimensions = new Dictionary(); /// /// 获取所有已注册的维度。返回一个只读字典副本,防止外部直接修改。 /// public IReadOnlyDictionary RegisteredDimensions => _registeredDimensions; /// /// 注册一个维度实例到 Program。 /// /// 要注册的维度实例。 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); Debug.Log($"[Program] Dimension '{id}' registered successfully."); if (focuseDimensionId == id) { FocusedDimension = dimension; Debug.Log($"[Program] FocusedDimension updated to '{id}'."); } } /// /// 从 Program 注销一个维度实例。 /// /// 要注销的维度实例。 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)) { Debug.Log($"[Program] Dimension '{id}' unregistered successfully."); if (focuseDimensionId == id) { focuseDimensionId = null; FocusedDimension = null; Debug.Log($"[Program] Focused dimension '{id}' was unregistered and has been cleared."); } } else { Debug.LogWarning($"[Program] Attempted to unregister Dimension '{id}' which was not found in the registered list."); } } /// /// 根据ID获取一个已注册的维度。 /// /// 维度的唯一标识符。 /// 对应的Dimension实例,如果未找到则返回null。 public Dimension GetDimension(string dimensionId) { _registeredDimensions.TryGetValue(dimensionId, out Dimension dimension); return dimension; } /// /// 设置当前聚焦的维度。 /// /// 要设置为焦点的维度的唯一标识符。如果传入null或空字符串,将清除当前焦点维度。 public void SetFocusedDimension(string dimensionId) { if (string.IsNullOrEmpty(dimensionId)) { focuseDimensionId = null; FocusedDimension = null; Debug.Log("[Program] Focused dimension cleared."); return; } if (_registeredDimensions.TryGetValue(dimensionId, out Dimension newFocusedDimension)) { focuseDimensionId = dimensionId; FocusedDimension = newFocusedDimension; Debug.Log($"[Program] Focused dimension set to '{dimensionId}'."); } else { Debug.LogWarning($"[Program] Attempted to set focus to unregistered dimension ID: '{dimensionId}'. Focus not changed."); } } }