コード例 #1
0
ファイル: HexGrids.cs プロジェクト: maydinkenan/Hexagons
    public void GenerateGrid(List <List <HexCell> > newGrid)
    {
        Game_Manager._instance.SetRowsCols(rows, cols);

        rowList = new List <List <GameObject> >();
        // Distance the rows are apart Sqrt(objDist^2 - (objDist/2)^2)
        float fT = ((objDistance * objDistance) - ((objDistance * objDistance * 0.25f)));

        rowDist  = Mathf.Sqrt((objDistance * objDistance) - ((objDistance * objDistance * 0.25f)));
        rowStart = -(cols * objDistance / 2.0f - 0.25f * objDistance);
        v3Pos    = new Vector3(rowStart, rows * rowDist / 2.0f, 0.0f);

        for (int i = 0; i < rows; i++)
        {
            if ((i % 2) == 1)
            {
                v3Pos.x -= objDistance / 2.0f;
            }

            List <GameObject> col = new List <GameObject>();
            for (int j = 0; j < cols; j++)
            {
                GameObject go = Instantiate(prefab);
                go.transform.position   = v3Pos + v3Center;
                go.transform.localScale = v3Scale;
                go.transform.parent     = this.transform;
                go.transform.name      += " " + i + " - " + j;
                v3Pos.x += objDistance;
                Color   cellColor = Game_Manager._instance.GetColor();
                HexCell hc        = go.GetComponent <HexCell>();

                hc.SpawnHexCell(v3Scale, cellColor);
                col.Add(go);
                if (j != 0) // Adds Neighbour to the previous Cell in the Column
                {
                    hc.AddNeighbour(col[j - 1]);
                }

                if (i > 0) // Adds Neighbour to the previous Cells in the previous Row
                {
                    if (i % 2 == 1)
                    {
                        int index = Mathf.Clamp(j - 1, 0, rows - 1);
                        hc.AddNeighbour(rowList[i - 1][index]);
                    }
                    else
                    {
                        int index = Mathf.Clamp(j + 1, 0, cols - 1);
                        hc.AddNeighbour(rowList[i - 1][index]);
                    }
                    go.gameObject.GetComponent <HexCell>().AddNeighbour(rowList[i - 1][j]);
                }

                iTween.ScaleFrom(go.gameObject, Vector3.zero, Random.Range(0.1f, 1f));
            }
            rowList.Add(col);
            v3Pos.x  = rowStart;
            v3Pos.y -= rowDist;
        }
    }
コード例 #2
0
    protected void AddNeighbour(HexCell neighbour, Directions direction)
    {
        if (neighbour != null && !_neighbours.Contains(neighbour))
        {
            if (_neighbours[(int)direction] == null)
            {
                _fieldController.RemoveCellFromDirectionEdges(this, direction);
            }

            _neighbours[(int)direction] = neighbour;
            neighbour.AddNeighbour(this, GetOpositeDirection(direction));
        }
    }