备份CatanBuilding瘦身独立工程
This commit is contained in:
72
Assets/Scripts/HexGrid/MapHelper/HexUtils.cs
Normal file
72
Assets/Scripts/HexGrid/MapHelper/HexUtils.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
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<MeshFilter>(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<Vector2Int> GetNeighborList(HexTile tile)
|
||||
{
|
||||
var list = new List<Vector2Int>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/HexGrid/MapHelper/HexUtils.cs.meta
Normal file
3
Assets/Scripts/HexGrid/MapHelper/HexUtils.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd82ff490dfb40e08b4cb74012fcfd72
|
||||
timeCreated: 1757937441
|
||||
202
Assets/Scripts/HexGrid/MapHelper/MapRoot.cs
Normal file
202
Assets/Scripts/HexGrid/MapHelper/MapRoot.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using System;
|
||||
using HexGrid.Math;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace HexGrid.MapHelper
|
||||
{
|
||||
|
||||
public enum GridType
|
||||
{
|
||||
PointyTop,
|
||||
|
||||
// 当前仅支持尖顶
|
||||
// FlatTop,
|
||||
}
|
||||
public class MapRoot : MonoBehaviour
|
||||
{
|
||||
//
|
||||
[SerializeField] private GridType gridType;
|
||||
// 格子的大小
|
||||
[SerializeField] private float gridSize = 1.52f;
|
||||
//
|
||||
[SerializeField] private GameObject standGridPrefab;
|
||||
|
||||
private GameObject go;
|
||||
private void Awake()
|
||||
{
|
||||
if (standGridPrefab)
|
||||
{
|
||||
go = Instantiate(standGridPrefab, Vector3.zero, Quaternion.identity, this.transform);
|
||||
go.name = "grid_00";
|
||||
go.SetActive(false);
|
||||
var size = DetermineTileSize(go, out var bounds);
|
||||
gridSize = size.z * 0.5f;
|
||||
}
|
||||
// HexMath.SetHexMapOrigin( new Vector3(1.32f, 0.00f, -2.28f));
|
||||
HexMath.SetHexMapOrigin(Vector3.zero);
|
||||
HexMath.SetGridSize(gridSize);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// var worldPos0 = HexMath.AxialToPixel(0, 0, gridSize);
|
||||
// var worldPos1 = HexMath.AxialToPixel(0, 1, gridSize);
|
||||
// Log($"worldPos0 -> {worldPos0} ,worldPos1 ->{worldPos1}");
|
||||
//
|
||||
// //
|
||||
// DrawHex(worldPos0,gridSize);
|
||||
// DrawHex(worldPos1,gridSize);
|
||||
// for (int i = -5; i < 5; i++)
|
||||
// {
|
||||
// for (int j = -5; j < 5; j++)
|
||||
// {
|
||||
// Log($"Grid-({i},{j}): {HexMath.AxialToPixel(i,j)}");
|
||||
// }
|
||||
// }
|
||||
// Log($"World -> -1.29904,0,2.25 -> {HexMath.PixelToAxial(new Vector3(-1.29904f,0,2.25f))}");
|
||||
// Log($"World -> 0,0,0 -> {HexMath.PixelToAxial(Vector3.zero)}");
|
||||
// Log($"(1,1) -> World -> 3.95, 0.00, 2.28 -> {HexMath.PixelToAxial(new Vector3(3.95f, 0.00f, 2.28f))}");
|
||||
// Log($"(1,-1)-> World -> 1.32, 0.00, -2.28 -> {HexMath.PixelToAxial(new Vector3(1.32f, 0.00f, -2.28f))}");
|
||||
// Log($"(-1,1)-> World -> -1.32, 0.00, 2.28 -> {HexMath.PixelToAxial(new Vector3(-1.32f, 0.00f, 2.28f))}");
|
||||
// Log($"(-1,-1)-> World ->-3.95, 0.00, -2.28 -> {HexMath.PixelToAxial(new Vector3(-3.95f, 0.00f, -2.28f))}");
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// var worldPos0 = HexMath.AxialToPixel(0, 0 );
|
||||
// var worldPos1 = HexMath.AxialToPixel(0, 1);
|
||||
// var worldPos2 = HexMath.AxialToPixel(0, 2);
|
||||
|
||||
//
|
||||
// var worldPos10 = HexMath.AxialToPixel(1, 0, gridSize);
|
||||
// var worldPos20 = HexMath.AxialToPixel(2, 0, gridSize);
|
||||
// var worldPos30 = HexMath.AxialToPixel(3, 0, gridSize);
|
||||
//
|
||||
// var worldPos11 = HexMath.AxialToPixel(1, 1, gridSize);
|
||||
// var worldPos21 = HexMath.AxialToPixel(2, 1, gridSize);
|
||||
// var worldPos31 = HexMath.AxialToPixel(3, 1, gridSize);
|
||||
//
|
||||
// Log(HexMath.PixelToAxial(worldPos0,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos1,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos2,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos10,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos20,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos30,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos11,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos21,gridSize));
|
||||
// Log(HexMath.PixelToAxial(worldPos31,gridSize));
|
||||
//
|
||||
//
|
||||
//
|
||||
// DrawHex(worldPos0,gridSize);
|
||||
// DrawHex(worldPos1,gridSize);
|
||||
// DrawHex(worldPos2,gridSize);
|
||||
//
|
||||
// DrawHex(worldPos10,gridSize,Color.green);
|
||||
// DrawHex(worldPos20,gridSize,Color.green);
|
||||
// DrawHex(worldPos30,gridSize,Color.green);
|
||||
//
|
||||
// DrawHex(worldPos11,gridSize,Color.yellow);
|
||||
// DrawHex(worldPos21,gridSize,Color.yellow);
|
||||
// DrawHex(worldPos31,gridSize,Color.yellow);
|
||||
//
|
||||
// DrawHex(HexMath.AxialToPixel(-1,1,gridSize),gridSize,Color.blue);
|
||||
// DrawHex(HexMath.AxialToPixel(-2,1,gridSize),gridSize,Color.blue);
|
||||
// DrawHex(HexMath.AxialToPixel(-3,1,gridSize),gridSize,Color.blue);
|
||||
// DrawHex(HexMath.AxialToPixel(-4,1,gridSize),gridSize,Color.blue);
|
||||
// DrawHex(HexMath.AxialToPixel(-5,1,gridSize),gridSize,Color.blue);
|
||||
//
|
||||
|
||||
}
|
||||
|
||||
private Vector3 DetermineTileSize(GameObject tileObject,out Bounds bounds)
|
||||
{
|
||||
var meshFilter = tileObject.GetComponentInChildren<MeshFilter>(true);
|
||||
bounds = meshFilter.sharedMesh.bounds;
|
||||
Log($"DetermineTileSize -> {bounds} {bounds.size}");
|
||||
// return new Vector3((bounds.extents.x * 2) * 0.75f,0, bounds.extents.y * 2);
|
||||
return new Vector3(bounds.size.x, 0,bounds.size.y);
|
||||
|
||||
// return bounds;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
// GameObject[] selectedObjects = Selection.gameObjects;
|
||||
// var tile = GameObject.Find("grid_00");
|
||||
// var size = DetermineTileSize(tile,out var bounds);
|
||||
// Gizmos.color = Color.yellow;
|
||||
// Gizmos.DrawWireCube(Vector3.zero, bounds.size * 2);
|
||||
// Gizmos.DrawWireCube(Vector3.zero, size);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// if (Application.isPlaying && Application.isEditor)
|
||||
// {
|
||||
// Vector3 origin = Vector3.zero;
|
||||
//
|
||||
// if (go)
|
||||
// {
|
||||
// var size = DetermineTileSize(go, out var bounds);
|
||||
// Gizmos.color = Color.yellow;
|
||||
// Gizmos.DrawWireCube(origin, size);
|
||||
// gridSize = size.z * 0.5f;
|
||||
// Gizmos.color = Color.magenta;
|
||||
// DrawHex(origin, gridSize);
|
||||
// }
|
||||
// }
|
||||
|
||||
// Gizmos.color = Color.green;
|
||||
// Gizmos.DrawWireCube(Vector3.zero, size);
|
||||
}
|
||||
#endif
|
||||
// HexGrid
|
||||
private void DrawGrid(int q,int r)
|
||||
{
|
||||
// 00,01
|
||||
// Vector3 gridPos = HexMath.AxialToPixel(q, r, gridSize);
|
||||
}
|
||||
|
||||
private void DrawHex(Vector3 center, float size,Color color = default)
|
||||
{
|
||||
color = color == default ? Color.magenta : color;
|
||||
|
||||
var angleDelta = gridType == GridType.PointyTop ? 30 : 0;
|
||||
var corners = new Vector3[7];
|
||||
for (var i = 0; i < 6; i++)
|
||||
{
|
||||
float angleDeg = 60 * i + angleDelta;
|
||||
var angleRad = Mathf.Deg2Rad * angleDeg;
|
||||
corners[i] = new Vector3(
|
||||
center.x + size * Mathf.Cos(angleRad),
|
||||
0,
|
||||
center.z + size * Mathf.Sin(angleRad)
|
||||
);
|
||||
}
|
||||
corners[6] = corners[0];
|
||||
for (var i = 0; i < 6; i++)
|
||||
{
|
||||
// Gizmos.DrawLine(corners[i], corners[i + 1]);
|
||||
Debug.DrawLine(corners[i],corners[i + 1],color);
|
||||
}
|
||||
}
|
||||
// 画格子
|
||||
private void DrawHexGrid(Vector3 origin)
|
||||
{
|
||||
Log("DrawHexGrid-> {}");
|
||||
}
|
||||
// MapRoot
|
||||
private static void Log(object t)
|
||||
{
|
||||
Debug.Log($"<color=yellow> MapRoot -> {t} </color>");
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/HexGrid/MapHelper/MapRoot.cs.meta
Normal file
3
Assets/Scripts/HexGrid/MapHelper/MapRoot.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c66c391ffdb4fdb8340fcdcffcb0b47
|
||||
timeCreated: 1757907477
|
||||
Reference in New Issue
Block a user