Create a map preview image.

制作地图预览图。
This commit is contained in:
Cold-Mint 2024-08-27 23:06:33 +08:00
parent 5b0803f85f
commit 1ef0f08e2f
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
2 changed files with 83 additions and 0 deletions

View File

@ -7,6 +7,7 @@ using ColdMint.scripts.map.events;
using ColdMint.scripts.map.interfaces; using ColdMint.scripts.map.interfaces;
using ColdMint.scripts.map.LayoutParsingStrategy; using ColdMint.scripts.map.LayoutParsingStrategy;
using ColdMint.scripts.map.layoutStrategy; using ColdMint.scripts.map.layoutStrategy;
using ColdMint.scripts.map.preview;
using ColdMint.scripts.map.room; using ColdMint.scripts.map.room;
using ColdMint.scripts.serialization; using ColdMint.scripts.serialization;
using ColdMint.scripts.utils; using ColdMint.scripts.utils;
@ -338,6 +339,18 @@ public static class MapGenerator
dictionary.Add(roomNodeDataId, roomPlacementData.NewRoom); dictionary.Add(roomNodeDataId, roomPlacementData.NewRoom);
LogCat.LogWithFormat("room_placement_information", LogCat.LogLabel.Default, LogCat.UploadFormat, roomNodeDataId, LogCat.LogWithFormat("room_placement_information", LogCat.LogLabel.Default, LogCat.UploadFormat, roomNodeDataId,
roomPlacementData.Position.ToString()); roomPlacementData.Position.ToString());
//Create a room preview image.
//创建房间预览图。
var rootNode = roomPlacementData.NewRoom.RootNode;
var image = RoomPreview.CreateImage(roomPlacementData.NewRoom.GetTileMapLayer(Config.TileMapLayerName.Ground));
if (rootNode != null && image != null)
{
var sprite = new Sprite2D();
sprite.Scale = new Vector2(10, 10);
sprite.Texture = image;
NodeUtils.CallDeferredAddChild(rootNode, sprite);
}
return true; return true;
} }
} }

View File

@ -0,0 +1,70 @@
namespace ColdMint.scripts.map.preview;
using Godot;
public static class RoomPreview
{
/// <summary>
/// <para>Create Image</para>
/// <para>创建图像</para>
/// </summary>
/// <param name="groundTileMapLayer"></param>
/// <returns></returns>
public static ImageTexture? CreateImage(TileMapLayer? groundTileMapLayer)
{
if (groundTileMapLayer == null)
{
return null;
}
//Calculate the size of the image.
//计算图像的尺寸。
//The width is maxX minX and the height is maxY minY.
//宽度为maxX-minX高度为maxY-minY。
var maxY = int.MinValue;
var maxX = int.MinValue;
var minY = int.MaxValue;
var minX = int.MaxValue;
var cellsArray = groundTileMapLayer.GetUsedCells();
foreach (var vector2I in cellsArray)
{
if (vector2I.Y > maxY)
{
maxY = vector2I.Y;
}
if (vector2I.Y < minY)
{
minY = vector2I.Y;
}
if (vector2I.X > maxX)
{
maxX = vector2I.X;
}
if (vector2I.X < minX)
{
minX = vector2I.X;
}
}
var height = maxY - minY;
var width = maxX - minX;
var offsetVector2 = Vector2I.Zero - new Vector2I(minX, minY);
//Create an image.
//创建image。
var image = Image.CreateEmpty(width + 1, height + 1, false, Image.Format.Rgba8);
//image.Fill(Colors.Green);
//Fill in pixels
//填充像素点
foreach (var vector2I in cellsArray)
{
image.SetPixel(vector2I.X + offsetVector2.X, vector2I.Y + offsetVector2.Y, new Color(100, 221, 23));
}
//Create texture.
//创建texture
return ImageTexture.CreateFromImage(image);
}
}