From 526e8850e463f44d308b85aacd621542f07608e1 Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Thu, 17 Jul 2025 19:14:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E5=AE=8C=E6=88=90=E7=9A=84?= =?UTF-8?q?=E7=93=A6=E7=89=87=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Managers/PackagesImageManager.cs | 2 +- Client/Assets/Scripts/Map/DoubleMap.cs | 95 +++++++++++++++++-- Client/Data/Core/Define/Map/Map.xml | 6 +- 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/Client/Assets/Scripts/Managers/PackagesImageManager.cs b/Client/Assets/Scripts/Managers/PackagesImageManager.cs index b645994..a0d019b 100644 --- a/Client/Assets/Scripts/Managers/PackagesImageManager.cs +++ b/Client/Assets/Scripts/Managers/PackagesImageManager.cs @@ -61,7 +61,7 @@ namespace Managers // 创建Sprite var sprite = Sprite.Create(texture, (Rect)spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit); - var index = row * cols + col; + var index = (rows - row - 1) * cols + col; sprites[name + $"_{index}"] = sprite; } } diff --git a/Client/Assets/Scripts/Map/DoubleMap.cs b/Client/Assets/Scripts/Map/DoubleMap.cs index 093e6da..b6471e6 100644 --- a/Client/Assets/Scripts/Map/DoubleMap.cs +++ b/Client/Assets/Scripts/Map/DoubleMap.cs @@ -16,27 +16,98 @@ namespace Map void Start() { TileManager.Instance.Init(); + var mapSize = 100; + float noiseScale = 0.1f; + + for (int x = 0; x < mapSize; x++) + { + List col = new(); + for (int y = 0; y < mapSize; y++) + { + // 计算柏林噪声值 + float noiseValue = Mathf.PerlinNoise(x * noiseScale, y * noiseScale); + if (noiseValue < 0.5f) // 小于 0.5 表示 Dirt + { + col.Add(TileManager.Instance.tileID.GetValueOrDefault("Dirt")); + } + else // 大于等于 0.5 表示 Grass + { + col.Add(TileManager.Instance.tileID.GetValueOrDefault("Grass")); + } + + } + mapData.Add(col); + UpdateTexture(); + } + + for (int x = 0; x < 16; x++) + { + textureLevel.SetTile(new(x,-1),TileManager.Instance.tileBaseMapping.GetValueOrDefault($"GrassDirt_{x}")); + } } public void UpdateTexture() { - + for (int i = 0; i < mapData.Count; i++) + { + for (int j = 0; j < mapData[i].Count; j++) + { + UpdateTexture(i, j); + } + } + } + + public int GetTile(int x, int y) + { + if (x < 0 || x >= mapData.Count) + { + return 0; + } + + var col = mapData[x]; + if (y < 0 || y >= mapData.Count) + { + return 0; + } + + return col[y]; } public void SetTile(int x, int y, string tileName) { - + SetTile(x,y,TileManager.Instance.tileID.GetValueOrDefault(tileName)); } public void SetTile(int x, int y, int id) { - + mapData[x][y] = id; + UpdateTexture(x, y); + UpdateTexture(x, y-1); + UpdateTexture(x-1, y); + UpdateTexture(x-1, y-1); + } + /// + /// 更新对应坐标的贴图 + /// + /// + /// + /// + public void UpdateTexture(int x, int y) + { + var lt = GetTile(x, y + 1); + var rt = GetTile(x + 1, y + 1); + var lb = GetTile(x, y); + var rb = GetTile(x + 1, y); + if (TileManager.Instance.tileToTileBaseMapping.ContainsKey((lt, rt, lb, rb))) + { + textureLevel.SetTile(new(x,y),TileManager.Instance.tileToTileBaseMapping[(lt, rt, lb, rb)]); + } } - } public class TileManager:Utils.Singleton { + public Dictionary tileBaseMapping = new(); public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new(); public Dictionary tileID = new(); public void Init() @@ -46,7 +117,7 @@ namespace Map Managers.PackagesImageManager.Instance.Init(); var imagePack = Managers.PackagesImageManager.Instance; var tileType = Managers.DefineManager.Instance.QueryDefinesByType(); - for (var i = 1; i < tileType.Length; i++) + for (var i = 0; i < tileType.Length; i++) { tileID.Add(tileType[i].name, i); } @@ -57,7 +128,7 @@ namespace Map foreach (var keyVal in mappingTableDef.tileDict) { var key = keyVal.Key; - var val=keyVal.Value; + var val = keyVal.Value; var parts = key.Split('_'); if (parts.Length != 4) { @@ -67,14 +138,15 @@ namespace Map } if (!(tileID.TryGetValue(parts[0], out var k1) && - tileID.TryGetValue(parts[1], out var k2) && - tileID.TryGetValue(parts[2], out var k3) && - tileID.TryGetValue(parts[3], out var k4))) + tileID.TryGetValue(parts[1], out var k2) && + tileID.TryGetValue(parts[2], out var k3) && + tileID.TryGetValue(parts[3], out var k4))) { var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef); Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}中存在未定义的瓦片名称"); continue; } + var sprite = imagePack.GetSprite(val); if (sprite == null) { @@ -82,14 +154,17 @@ namespace Map Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{val}中存在未定义的图片名称"); continue; } + if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4))) { var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef); Debug.LogWarning($"来自{packName}定义的TileMappingTableDef键值{(k1, k2, k3, k4)}存在重复索引,将忽略重复项"); continue; } + var tile = LoadTile(sprite); - tileToTileBaseMapping.Add((k1, k2, k3, k4), tile); + tileToTileBaseMapping[(k1, k2, k3, k4)] = tile; + tileBaseMapping[val] = tile; } } } diff --git a/Client/Data/Core/Define/Map/Map.xml b/Client/Data/Core/Define/Map/Map.xml index a9778d4..48aa780 100644 --- a/Client/Data/Core/Define/Map/Map.xml +++ b/Client/Data/Core/Define/Map/Map.xml @@ -28,17 +28,17 @@ - + - + - +