From 1ef0f08e2faa59a1724ea43a4e61dba913598a2b Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Tue, 27 Aug 2024 23:06:33 +0800 Subject: [PATCH] =?UTF-8?q?Create=20a=20map=20preview=20image.=20=E5=88=B6?= =?UTF-8?q?=E4=BD=9C=E5=9C=B0=E5=9B=BE=E9=A2=84=E8=A7=88=E5=9B=BE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/map/MapGenerator.cs | 13 ++++++ scripts/map/preview/RoomPreview.cs | 70 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 scripts/map/preview/RoomPreview.cs diff --git a/scripts/map/MapGenerator.cs b/scripts/map/MapGenerator.cs index de246b0..c20750f 100644 --- a/scripts/map/MapGenerator.cs +++ b/scripts/map/MapGenerator.cs @@ -7,6 +7,7 @@ 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; @@ -338,6 +339,18 @@ public static class MapGenerator dictionary.Add(roomNodeDataId, roomPlacementData.NewRoom); LogCat.LogWithFormat("room_placement_information", LogCat.LogLabel.Default, LogCat.UploadFormat, roomNodeDataId, 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; } } \ No newline at end of file diff --git a/scripts/map/preview/RoomPreview.cs b/scripts/map/preview/RoomPreview.cs new file mode 100644 index 0000000..70fbcdc --- /dev/null +++ b/scripts/map/preview/RoomPreview.cs @@ -0,0 +1,70 @@ +namespace ColdMint.scripts.map.preview; + +using Godot; + +public static class RoomPreview +{ + /// + /// Create Image + /// 创建图像 + /// + /// + /// + 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); + } +} \ No newline at end of file