コード例 #1
0
        /// <summary>
        /// 坐标转换成网格坐标
        /// </summary>
        /// <param name="position"></param>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static HexagonCoordinates FromPosition(Vector3 position, HexagonGrid grid)
        {
            float x      = position.x / (grid.CellInnerRadius * 2f);
            float y      = -x;
            float offset = position.z / (grid.CellOuterRadius * 3f);

            x -= offset;
            y -= offset;
            int iX = Mathf.RoundToInt(x);
            int iY = Mathf.RoundToInt(y);
            int iZ = Mathf.RoundToInt(-x - y);

            if (iX + iY + iZ != 0)
            {
                Debug.LogWarning("rounding error!");
                float dX = Mathf.Abs(x - iX);
                float dY = Mathf.Abs(y - iY);
                float dZ = Mathf.Abs(-x - y - iZ);
                if (dX > dY && dX > dZ)
                {
                    iX = -iY - iZ;
                }
                else if (dZ > dY)
                {
                    iZ = -iX - iY;
                }
            }
            return(new HexagonCoordinates(iX, iZ));
        }
コード例 #2
0
        public void Initialize(HexagonGrid grid)
        {
            this.grid = grid;

            // 初始化六个定点
            this.corners [0] = new Vector3(0f, 0f, grid.CellOuterRadius) + this.transform.localPosition;
            this.corners [1] = new Vector3(grid.CellInnerRadius, 0f, 0.5f * grid.CellOuterRadius) + this.transform.localPosition;
            this.corners [2] = new Vector3(grid.CellInnerRadius, 0f, -0.5f * grid.CellOuterRadius) + this.transform.localPosition;
            this.corners [3] = new Vector3(0f, 0f, -grid.CellOuterRadius) + this.transform.localPosition;
            this.corners [4] = new Vector3(-grid.CellInnerRadius, 0f, -0.5f * grid.CellOuterRadius) + this.transform.localPosition;
            this.corners [5] = new Vector3(-grid.CellInnerRadius, 0f, 0.5f * grid.CellOuterRadius) + this.transform.localPosition;
            this.corners [6] = new Vector3(0f, 0f, grid.CellOuterRadius) + this.transform.localPosition;
        }