2025-09-19 08:26:54 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Data;
|
|
|
|
|
|
using Managers;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Map
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 表示游戏中的一个维度或场景区域。
|
|
|
|
|
|
/// 实体管理器将根据此维度来组织和管理实体。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class Dimension : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
[SerializeField] private bool defaultOpen = false;
|
|
|
|
|
|
|
2025-08-28 16:20:24 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
|
[Tooltip("此维度的唯一标识符。如果为空,将使用GameObject的名称。")]
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private string _dimensionId;
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
[SerializeField] public Landform landform;
|
2025-08-28 16:20:24 +08:00
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
public Vector3 cameraPosition = new(0, 0, -10);
|
2025-09-03 19:59:22 +08:00
|
|
|
|
|
|
|
|
|
|
public Entity.Entity focusEntity;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
|
|
|
|
|
public string mapGeneratorId;
|
|
|
|
|
|
public DimensionDef dimensionDefinition;
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
public Vector2Int DimensionSize { get;private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public event System.Action<Dimension> OnDimensionLoaded;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取此维度的唯一标识符。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string DimensionId
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(_dimensionId))
|
|
|
|
|
|
{
|
|
|
|
|
|
_dimensionId = gameObject.name; // 如果未设置,默认使用GameObject名称
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _dimensionId;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 内部设置器,允许在Awake中初始化或编辑器中赋值
|
|
|
|
|
|
private set => _dimensionId = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 此维度下所有实体的根Transform。用于组织场景层级。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Transform DimensionRoot { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 1. 确保 DimensionId 已初始化,这会触发 DimensionId 属性的 getter 逻辑
|
|
|
|
|
|
var id = DimensionId;
|
|
|
|
|
|
// 2. 创建一个用于存放此维度下所有实体的根GameObject,方便管理
|
|
|
|
|
|
var rootObj = new GameObject($"_Entities_{id}");
|
|
|
|
|
|
rootObj.transform.SetParent(this.transform); // 将其作为Dimension对象的子对象
|
|
|
|
|
|
DimensionRoot = rootObj.transform;
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
Program.Instance.RegisterDimension(this);
|
|
|
|
|
|
landform.Init();
|
2025-09-03 19:59:22 +08:00
|
|
|
|
if (defaultOpen)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
Program.Instance.SetFocusedDimension(_dimensionId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 15:02:57 +08:00
|
|
|
|
mapGeneratorId = Configs.ConfigManager.Instance.GetValue<string>(DimensionId);
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(mapGeneratorId))
|
|
|
|
|
|
{
|
|
|
|
|
|
dimensionDefinition=DefineManager.Instance.FindDefine<DimensionDef>(mapGeneratorId);
|
|
|
|
|
|
if (dimensionDefinition != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_=ApplyDimensionDef(dimensionDefinition);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
2025-09-03 19:59:22 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 当 Dimension 对象被销毁时(例如,场景卸载),从 Program 和 EntityManage 注销
|
|
|
|
|
|
if (Program.Instance != null) // 检查单例是否仍然存在
|
|
|
|
|
|
{
|
|
|
|
|
|
Program.Instance.UnregisterDimension(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
|
|
|
|
|
private async Task ApplyDimensionDef(DimensionDef def)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
var maxWidth = DimensionSize.x; // 初始化为当前的维度尺寸
|
|
|
|
|
|
var maxHeight = DimensionSize.y; // 初始化为当前的维度尺寸
|
|
|
|
|
|
|
2025-09-19 08:26:54 +08:00
|
|
|
|
if (def.mapGenerators != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var defMapGenerator in def.mapGenerators)
|
|
|
|
|
|
{
|
2025-09-28 15:02:57 +08:00
|
|
|
|
var workClass = TileManager.Instance.GetMapGeneratorWorkClass(defMapGenerator.defName);
|
|
|
|
|
|
if (workClass == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
var requiredSize = workClass.GetSize();
|
|
|
|
|
|
maxWidth = Mathf.Max(maxWidth, requiredSize.x);
|
|
|
|
|
|
maxHeight = Mathf.Max(maxHeight, requiredSize.y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DimensionSize = new Vector2Int(maxWidth, maxHeight);
|
|
|
|
|
|
cameraPosition = new Vector3(DimensionSize.x / 2f, DimensionSize.y / 2f, -10)+transform.position;
|
|
|
|
|
|
foreach (var defMapGenerator in def.mapGenerators)
|
|
|
|
|
|
{
|
|
|
|
|
|
await TileManager.Instance.ApplyMapGenerator(defMapGenerator.defName, landform);
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
OnDimensionLoaded.Invoke(this);
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
2025-09-28 15:02:57 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|