Hands over the responsibility of creating room previews to the minimap.

将创建房间预览图的职责移交给迷你地图。
This commit is contained in:
Cold-Mint 2024-09-06 23:36:21 +08:00
parent ab0d05d16d
commit 26c0843202
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
9 changed files with 135 additions and 87 deletions

View File

@ -4,10 +4,10 @@ using ColdMint.scripts.map.events;
namespace ColdMint.scripts;
/// <summary>
/// <para>EventManager</para>
/// <para>事件管理器</para>
/// <para>EventBus</para>
/// <para>事件总线</para>
/// </summary>
public static class EventManager
public static class EventBus
{
/// <summary>
/// <para>Event when the AI character is generated</para>
@ -33,6 +33,12 @@ public static class EventManager
/// </summary>
public static Action<MapGenerationStartEvent>? MapGenerationStartEvent;
/// <summary>
/// <para>New rooms are placed within the map to perform events</para>
/// <para>新的房间被放置在地图内执行的事件</para>
/// </summary>
public static Action<MapGenerationPlaceRoomFinishEvent>? MapGenerationPlaceRoomFinishEvent;
/// <summary>
/// <para>Map generation completion event</para>
/// <para>地图生成完成事件</para>

View File

@ -405,7 +405,7 @@ public partial class Player : CharacterTemplate
{
Hide();
ProcessMode = ProcessModeEnum.Disabled;
if (EventManager.GameOverEvent == null)
if (EventBus.GameOverEvent == null)
{
return;
}
@ -416,7 +416,7 @@ public partial class Player : CharacterTemplate
gameOverEvent.DeathInfo = await DeathInfoGenerator.GenerateDeathInfo(this, damageTemplate.Attacker);
}
EventManager.GameOverEvent.Invoke(gameOverEvent);
EventBus.GameOverEvent.Invoke(gameOverEvent);
}
protected override void EnterThePickingRangeBody(Node node)

View File

@ -22,7 +22,7 @@ public partial class GameOverLoaderMenuLoader : UiLoaderTemplate
_restartButton = GetNodeOrNull<Button>("CenterContainer/VBoxContainer/MarginContainer2/RestartButton");
_deathInfoLabel =
GetNode<Label>("CenterContainer/VBoxContainer/MarginContainer/CenterContainer2/DeathInfoLabel");
EventManager.GameOverEvent += OnGameOver;
EventBus.GameOverEvent += OnGameOver;
}
public override void LoadUiActions()
@ -32,7 +32,7 @@ public partial class GameOverLoaderMenuLoader : UiLoaderTemplate
_restartButton.Pressed += () =>
{
var replayEvent = new GameReplayEvent();
EventManager.GameReplayEvent?.Invoke(replayEvent);
EventBus.GameReplayEvent?.Invoke(replayEvent);
Hide();
};
}
@ -52,6 +52,6 @@ public partial class GameOverLoaderMenuLoader : UiLoaderTemplate
public override void _ExitTree()
{
base._ExitTree();
EventManager.GameOverEvent -= OnGameOver;
EventBus.GameOverEvent -= OnGameOver;
}
}

View File

@ -23,7 +23,7 @@ public partial class AiCharacterSpawn : Marker2D
_packedScene = GD.Load<PackedScene>(ResPath);
}
EventManager.AiCharacterGenerateEvent += OnAiCharacterGenerateEvent;
EventBus.AiCharacterGenerateEvent += OnAiCharacterGenerateEvent;
}
/// <summary>
@ -55,6 +55,6 @@ public partial class AiCharacterSpawn : Marker2D
public override void _ExitTree()
{
EventManager.AiCharacterGenerateEvent -= OnAiCharacterGenerateEvent;
EventBus.AiCharacterGenerateEvent -= OnAiCharacterGenerateEvent;
}
}

View File

@ -16,7 +16,7 @@ public partial class ItemSpawn : Marker2D
{
base._Ready();
EventManager.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
EventBus.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
}
private void MapGenerationCompleteEvent(MapGenerationCompleteEvent mapGenerationCompleteEvent)
@ -38,6 +38,6 @@ public partial class ItemSpawn : Marker2D
public override void _ExitTree()
{
base._ExitTree();
EventManager.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
EventBus.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
}
}

View File

