Add room injection processor.

加入房间注入处理器。
This commit is contained in:
Cold-Mint 2024-05-28 22:14:35 +08:00
parent ea319f032e
commit 959dcc3092
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
5 changed files with 122 additions and 1 deletions

View File

@ -50,6 +50,13 @@ public partial class GameSceneLoader : SceneLoaderTemplate
MapGenerator.LayoutStrategy = new TestLayoutStrategy();
MapGenerator.LayoutParsingStrategy = new SequenceLayoutParsingStrategy();
MapGenerator.RoomPlacementStrategy = new PatchworkRoomPlacementStrategy();
MapGenerator.MapGenerationCompleteEvent += (_) =>
{
};
MapGenerator.MapGenerationStartEvent+= (_) =>
{
};
await GenerateMap();
}

View File

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using ColdMint.scripts.debug;
using ColdMint.scripts.map.dateBean;
using ColdMint.scripts.map.events;
using ColdMint.scripts.map.interfaces;
using ColdMint.scripts.map.LayoutParsingStrategy;
using ColdMint.scripts.map.layoutStrategy;
@ -43,6 +44,59 @@ public static class MapGenerator
private static ulong _seed;
private static Dictionary<string, IRoomInjectionProcessor>? _roomInjectionProcessorsDictionary;
/// <summary>
/// <para>Register the room injection processor</para>
/// <para>注册房间注入处理器</para>
/// </summary>
/// <param name="roomInjectionProcessor"></param>
/// <returns></returns>
public static bool RegisterRoomInjectionProcessor(IRoomInjectionProcessor roomInjectionProcessor)
{
var key = roomInjectionProcessor.GetId();
if (_roomInjectionProcessorsDictionary == null)
{
_roomInjectionProcessorsDictionary = new Dictionary<string, IRoomInjectionProcessor>
{ { key, roomInjectionProcessor } };
return true;
}
return _roomInjectionProcessorsDictionary.TryAdd(key, roomInjectionProcessor);
}
/// <summary>
/// <para>Log out of the room injection processor</para>
/// <para>注销房间注入处理器</para>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool UnRegisterRoomInjectionProcessor(string id)
{
if (_roomInjectionProcessorsDictionary == null)
{
return false;
}
return _roomInjectionProcessorsDictionary.Remove(id);
}
public delegate void MapGenerationStartEventHandler(MapGenerationStartEvent mapGenerationStartEvent);
public delegate void MapGenerationCompleteEventHandler(MapGenerationCompleteEvent mapGenerationCompleteEvent);
/// <summary>
/// <para>Map starts generating events</para>
/// <para>地图开始生成的事件</para>
/// </summary>
public static event MapGenerationStartEventHandler? MapGenerationStartEvent;
/// <summary>
/// <para>Map generation completion event</para>
/// <para>地图生成完成事件</para>
/// </summary>
public static event MapGenerationCompleteEventHandler? MapGenerationCompleteEvent;
/// <summary>
/// <para>Set seed</para>
/// <para>设置种子</para>
@ -98,6 +152,7 @@ public static class MapGenerator
}
_running = true;
MapGenerationStartEvent?.Invoke(new MapGenerationStartEvent());
if (_layoutStrategy == null || _roomPlacementStrategy == null || _layoutParsingStrategy == null ||
_mapRoot == null)
{
@ -197,6 +252,13 @@ public static class MapGenerator
//所有房间已放置完毕。
await _roomPlacementStrategy.GeneratedComplete(_mapRoot);
_running = false;
//Invoke the map generation completion event
//调用地图生成完成事件
var eventObj = new MapGenerationCompleteEvent
{
RandomNumberGenerator = randomNumberGenerator
};
MapGenerationCompleteEvent?.Invoke(eventObj);
}
/// <summary>

View File

@ -0,0 +1,17 @@

using Godot;
namespace ColdMint.scripts.map.events;
/// <summary>
/// <para>Event when the map is created</para>
/// <para>地图创建完成的事件</para>
/// </summary>
public class MapGenerationCompleteEvent
{
/// <summary>
/// <para>Random number generator generated from seed</para>
/// <para>根据种子生成的随机数生成器</para>
/// </summary>
public RandomNumberGenerator? RandomNumberGenerator { get; set; }
}

View File

@ -0,0 +1,10 @@
namespace ColdMint.scripts.map.events;
/// <summary>
/// <para>The map generator starts the event</para>
/// <para>地图生成器启动事件</para>
/// </summary>
public class MapGenerationStartEvent
{
}

View File

@ -0,0 +1,25 @@
using System.Threading.Tasks;
using ColdMint.scripts.map.dateBean;
namespace ColdMint.scripts.map.interfaces;
/// <summary>
/// <para>Room injection processor</para>
/// <para>房间注入处理器</para>
/// </summary>
public interface IRoomInjectionProcessor
{
/// <summary>
/// <para>The room injection processor has an ID</para>
/// <para>房间注入处理器有一个ID</para>
/// </summary>
/// <returns></returns>
public string GetId();
/// <summary>
/// <para>The processing method should return to the room to place the data</para>
/// <para>处理方法,应当返回房间放置数据</para>
/// </summary>
/// <returns></returns>
public Task<RoomPlacementData?> Processor();
}