六边形网格笔记

落花浮王杯 提交于 2020-03-11 16:06:44
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hexagonal : MonoBehaviour
{
    public GameObject prefab;
    public Transform root;

    private const float size = 0.59f;

    private Offset offset_x, offset_y, offset_z;

    private List<Cell> cells = new List<Cell>();

    // Start is called before the first frame update
    void Start()
    {
        offset_x = new Offset(size * Mathf.Cos(Mathf.PI / 6), size / 2);
        offset_y = new Offset(-size * Mathf.Cos(Mathf.PI / 6), size / 2);
        offset_z = new Offset(0, -size);

        Cell cell_o = new Cell(0, 0, 0);
        cells = GetCellsByStep(cell_o, 2, true);
        foreach (Cell cell in cells)
        {
            GenerateCell(cell);
        }
    }

    public void GenerateCell(Cell cell)
    {
        if(cell.x + cell.y + cell.z != 0)
        {
            Debug.Log("数据出错");
            return;
        }
        GameObject p = Instantiate(prefab);
        p.transform.SetParent(root);
        var x = cell.x * offset_x.X + cell.y * offset_y.X + cell.z * offset_z.X;
        var y = cell.x * offset_x.Y + cell.y * offset_y.Y + cell.z * offset_z.Y;
        p.transform.localPosition = new Vector3(x, y, 0);
        p.SetActive(true);
    }

    public List<Cell> GetCellsByStep(Cell cell, int step, bool isRim = false)
    {
        List<Cell> ret = new List<Cell>();
        if(step == 0)
        {
            ret.Add(cell);
        }
        else
        {
            var max_x = cell.x + step;
            var max_y = cell.y + step;
            var max_z = cell.z + step;
            var min_x = cell.x - step;
            var min_y = cell.y - step;
            var min_z = cell.z - step;
            for (int x = min_x; x <= max_x; x ++)
            {
                for (int y = min_y; y <= max_y; y++)
                {
                    for (int z = min_z; z <= max_z; z++)
                    {
                        if (x + y + z == 0)
                        {
                            if(isRim)
                            {
                                if (x == max_x || x == min_x || y == max_y || y == min_y || z == max_x || z == min_z)
                                    ret.Add(new Cell(x, y, z));
                            }
                            else
                            {
                                ret.Add(new Cell(x, y, z));
                            }
                        }
                    }
                }
            }
        }
        return ret;
    }

    //尖顶六边形顶点
    public Point PointyHexCorner(Point center, float size, int i)
    {
        var angle_deg = 60 * i - 30;
        var angle_rad = Mathf.PI / 180 * angle_deg;
        return new Point(center.X+ size * Mathf.Cos(angle_rad), center.Y + size * Mathf.Sin(angle_rad));
    }

    //平顶六边形顶点
    public Point FlatHexCorner(Point center, float size, int i)
    {
        var angle_deg = 60 * i;
        var angle_rad = Mathf.PI / 180 * angle_deg;
        return new Point(center.X + size * Mathf.Cos(angle_rad), center.Y + size * Mathf.Sin(angle_rad));
    }
}

public class Point
{
    private float m_fx;
    private float m_fy;

    public float X
    {
        get
        {
            return m_fx;
        }
    }

    public float Y
    {
        get
        {
            return m_fy;
        }
    }

    public Point(float x, float y)
    {
        m_fx = x;
        m_fy = y;
    }
}

public class Offset
{
    private float m_fx;
    private float m_fy;

    public float X
    {
        get
        {
            return m_fx;
        }
    }

    public float Y
    {
        get
        {
            return m_fy;
        }
    }

    public Offset(float x, float y)
    {
        m_fx = x;
        m_fy = y;
    }
}

public class Cell
{
    public int x, y, z;
    public Cell(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public new string ToString()
    {
        return ("x:" + x + " y:" + y + " z:" + z);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!