@ -7,7 +7,6 @@ using ColdMint.scripts.map.events;
using ColdMint.scripts.map.interfaces;
using ColdMint.scripts.map.LayoutParsingStrategy;
using ColdMint.scripts.map.layoutStrategy;
using ColdMint.scripts.map.preview;
using ColdMint.scripts.map.room;
using ColdMint.scripts.serialization;
using ColdMint.scripts.utils;
@ -140,7 +139,7 @@ public static class MapGenerator
}
_running = true;
EventManager.MapGenerationStartEvent?.Invoke(new MapGenerationStartEvent());
EventBus.MapGenerationStartEvent?.Invoke(new MapGenerationStartEvent());
if (_layoutStrategy == null || _roomPlacementStrategy == null || _layoutParsingStrategy == null ||
_mapRoot == null)
{
@ -301,12 +300,12 @@ public static class MapGenerator
{
RandomNumberGenerator = randomNumberGenerator
};
EventManager.MapGenerationCompleteEvent?.Invoke(eventObj);
EventBus.MapGenerationCompleteEvent?.Invoke(eventObj);
var aiCharacterGenerateEvent = new AiCharacterGenerateEvent
{
Tag = AiCharacterGenerateEvent.TagMapGenerationComplete
};
EventManager.AiCharacterGenerateEvent?.Invoke(aiCharacterGenerateEvent);
EventBus.AiCharacterGenerateEvent?.Invoke(aiCharacterGenerateEvent);
}
/// <summary>
@ -341,77 +340,15 @@ public static class MapGenerator
return false;
}
var tileMapLayer = roomPlacementData.NewRoom.GetTileMapLayer(Config.TileMapLayerName.Ground);
if (!CreateRoomPreview(tileMapLayer,
CalculateRelativePositionOnTheMinimap(tileMapLayer, roomPlacementData)))
{
LogCat.LogWithFormat("failed_to_create_room_preview", LogCat.LogLabel.Default, LogCat.UploadFormat,
roomNodeDataId);
return false;
}
//Rooms are added to the dictionary only after the preview is created.
//创建预览图后才将房间添加到字典。
dictionary.Add(roomNodeDataId, roomPlacementData.NewRoom);
LogCat.LogWithFormat("room_placement_information", LogCat.LogLabel.Default, LogCat.UploadFormat, roomNodeDataId,
roomPlacementData.Position.ToString());
return true;
}
/// <summary>
/// <para>CalculateRelativePositionOnTheMinimap</para>
/// <para>计算在迷你地图上的相对位置</para>
/// </summary>
/// <returns>
///<para>Returns the position relative to the point in the minimap container</para>
///<para>返回相对对于迷你地图容器中点的位置</para>
/// </returns>
private static Vector2? CalculateRelativePositionOnTheMinimap(TileMapLayer? groundTileMapLayer,
RoomPlacementData roomPlacementData)
{
if (groundTileMapLayer == null || roomPlacementData.Position == null)
var mapGenerationPlaceRoomFinishEvent = new MapGenerationPlaceRoomFinishEvent
{
return null;
}
return roomPlacementData.Position.Value / Config.CellSize * Config.RoomPreviewScale;
}
/// <summary>
/// <para>Create a room preview image.</para>
/// <para>创建房间预览图</para>
/// </summary>
/// <param name="groundTileMapLayer">
///<para>Layers that need to be drawn onto a minimap</para>
///<para>需要绘制到迷你地图上的图层</para>
/// </param>
/// <param name="position">
///<para>Relative to the position of the point in the minimap container</para>
///<para>相对于迷你地图容器中点的位置</para>
/// </param>
/// <returns></returns>
private static bool CreateRoomPreview(TileMapLayer? groundTileMapLayer, Vector2? position)
{
if (GameSceneDepend.MiniMapContainerNode == null || position == null)
{
return false;
}
var image = RoomPreview.CreateImage(groundTileMapLayer);
if (image == null)
{
return false;
}
var sprite = new TextureRect();
sprite.Scale = new Vector2(Config.RoomPreviewScale, Config.RoomPreviewScale);
sprite.Texture = image;
if (GameSceneDepend.MiniMap != null)
{
sprite.Position = GameSceneDepend.MiniMap.MiniMapMidpointCoordinate + position.Value;
}
NodeUtils.CallDeferredAddChild(GameSceneDepend.MiniMapContainerNode, sprite);
RoomNodeDataId = roomNodeDataId,
RoomPlacementData = roomPlacementData
};
EventBus.MapGenerationPlaceRoomFinishEvent?.Invoke(mapGenerationPlaceRoomFinishEvent);
return true;
}
}

View File

@ -19,8 +19,8 @@ public partial class PlayerSpawn : Marker2D
{
base._Ready();
_playerPackedScene = GD.Load<PackedScene>("res://prefab/entitys/Character.tscn");
EventManager.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
EventManager.GameReplayEvent += GameReplayEvent;
EventBus.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
EventBus.GameReplayEvent += GameReplayEvent;
}
private void GameReplayEvent(GameReplayEvent gameReplayEvent)
@ -92,7 +92,7 @@ public partial class PlayerSpawn : Marker2D
public override void _ExitTree()
{
base._ExitTree();
EventManager.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
EventManager.GameReplayEvent -= GameReplayEvent;
EventBus.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
EventBus.GameReplayEvent -= GameReplayEvent;
}
}

