Files
2026-05-26 16:15:54 +08:00

202 lines
7.6 KiB
C#

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>");
}
}
}