Exemplo n.º 1
0
    //画线预览效果
    void MazeDraft()
    {
        //重新生成
        //按的时候前提需要focus on Game window,有点尴尬…
        if (Input.GetKeyDown(KeyCode.G))
        {
            mazeGraph.Generate();
        }

        float cellWidth     = 0.2f;
        float cellDepth     = 0.2f;
        float halfMazeWidth = mazeGraph.MazeColumns * cellWidth * 0.5f;
        float halfMazeDepth = mazeGraph.MazeRows * cellDepth * 0.5f;

        int[,] mazeTable = mazeGraph.MazeTable;
        for (int i = 0; i < mazeGraph.CellNum; i++)
        {
            int row    = i / mazeGraph.MazeColumns;
            int column = i % mazeGraph.MazeColumns;
            //画右边的竖线
            if (column != mazeGraph.MazeColumns - 1 && mazeTable[i, 3] < 0)
            {
                Vector3 start = new Vector3((column + 1) * cellWidth - halfMazeWidth, 0f, halfMazeDepth - row * cellDepth);
                Vector3 end   = new Vector3((column + 1) * cellWidth - halfMazeWidth, 0f, halfMazeDepth - (row + 1) * cellDepth);
                Debug.DrawLine(start, end, Color.blue);
            }
            //画下面的横线
            if (row != mazeGraph.MazeRows - 1 && mazeTable[i, 1] < 0)
            {
                Vector3 start = new Vector3(column * cellWidth - halfMazeWidth, 0f, halfMazeDepth - (row + 1) * cellDepth);
                Vector3 end   = new Vector3((column + 1) * cellWidth - halfMazeWidth, 0f, halfMazeDepth - (row + 1) * cellDepth);
                Debug.DrawLine(start, end, Color.blue);
            }
        }
        //画四条边
        Debug.DrawLine(new Vector3(-halfMazeWidth, 0f, halfMazeDepth), new Vector3(halfMazeWidth, 0f, halfMazeDepth), Color.blue);
        Debug.DrawLine(new Vector3(halfMazeWidth, 0f, halfMazeDepth), new Vector3(halfMazeWidth, 0f, cellDepth - halfMazeDepth), Color.blue);
        Debug.DrawLine(new Vector3(-halfMazeWidth, 0f, halfMazeDepth - cellDepth), new Vector3(-halfMazeWidth, 0f, -halfMazeDepth), Color.blue);
        Debug.DrawLine(new Vector3(-halfMazeWidth, 0f, -halfMazeDepth), new Vector3(halfMazeWidth, 0f, -halfMazeDepth), Color.blue);

        //path
        List <int> path = mazeGraph.CalPath();
        Vector3    pathStart = new Vector3(-halfMazeWidth + 0.5f * cellWidth, 0f, halfMazeDepth - 0.5f * cellDepth), pathEnd;

        for (int i = path.Count - 2; i >= 0; i--)
        {
            int row    = path[i] / mazeGraph.MazeColumns;
            int column = path[i] % mazeGraph.MazeColumns;
            pathEnd = new Vector3(column * cellWidth - halfMazeWidth + 0.5f * cellWidth, 0f, halfMazeDepth - row * cellDepth - 0.5f * cellDepth);
            Debug.DrawLine(pathStart, pathEnd, Color.green);
            pathStart = pathEnd;
        }
        Debug.DrawLine(pathStart, new Vector3(halfMazeWidth - 0.5f * cellWidth, 0f, -halfMazeDepth + 0.5f * cellDepth), Color.green);
    }