Exemplo n.º 1
0
        /* Constructor */
        public HexCell(HexGrid grid, int index, Vector3 center, HexCoordinate coordinate, Color color)
        {
            m_grid          = grid;
            m_index         = index;
            m_center        = center;
            m_hexCoordinate = coordinate;
            m_color         = color;
            m_neighbors     = new int[6];   // every hex has 6 neighbors    // TODO only assign as many neighbors as actually exist [?] (corners and margins)

            for (int i = 0; i < m_neighbors.Length; i++)
            {
                m_neighbors[i] = -1;
            }
        }
        /* Generate Cell */
        private void GenerateCell(int x, int z, int index)
        {
            Vector3 center = new Vector3(
                (x + z * 0.5f - z / 2) * (HexMeshUtility.InnerRadius * 2f),          // Applied z * 0.5 - z/2 to account for row displacement (z * 0.5 - )   (z / 2 - every second row, cells move one additional step)
                0,
                z * (HexMeshUtility.OuterRadius * 1.5f)
                );

            m_cells[index] = new HexCell(this, index, center, HexCoordinate.FromOffsetCoordinates(x, z), defaultColor);

            // Set connections to the Left and Right
            if (x > 0)
            {
                m_cells[index].SetNeighbor(HexDirection.Left, ref m_cells[index - 1]);
            }


            if (z > 0)
            {
                // Even rows
                if ((z & 1) == 0)      // bitwise and 1 => mask everything but last digit. if 0 => even
                {
                    // Set connections BottomRight and TopLeft
                    m_cells[index].SetNeighbor(HexDirection.BottomRight, ref m_cells[index - m_cellCountX]);

                    // Set connections BottomLeft and TopRight
                    if (x > 0)
                    {
                        m_cells[index].SetNeighbor(HexDirection.BottomLeft, ref m_cells[index - m_cellCountX - 1]);
                    }
                }

                // Odd rows
                else
                {
                    // Set connections BottomLeft and TopRight
                    m_cells[index].SetNeighbor(HexDirection.BottomLeft, ref m_cells[index - m_cellCountX]);

                    // Set connections BottomRight and TopLeft
                    if (x < m_cellCountX - 1)
                    {
                        m_cells[index].SetNeighbor(HexDirection.BottomRight, ref m_cells[index - m_cellCountX + 1]);
                    }
                }
            }

            AddCellToChunk(x, z, ref m_cells[index]);
        }
        /* Handle Input */
        private HexCell HandleInput()
        {
            Ray        inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(inputRay, out hit))
            {
                Vector3       position   = transform.InverseTransformPoint(hit.point);
                HexCoordinate coordinate = HexCoordinate.FromPosition(position, HexMeshUtility.InnerRadius, HexMeshUtility.OuterRadius);
                int           index      = coordinate.X + coordinate.Z * m_cellCountX + coordinate.Z / 2;
                if (index < m_cells.Length)
                {
                    return(m_cells[index]);
                }
            }

            return(null);
        }
 public int DistanceTo(HexCoordinate other)
 {
     return(Mathf.Abs(x - other.x) + Mathf.Abs(Y - other.Y) + Mathf.Abs(z - other.z) / 2);
 }