2025-08-28 16:20:24 +08:00
|
|
|
|
using Map;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
using Base;
|
|
|
|
|
|
using Data;
|
|
|
|
|
|
using Managers;
|
2025-07-08 09:42:27 +08:00
|
|
|
|
using UnityEngine;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using Utils;
|
2025-07-08 09:42:27 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Program 类作为单例模式的核心管理器,负责维护和管理游戏或应用中的维度(Dimension)实例和焦点状态。
|
|
|
|
|
|
/// 它提供了维度注册、注销、获取以及焦点维度设置的功能。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class Program : Singleton<Program>
|
2025-07-08 09:42:27 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 当前角色定义
|
|
|
|
|
|
public CharacterDef CurrentCharacter { get;private set; }
|
|
|
|
|
|
|
|
|
|
|
|
// 指示是否需要加载数据的标志,例如在游戏启动或场景切换时。
|
|
|
|
|
|
private bool _needLoad = true;
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 指示是否需要加载数据的标志,例如在游戏启动或场景切换时。
|
|
|
|
|
|
/// </summary>
|
2025-09-28 15:02:57 +08:00
|
|
|
|
public bool NeedLoad
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _needLoad;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value)
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentCharacter = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
_needLoad = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool OnPlay => CurrentCharacter != null;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前聚焦的实体对象。
|
|
|
|
|
|
/// 变更为属性,并私有化setter,确保通过 SetFocusedEntity 方法集中管理其更新和事件触发。
|
|
|
|
|
|
/// </summary>
|
2025-09-19 08:26:54 +08:00
|
|
|
|
public Entity.Entity FocusedEntity => FocusedDimension?.focusEntity;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当焦点实体发生变化时触发的事件。
|
|
|
|
|
|
/// 事件参数为新的焦点 Entity 实例,如果焦点被清除则为 null。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event Action<Entity.Entity> OnFocusedEntityChanged;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前活跃焦点的维度唯一标识符,可能为空。
|
|
|
|
|
|
/// 变更为属性,并私有化setter,确保通过 SetFocusedDimension 方法集中管理其更新。
|
|
|
|
|
|
/// </summary>
|
2025-08-28 16:20:24 +08:00
|
|
|
|
public string FocusedDimensionId { get; private set; } = null;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前聚焦的维度对象实例。当 <see cref="FocusedDimensionId"/> 不为空时,此属性指向对应的维度实例。
|
|
|
|
|
|
/// </summary>
|
2025-08-28 16:20:24 +08:00
|
|
|
|
public Dimension FocusedDimension { get; private set; }
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 维护所有已注册的维度实例的字典,键是维度的唯一标识符 (ID)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly Dictionary<string, Dimension> _registeredDimensions = new Dictionary<string, Dimension>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有已注册的维度。返回一个只读字典副本,防止外部直接修改。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public IReadOnlyDictionary<string, Dimension> RegisteredDimensions => _registeredDimensions;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当焦点维度发生变化时触发的事件。
|
|
|
|
|
|
/// 事件参数为新的焦点 Dimension 实例,如果焦点被清除则为 null。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event Action<Dimension> OnFocusedDimensionChanged;
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取已注册维度的数量。
|
|
|
|
|
|
/// </summary>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public int DimensionCount => _registeredDimensions.Count;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有已注册维度的ID数组。
|
|
|
|
|
|
/// </summary>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public string[] Dimensions => _registeredDimensions.Keys.ToArray();
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool CanOpenRightMenu => Setting.Instance.CurrentSettings.developerMode && !OnPlay;
|
|
|
|
|
|
public bool CanMoveCamera => CanOpenRightMenu;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册一个维度实例到 Program。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dimension">要注册的维度实例。</param>
|
|
|
|
|
|
public void RegisterDimension(Dimension dimension)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dimension == null)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Debug.LogError("[Program] 尝试注册一个空的维度。");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var id = dimension.DimensionId;
|
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Debug.LogError($"[Program] 尝试注册一个ID为空的维度: {dimension.name}");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
if (!_registeredDimensions.TryAdd(id, dimension))
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Debug.LogWarning($"[Program] ID为 '{id}' 的维度已注册。跳过重复注册 {dimension.name}。");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-08-28 16:20:24 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 如果注册的维度恰好是当前 focusedDimensionId,SetFocusedDimension(id) 将负责更新 FocusedDimension 并触发事件。
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 并且,由于SetFocusedDimension会清空FocusedEntity,这也符合新维度的行为。
|
|
|
|
|
|
if (FocusedDimensionId == id)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetFocusedDimension(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 Program 注销一个维度实例。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dimension">要注销的维度实例。</param>
|
|
|
|
|
|
public void UnregisterDimension(Dimension dimension)
|
2025-07-08 09:42:27 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (dimension == null)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Debug.LogWarning("[Program] 尝试注销一个空的维度。");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var id = dimension.DimensionId;
|
|
|
|
|
|
if (_registeredDimensions.Remove(id))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果注销的维度是当前焦点维度,则调用 SetFocusedDimension(null) 清除焦点并触发事件。
|
|
|
|
|
|
if (FocusedDimensionId == id)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetFocusedDimension(null); // 清除焦点
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Debug.LogWarning($"[Program] 尝试注销ID为 '{id}' 的维度,但未在已注册列表中找到。");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
2025-07-08 09:42:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID获取一个已注册的维度。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dimensionId">维度的唯一标识符。</param>
|
|
|
|
|
|
/// <returns>对应的Dimension实例,如果未找到则返回null。</returns>
|
|
|
|
|
|
public Dimension GetDimension(string dimensionId)
|
2025-07-08 09:42:27 +08:00
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
if (dimensionId == null)
|
|
|
|
|
|
return null;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
_registeredDimensions.TryGetValue(dimensionId, out var dimension);
|
|
|
|
|
|
return dimension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置当前聚焦的维度。
|
|
|
|
|
|
/// 这是更改焦点维度的唯一官方入口。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dimensionId">要设置为焦点的维度的唯一标识符。如果传入null或空字符串,将清除当前焦点维度。</param>
|
|
|
|
|
|
public void SetFocusedDimension(string dimensionId)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 确定新的焦点维度及其ID
|
2025-08-27 19:56:49 +08:00
|
|
|
|
Dimension newFocusedDimension = null; // 默认为清除焦点
|
2025-09-28 15:02:57 +08:00
|
|
|
|
string newFocusedDimensionId = null;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(dimensionId))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_registeredDimensions.TryGetValue(dimensionId, out var foundDimension))
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
newFocusedDimensionId = dimensionId;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
newFocusedDimension = foundDimension;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果尝试设置未注册的维度,警告并直接返回,不改变当前焦点状态。
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Debug.LogWarning($"[Program] 尝试设置焦点到未注册的维度ID: '{dimensionId}'。焦点未改变。");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果 dimensionId 为 null 或空, newFocusedDimension 和 newFocusedDimensionId 将保持为 null,表示清除焦点。
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 检查是否实际发生变化
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (FocusedDimensionId == newFocusedDimensionId && FocusedDimension == newFocusedDimension)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (FocusedEntity)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
FocusedEntity.PlayerControlled = false;
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 更新内部状态
|
|
|
|
|
|
FocusedDimensionId = newFocusedDimensionId;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
FocusedDimension = newFocusedDimension;
|
2025-08-28 16:20:24 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 确保功能一致性:当维度焦点改变(或被清除)时,任何实体焦点也应被清除。
|
|
|
|
|
|
SetFocusedEntity(null);
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 触发事件
|
2025-08-27 19:56:49 +08:00
|
|
|
|
OnFocusedDimensionChanged?.Invoke(FocusedDimension);
|
|
|
|
|
|
}
|
2025-08-28 16:20:24 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置当前聚焦的实体。
|
|
|
|
|
|
/// 这是更改焦点实体的唯一官方入口,并会在实体改变时触发 OnFocusedEntityChanged 事件。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要设置为焦点的实体实例。如果传入null,将清除当前焦点实体。</param>
|
|
|
|
|
|
public void SetFocusedEntity(Entity.Entity entity)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
// 检查是否实际发生变化
|
|
|
|
|
|
if (FocusedEntity == entity || !FocusedDimension)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
FocusedDimension.focusEntity = entity;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 触发事件,将新的焦点实体(或 null)作为参数传递。
|
|
|
|
|
|
OnFocusedEntityChanged?.Invoke(FocusedEntity);
|
2025-07-08 09:42:27 +08:00
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
public void PlayGame(CharacterDef character)
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentCharacter = character;
|
|
|
|
|
|
if (character == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
var pos = CameraControl.CameraControl.Instance.Position;
|
|
|
|
|
|
var player = EntityManager.Instance.GenerateEntity(FocusedDimensionId, character, new Vector3(pos.x, pos.y));
|
|
|
|
|
|
if(!player)
|
|
|
|
|
|
return;
|
|
|
|
|
|
player.entity.PlayerControlled = true;
|
|
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|