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

135 lines
4.7 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 System.Collections.Generic;
using Logging;
using Map;
using UnityEngine;
using Utils;
/// <summary>
/// Program 类作为单例模式的核心管理器,负责维护和管理游戏或应用中的维度(Dimension)实例和焦点状态。
/// 它提供了维度注册、注销、获取以及焦点维度设置的功能。
/// </summary>
public class Program : Singleton<Program>
{
/// <summary>
/// 指示是否需要加载数据的标志,例如在游戏启动或场景切换时。
/// </summary>
public bool needLoad = true;
/// <summary>
/// 当前聚焦的实体对象,可能为空。
/// </summary>
public Entity.Entity focusedEntity = null;
/// <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);
// 如果注册的维度是当前焦点维度,则更新 FocusedDimension 属性
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))
{
// 如果注销的维度是当前焦点维度,则清除焦点
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.");
}
}
}