Example #1
0
    private void SetRandomBlock(SquareCellWithWall self, SquareDirection neighborDir, int probability)
    {
        if (self == null)
        {
            return;
        }

        SquareCellWithWall neighbor = self.neighbors[(int)neighborDir] as SquareCellWithWall;

        if (self == null)
        {
            return;
        }

        if (neighbor == null)
        {
            return;
        }

        if (UnityEngine.Random.Range(0, 100) <= probability)
        {
            self.SetNeighbor(neighborDir, null);

            SquareDirection selfDir = neighborDir.Opposite();
            neighbor.SetNeighbor(selfDir, null);
        }
    }
Example #2
0
    private void ResetNeightbor(CellShape cellShape, int x, int y)
    {
        int id = x + MapManager.Instance.MapWidth * y;

        if (cellShape == CellShape.Hexagon)
        {
            HexCell cell = CellManager.Instance.GetCellByCoordinates(x, y) as HexCell;

            if (cell == null)
            {
                return;
            }

            // 初始化 东-西方向 邻居关系
            if (x > 0)
            {
                cell.SetNeighbor(HexDirection.W, CellManager.Instance.cells[id - 1] as HexCell);
            }
            // 有另外两个双向的连接需要完成。由于它们在不同行之间,我们只能连接之前的行。
            // 需要跳过整个第一行。
            if (y > 0)
            {
                // 偶数行
                if ((y & 1) == 0)
                {
                    // 所有单元都有东南方向的邻居。
                    cell.SetNeighbor(HexDirection.SE, CellManager.Instance.cells[id - width] as HexCell);
                    // 除了第一个单元,都有西南邻居。
                    if (x > 0)
                    {
                        cell.SetNeighbor(HexDirection.SW, CellManager.Instance.cells[id - width - 1] as HexCell);
                    }
                }
                // 奇数行
                else
                {
                    cell.SetNeighbor(HexDirection.SW, CellManager.Instance.cells[id - width] as HexCell);
                    if (x < width - 1)
                    {
                        cell.SetNeighbor(HexDirection.SE, CellManager.Instance.cells[id - width + 1] as HexCell);
                    }
                }
            }
        }
        else if (cellShape == CellShape.Square)
        {
            SquareCell cell = CellManager.Instance.GetCellByCoordinates(x, y) as SquareCell;

            if (cell == null)
            {
                return;
            }

            // 初始化 东-西方向 邻居关系
            if (x > 0)
            {
                cell.SetNeighbor(SquareDirection.W, CellManager.Instance.cells[id - 1] as SquareCell);
            }

            // 有另外两个双向的连接需要完成。由于它们在不同行之间,我们只能连接之前的行。
            // 需要跳过整个第一行。
            if (y > 0)
            {
                cell.SetNeighbor(SquareDirection.S, CellManager.Instance.cells[id - width] as SquareCell);
            }
        }
        else if (cellShape == CellShape.SquareWithWall)
        {
            SquareCellWithWall cell = CellManager.Instance.GetCellByCoordinates(x, y) as SquareCellWithWall;

            if (cell == null)
            {
                return;
            }

            // 初始化 东-西方向 邻居关系
            if (x > 0)
            {
                cell.SetNeighbor(SquareDirection.W, CellManager.Instance.cells[id - 1] as SquareCellWithWall);
            }

            if (y > 0)
            {
                cell.SetNeighbor(SquareDirection.S, CellManager.Instance.cells[id - width] as SquareCellWithWall);
            }
        }
        else
        {
            Debug.LogError("No Such Shape.");
        }
    }