using System.Collections.Generic; using UnityEngine; namespace HexGrid.MapHelper { public static class HexUtils { public static readonly Vector2Int[] neighbors = { new Vector2Int(1, 0), new Vector2Int(+1, 0), new Vector2Int(+1, -1), new Vector2Int(0, -1), new Vector2Int(-1, 0), new Vector2Int(-1, +1), new Vector2Int(0, +1) }; public static Vector3 DetermineTileSize(GameObject tileObject,out Bounds bounds) { var meshFilter = tileObject.GetComponentInChildren(true); bounds = meshFilter.sharedMesh.bounds; return new Vector3(bounds.size.x, 0,bounds.size.y); } // 构建地块Id public static string MakeTileId(Vector2Int tileCoords) { var x = tileCoords.x; var y = tileCoords.y; return $"tile:({x},{y})"; } // 通过id,解析地块 public static Vector2Int ParseTileId(string tileString) { const string prefix = "tile:"; if (string.IsNullOrEmpty(tileString)) return Vector2Int.zero; // 检查是否以 "tile:" 开头 if (!tileString.StartsWith(prefix)) return Vector2Int.zero; int startIndex = prefix.Length; int endIndex = tileString.Length - 1; if (endIndex <= startIndex) return Vector2Int.zero; var coordsPart = tileString.Substring(startIndex,endIndex); var coords = coordsPart.Split(','); if (coords.Length != 2) return Vector2Int.zero; // 尝试解析 x 和 y 坐标 if (int.TryParse(coords[0].Trim(), out var x) && int.TryParse(coords[1].Trim(), out var y)) { return new Vector2Int(x, y); } return Vector2Int.zero; } // 获得当前模块的邻接Tile public static List GetNeighborList(HexTile tile) { var list = new List(); foreach (var neighbor in neighbors) { var x = tile.tileCoords.x + neighbor.x; var y = tile.tileCoords.y + neighbor.y; list.Add(new Vector2Int(x,y)); } return list; } } }