2025-02-11 18:00:25 +08:00
|
|
|
|
using System;
|
2025-02-10 17:55:21 +08:00
|
|
|
|
using UnityEngine;
|
2025-02-11 18:00:25 +08:00
|
|
|
|
using Random = UnityEngine.Random;
|
2025-02-10 17:55:21 +08:00
|
|
|
|
|
|
|
|
|
public class HexGrid : MonoBehaviour
|
|
|
|
|
{
|
2025-02-12 13:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 格子宽度
|
|
|
|
|
/// </summary>
|
2025-02-10 17:55:21 +08:00
|
|
|
|
public int width = 6;
|
2025-02-12 13:55:41 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 格子高度
|
|
|
|
|
/// </summary>
|
2025-02-10 17:55:21 +08:00
|
|
|
|
public int height = 6;
|
|
|
|
|
|
2025-02-12 13:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 格子实例预制体(用于实际角色放入交互,也有可能会删掉改用判断位置的方式处理交互)
|
|
|
|
|
/// </summary>
|
2025-02-10 17:55:21 +08:00
|
|
|
|
public HexCell cellPrefab;
|
2025-02-12 13:55:41 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 六边形渲染mesh
|
|
|
|
|
/// </summary>
|
2025-02-11 18:00:25 +08:00
|
|
|
|
HexMesh hexMesh;
|
2025-02-12 13:55:41 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 格子存储
|
|
|
|
|
/// </summary>
|
2025-02-10 17:55:21 +08:00
|
|
|
|
HexCell[] cells;
|
2025-02-11 18:00:25 +08:00
|
|
|
|
|
|
|
|
|
public Color defaultColor = Color.white;
|
|
|
|
|
public Color touchedColor = Color.magenta;
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
hexMesh = GetComponentInChildren<HexMesh>();
|
2025-02-10 17:55:21 +08:00
|
|
|
|
cells = new HexCell[height * width];
|
|
|
|
|
|
2025-02-11 18:00:25 +08:00
|
|
|
|
for (int z = 0, i = 0; z < height; z++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < width; x++)
|
|
|
|
|
{
|
2025-02-10 17:55:21 +08:00
|
|
|
|
CreateCell(x, z, i++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-11 18:00:25 +08:00
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2025-02-12 13:55:41 +08:00
|
|
|
|
// 渲染mesh
|
2025-02-11 18:00:25 +08:00
|
|
|
|
hexMesh.Triangulate(cells);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 13:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建格子以及实例
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x"></param>
|
|
|
|
|
/// <param name="z"></param>
|
|
|
|
|
/// <param name="i"></param>
|
2025-02-11 18:00:25 +08:00
|
|
|
|
void CreateCell(int x, int z, int i)
|
|
|
|
|
{
|
2025-02-10 17:55:21 +08:00
|
|
|
|
Vector3 position;
|
2025-02-11 18:00:25 +08:00
|
|
|
|
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
|
2025-02-10 17:55:21 +08:00
|
|
|
|
position.y = 0f;
|
2025-02-12 13:55:41 +08:00
|
|
|
|
position.z = HexMetrics.offset * z;
|
2025-02-10 17:55:21 +08:00
|
|
|
|
|
|
|
|
|
HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
|
|
|
|
|
cell.transform.SetParent(transform, false);
|
|
|
|
|
cell.transform.localPosition = position;
|
2025-02-11 18:00:25 +08:00
|
|
|
|
cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z);
|
2025-02-12 13:55:41 +08:00
|
|
|
|
cell.color = new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f), 1);
|
2025-02-11 18:00:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
|
|
|
{
|
2025-02-12 13:55:41 +08:00
|
|
|
|
HandleInput();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 射线检测拿到射线点,然后传递给TouchCell进行实际位置判断
|
|
|
|
|
/// </summary>
|
|
|
|
|
void HandleInput()
|
|
|
|
|
{
|
|
|
|
|
Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
|
|
|
RaycastHit hit;
|
|
|
|
|
if (Physics.Raycast(inputRay, out hit))
|
|
|
|
|
{
|
|
|
|
|
TouchCell(hit.point);
|
2025-02-11 18:00:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 13:55:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断点击的哪个格子
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
2025-02-11 18:00:25 +08:00
|
|
|
|
public void TouchCell(Vector3 position)
|
|
|
|
|
{
|
2025-02-12 13:55:41 +08:00
|
|
|
|
// 将 position 从世界空间变换到本地空间
|
2025-02-11 18:00:25 +08:00
|
|
|
|
position = transform.InverseTransformPoint(position);
|
|
|
|
|
HexCoordinates coordinates = HexCoordinates.FromPosition(position);
|
|
|
|
|
int index = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
|
|
|
|
|
HexCell cell = cells[index];
|
|
|
|
|
cell.color = touchedColor;
|
|
|
|
|
hexMesh.Triangulate(cells);
|
2025-02-10 17:55:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|