diff --git a/scripts/loader/sceneLoader/GameSceneLoader.cs b/scripts/loader/sceneLoader/GameSceneLoader.cs index 5683de9..5378dfe 100644 --- a/scripts/loader/sceneLoader/GameSceneLoader.cs +++ b/scripts/loader/sceneLoader/GameSceneLoader.cs @@ -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(); } diff --git a/scripts/map/MapGenerator.cs b/scripts/map/MapGenerator.cs index 4986f66..037f7c0 100644 --- a/scripts/map/MapGenerator.cs +++ b/scripts/map/MapGenerator.cs @@ -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? _roomInjectionProcessorsDictionary; + + /// + /// Register the room injection processor + /// 注册房间注入处理器 + /// + /// + /// + public static bool RegisterRoomInjectionProcessor(IRoomInjectionProcessor roomInjectionProcessor) + { + var key = roomInjectionProcessor.GetId(); + if (_roomInjectionProcessorsDictionary == null) + { + _roomInjectionProcessorsDictionary = new Dictionary + { { key, roomInjectionProcessor } }; + return true; + } + + return _roomInjectionProcessorsDictionary.TryAdd(key, roomInjectionProcessor); + } + + /// + /// Log out of the room injection processor + /// 注销房间注入处理器 + /// + /// + /// + 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); + + /// + /// Map starts generating events + /// 地图开始生成的事件 + /// + public static event MapGenerationStartEventHandler? MapGenerationStartEvent; + + /// + /// Map generation completion event + /// 地图生成完成事件 + /// + public static event MapGenerationCompleteEventHandler? MapGenerationCompleteEvent; + /// /// Set seed /// 设置种子 @@ -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); } /// @@ -231,7 +293,7 @@ public static class MapGenerator } dictionary.Add(roomNodeDataId, roomPlacementData.Room); - LogCat.LogWithFormat("room_placement_information",roomNodeDataId,roomPlacementData.Position.ToString()); + LogCat.LogWithFormat("room_placement_information", roomNodeDataId, roomPlacementData.Position.ToString()); return true; } } \ No newline at end of file diff --git a/scripts/map/events/MapGenerationCompleteEvent.cs b/scripts/map/events/MapGenerationCompleteEvent.cs new file mode 100644 index 0000000..b39a1e9 --- /dev/null +++ b/scripts/map/events/MapGenerationCompleteEvent.cs @@ -0,0 +1,17 @@ + +using Godot; + +namespace ColdMint.scripts.map.events; + +/// +/// Event when the map is created +/// 地图创建完成的事件 +/// +public class MapGenerationCompleteEvent +{ + /// + /// Random number generator generated from seed + /// 根据种子生成的随机数生成器 + /// + public RandomNumberGenerator? RandomNumberGenerator { get; set; } +} \ No newline at end of file diff --git a/scripts/map/events/MapGenerationStartEvent.cs b/scripts/map/events/MapGenerationStartEvent.cs new file mode 100644 index 0000000..e18d7a8 --- /dev/null +++ b/scripts/map/events/MapGenerationStartEvent.cs @@ -0,0 +1,10 @@ +namespace ColdMint.scripts.map.events; + +/// +/// The map generator starts the event +/// 地图生成器启动事件 +/// +public class MapGenerationStartEvent +{ + +} \ No newline at end of file diff --git a/scripts/map/interfaces/IRoomInjectionProcessor.cs b/scripts/map/interfaces/IRoomInjectionProcessor.cs new file mode 100644 index 0000000..f7a03e5 --- /dev/null +++ b/scripts/map/interfaces/IRoomInjectionProcessor.cs @@ -0,0 +1,25 @@ +using System.Threading.Tasks; +using ColdMint.scripts.map.dateBean; + +namespace ColdMint.scripts.map.interfaces; + +/// +/// Room injection processor +/// 房间注入处理器 +/// +public interface IRoomInjectionProcessor +{ + /// + /// The room injection processor has an ID + /// 房间注入处理器有一个ID + /// + /// + public string GetId(); + + /// + /// The processing method should return to the room to place the data + /// 处理方法,应当返回房间放置数据 + /// + /// + public Task Processor(); +} \ No newline at end of file