示例#1
0
    void CreateCell(int x, int z, int i)
    {
        Vector3 position;

        position.x = (x + z * 0.5f - z / 2) * (HexMatrics.innerRadius * 2);
        position.y = 0;
        position.z = z * (HexMatrics.outerRadius * 1.5f);

        // Create hex cells
        HexCell cell = cells[i] = Instantiate(cellPrfab);

        cell.transform.SetParent(transform, false);
        cell.transform.localPosition = position;
        cell.coords = HexCoords.FromOffsetCoords(x, z);

        cell.color = defaultColor;

        if (x > 0)
        {
            cell.SetNeighbor(HexDirection.W, cells[i - 1]);
        }
        if (z > 0)
        {
            // this is a bitwise opertator. It's the sam as &&, but for each bit.
            // both bits of a pair need to be 1 for the result to be 1.
            // For example, 10101010 & 00001111 yields 00001010.

            // This one checks if the numbers are even. To check for odd numbers, change the 0 to 1
            if ((z & 1) == 0)
            {
                // Connect the SE cells
                cell.SetNeighbor(HexDirection.SE, cells[i - width]);

                // conect the SW cells (expect the first one)
                if (x > 0)
                {
                    cell.SetNeighbor(HexDirection.SW, cells[i - width - 1]);
                }
            }
            else
            {
                cell.SetNeighbor(HexDirection.SW, cells[i - width]);

                if (x < width - 1)
                {
                    cell.SetNeighbor(HexDirection.SE, cells[i - width + 1]);
                }
            }
        }

        if (gridCanvas != null)
        {
            // Make hex cells more visable
            Text label = Instantiate(cellLabelPrefab);
            label.rectTransform.SetParent(gridCanvas.transform, false);
            label.rectTransform.anchoredPosition = new Vector2(position.x, position.z);
            label.text = cell.coords.ToStringOnSeperateLines();
        }
    }