2024-05-19 12:29:32 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ColdMint.scripts.debug;
|
|
|
|
|
using ColdMint.scripts.map.interfaces;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.map;
|
|
|
|
|
|
2024-05-18 15:35:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Map generator</para>
|
|
|
|
|
/// <para>地图生成器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
///<para>Responsible for the overall map generation process control</para>
|
|
|
|
|
///<para>负责地图的整体生成流程控制</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static class MapGenerator
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-05-08 10:22:04 +00:00
|
|
|
|
/// <summary>
|
2024-05-18 15:35:12 +00:00
|
|
|
|
/// <para>Layout map selection strategy</para>
|
|
|
|
|
/// <para>布局图选择策略</para>
|
2024-05-08 10:22:04 +00:00
|
|
|
|
/// </summary>
|
2024-05-18 15:35:12 +00:00
|
|
|
|
private static ILayoutStrategy? _layoutStrategy;
|
|
|
|
|
|
2024-05-19 12:29:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Room placement strategy</para>
|
|
|
|
|
/// <para>房间的放置策略</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static IRoomPlacementStrategy? _roomPlacementStrategy;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Layout diagram parsing policy</para>
|
|
|
|
|
/// <para>布局图解析策略</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static ILayoutParsingStrategy? _layoutParsingStrategy;
|
|
|
|
|
|
|
|
|
|
public static ILayoutParsingStrategy? LayoutParsingStrategy
|
|
|
|
|
{
|
|
|
|
|
get => _layoutParsingStrategy;
|
|
|
|
|
set => _layoutParsingStrategy = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IRoomPlacementStrategy? RoomPlacementStrategy
|
|
|
|
|
{
|
|
|
|
|
get => _roomPlacementStrategy;
|
|
|
|
|
set => _roomPlacementStrategy = value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 15:35:12 +00:00
|
|
|
|
public static ILayoutStrategy? LayoutStrategy
|
2024-05-08 10:22:04 +00:00
|
|
|
|
{
|
2024-05-18 15:35:12 +00:00
|
|
|
|
get => _layoutStrategy;
|
|
|
|
|
set => _layoutStrategy = value;
|
2024-05-08 10:22:04 +00:00
|
|
|
|
}
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
|
/// <summary>
|
2024-05-18 15:35:12 +00:00
|
|
|
|
/// <para>Generating a map</para>
|
|
|
|
|
/// <para>生成地图</para>
|
2024-05-08 10:22:04 +00:00
|
|
|
|
/// </summary>
|
2024-05-18 15:35:12 +00:00
|
|
|
|
public static async Task GenerateMap()
|
2024-05-08 10:22:04 +00:00
|
|
|
|
{
|
2024-05-19 12:29:32 +00:00
|
|
|
|
if (_layoutStrategy == null || _roomPlacementStrategy == null || _layoutParsingStrategy == null)
|
2024-05-08 10:22:04 +00:00
|
|
|
|
{
|
2024-05-19 12:29:32 +00:00
|
|
|
|
LogCat.LogError("map_generator_missing_parameters");
|
2024-05-18 15:35:12 +00:00
|
|
|
|
return;
|
2024-05-08 10:22:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 15:35:12 +00:00
|
|
|
|
//Get the layout data
|
|
|
|
|
//拿到布局图数据
|
|
|
|
|
var levelGraphEditorSaveData = await _layoutStrategy.GetLayout();
|
|
|
|
|
if (levelGraphEditorSaveData.RoomNodeDataList == null || levelGraphEditorSaveData.RoomNodeDataList.Count == 0)
|
2024-05-08 10:22:04 +00:00
|
|
|
|
{
|
2024-05-19 12:29:32 +00:00
|
|
|
|
LogCat.LogError("map_generator_attempts_to_parse_empty_layout_diagrams");
|
2024-05-18 15:35:12 +00:00
|
|
|
|
return;
|
2024-05-08 10:22:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 12:29:32 +00:00
|
|
|
|
_layoutParsingStrategy.SetLevelGraph(levelGraphEditorSaveData);
|
|
|
|
|
//Save the dictionary, put the ID in the room data, corresponding to the successful placement of the room.
|
|
|
|
|
//保存字典,将房间数据内的ID,对应放置成功的房间。
|
|
|
|
|
var roomDictionary = new Dictionary<string, IRoom>();
|
|
|
|
|
while (await _layoutParsingStrategy.HasNext())
|
2024-05-18 15:35:12 +00:00
|
|
|
|
{
|
2024-05-19 12:29:32 +00:00
|
|
|
|
//When a new room needs to be placed
|
|
|
|
|
//当有新的房间需要放置时
|
|
|
|
|
var roomNodeData = await _layoutParsingStrategy.Next();
|
|
|
|
|
if (roomNodeData == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var nextParentNodeId = await _layoutParsingStrategy.GetNextParentNodeId();
|
|
|
|
|
IRoom? parentRoomNode = null;
|
|
|
|
|
if (nextParentNodeId != null)
|
|
|
|
|
{
|
|
|
|
|
//If the new room has the parent's ID, then we pass the parent's room into the compute function.
|
|
|
|
|
//如果新房间有父节点的ID,那么我们将父节点的房间传入到计算函数内。
|
|
|
|
|
parentRoomNode = roomDictionary[nextParentNodeId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var roomPlacementData =
|
|
|
|
|
await _roomPlacementStrategy.CalculateNewRoomPlacementData(parentRoomNode, roomNodeData);
|
|
|
|
|
if (roomPlacementData == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!await _roomPlacementStrategy.PlaceRoom(roomPlacementData))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(roomNodeData.Id) && roomPlacementData.Room != null)
|
|
|
|
|
{
|
|
|
|
|
roomDictionary.Add(roomNodeData.Id, roomPlacementData.Room);
|
|
|
|
|
}
|
2024-05-18 15:35:12 +00:00
|
|
|
|
}
|
2024-05-19 12:29:32 +00:00
|
|
|
|
//All rooms have been placed.
|
|
|
|
|
//所有房间已放置完毕。
|
2024-05-08 10:22:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|