六边形寻路格子绘制

原创
07/06 17:43
阅读数 100
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AdventureIsland : MonoBehaviour
{
    static AdventureIsland instante;
    public AdventureGround ground;
    public Transform AdventureGridRoot;

    public int width = 10;
    public int height = 10;
    public string ID;
    public float gridSize = 0.2f;

    public List<AdventureGrid> adventureGrids = new List<AdventureGrid>();
    public static AdventureIsland Instante()
    {
        return instante;
    }
    public void Awake()
    {
        instante = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void RefreshInEditor()
    {
        AdventureGrid[] adventureGrids = GetAdventureGrids();
        foreach(var v in adventureGrids)
        {
            v.RefreshGridInfoInEditor();
        }
    }

    public AdventureGrid[] GetAdventureGrids()
    {
       return AdventureGridRoot.GetComponentsInChildren<AdventureGrid>();
    }

    public Vector3 GetPosByCoordinate(int x, int y)
    {
        float radius = gridSize / 2f;
        float h = radius + radius * Mathf.Sin(Mathf.PI / 6);
        float w = radius * Mathf.Cos(Mathf.PI / 6);
        float left = -width * w;
        float top = height * h / 2f;
        return new Vector3(left + w * x * 2 + y % 2 * w, 0, top - h * y);
    }

    public Point GetCoordinateByPos(Vector3 pos)
    {
        float radius = gridSize / 2f;
        float w = radius * Mathf.Cos(Mathf.PI / 6);
        float h = radius + radius * Mathf.Sin(Mathf.PI / 6);
        float left = -width * w;
        float top = height * h / 2f;

        int j = Mathf.RoundToInt((top - pos.z) / h);
        int i = Mathf.RoundToInt((pos.x - left - j % 2 * w) / (w * 2));
        return new Point { x = i, y = j };
    }
    void OnDrawGizmos()
    {
       
        float radius = gridSize / 2f;
        float y = radius + radius * Mathf.Sin(Mathf.PI / 6);
        float x = radius * Mathf.Cos(Mathf.PI / 6);
        float left = -width * x;
        float top = height * y/2f;

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Vector3 pos = transform.position + GetPosByCoordinate(i, j);// new Vector3(left + i * x * 2 + j % 2 * x, 0, top - j * y);
                Gizmos.color = Color.red;
                Gizmos.DrawSphere(pos, 0.05f);
                for (int k = 0; k < 6; k++)
                {
                    float angle1 = Mathf.PI / 3f * k + Mathf.PI / 6f;
                    float angle2 = Mathf.PI / 3f * ((k + 1) % 6) + Mathf.PI / 6f;
                    Vector3 p1 = pos + new Vector3(radius * Mathf.Cos(angle1), 0, radius  * Mathf.Sin(angle1));
                    Vector3 p2 = pos + new Vector3(radius * Mathf.Cos(angle2), 0, radius  * Mathf.Sin(angle2));
                    Gizmos.color = Color.blue;
                    Gizmos.DrawLine(p1, p2);
                }
            }
        }
    }
}

 

展开阅读全文
cos
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部