View File

@ -0,0 +1,14 @@
using ColdMint.scripts.map.dateBean;
namespace ColdMint.scripts.map.events;
public class MapGenerationPlaceRoomFinishEvent
{
public string? RoomNodeDataId { get; set; }
/// <summary>
/// <para>RoomPlacementData</para>
/// <para>房间的放置数据</para>
/// </summary>
public RoomPlacementData? RoomPlacementData { get; set; }
}

View File

@ -1,3 +1,8 @@
using ColdMint.scripts.debug;
using ColdMint.scripts.map.dateBean;
using ColdMint.scripts.map.events;
using ColdMint.scripts.map.preview;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.map.miniMap;
@ -27,6 +32,87 @@ public partial class MiniMap : NinePatchRect
{
_roomPreviewContainer = GetNode<Node2D>("RoomPreviewContainer");
_miniMapMidpointCoordinate = Size / 2;
EventBus.MapGenerationPlaceRoomFinishEvent += MapGenerationPlaceRoomFinishEvent;
}
/// <summary>
/// <para>After the map generator completes placing the room</para>
/// <para>地图生成器放置房间完成后</para>
/// </summary>
/// <param name="mapGenerationPlaceRoomFinishEvent"></param>
private void MapGenerationPlaceRoomFinishEvent(MapGenerationPlaceRoomFinishEvent mapGenerationPlaceRoomFinishEvent)
{
var roomPlacementData = mapGenerationPlaceRoomFinishEvent.RoomPlacementData;
if (roomPlacementData?.NewRoom == null || mapGenerationPlaceRoomFinishEvent.RoomNodeDataId == null)
{
return;
}
var tileMapLayer = roomPlacementData.NewRoom.GetTileMapLayer(Config.TileMapLayerName.Ground);
if (!CreateRoomPreview(tileMapLayer,
CalculateRelativePositionOnTheMinimap(tileMapLayer, roomPlacementData)))
{
LogCat.LogWithFormat("failed_to_create_room_preview", LogCat.LogLabel.Default, LogCat.UploadFormat,
mapGenerationPlaceRoomFinishEvent.RoomNodeDataId);
}
}
/// <summary>
/// <para>CalculateRelativePositionOnTheMinimap</para>
/// <para>计算在迷你地图上的相对位置</para>
/// </summary>
/// <returns>
///<para>Returns the position relative to the point in the minimap container</para>
///<para>返回相对对于迷你地图容器中点的位置</para>
/// </returns>
private Vector2? CalculateRelativePositionOnTheMinimap(TileMapLayer? groundTileMapLayer,
RoomPlacementData roomPlacementData)
{
if (groundTileMapLayer == null || roomPlacementData.Position == null)
{
return null;
}
return roomPlacementData.Position.Value / Config.CellSize * Config.RoomPreviewScale;
}
/// <summary>
/// <para>Create a room preview image.</para>
/// <para>创建房间预览图</para>
/// </summary>
/// <param name="groundTileMapLayer">
///<para>Layers that need to be drawn onto a minimap</para>
///<para>需要绘制到迷你地图上的图层</para>
/// </param>
/// <param name="position">
///<para>Relative to the position of the point in the minimap container</para>
///<para>相对于迷你地图容器中点的位置</para>
/// </param>
/// <returns></returns>
private bool CreateRoomPreview(TileMapLayer? groundTileMapLayer, Vector2? position)
{
if (GameSceneDepend.MiniMapContainerNode == null || position == null)
{
return false;
}
var image = RoomPreview.CreateImage(groundTileMapLayer);
if (image == null)
{
return false;
}
var sprite = new TextureRect();
sprite.Scale = new Vector2(Config.RoomPreviewScale, Config.RoomPreviewScale);
sprite.Texture = image;
if (GameSceneDepend.MiniMap != null)
{
sprite.Position = GameSceneDepend.MiniMap.MiniMapMidpointCoordinate + position.Value;
}
NodeUtils.CallDeferredAddChild(GameSceneDepend.MiniMapContainerNode, sprite);
return true;
}
public override void _Process(double delta)
@ -41,4 +127,9 @@ public partial class MiniMap : NinePatchRect
_roomPreviewContainer.Position = -OwnerNode.GlobalPosition / Config.CellSize * Config.RoomPreviewScale;
}
}
public override void _ExitTree()
{
EventBus.MapGenerationPlaceRoomFinishEvent -= MapGenerationPlaceRoomFinishEvent;
}
}