Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Program.cs

135 lines
5.0 KiB
C#
Raw Normal View History

2025-08-25 18:24:12 +08:00
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.
2025-07-08 09:42:27 +08:00
using UnityEngine;
2025-08-25 18:24:12 +08:00
using Utils; // Assuming 'Utils' is a standard or project-specific namespace.
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-25 18:24:12 +08:00
/// <summary>
/// 当前聚焦的实体对象,可能为空。
/// </summary>
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);
Debug.Log($"[Program] Dimension '{id}' registered successfully.");
if (focuseDimensionId == id)
{
FocusedDimension = dimension;
Debug.Log($"[Program] FocusedDimension updated to '{id}'.");
}
}
/// <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))
{
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.");
}
}
/// <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;
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.");
}
}
}