Пример #1
0
    //Called on button press
    //Creates the grid, runs the maze generation algorithm,
    //braids the maze and initialises walls
    public void RunGeneration()
    {
        if (CellPrefab != null && WallPrefab != null)
        {
            //initialise cells
            mCells = new List <cell> ();

            for (int jj = 0; jj < Height; jj++)
            {
                for (int ii = 0; ii < Width; ii++)
                {
                    GameObject obj   = Instantiate(CellPrefab, transform.position, Quaternion.identity, this.transform);
                    cell       cCell = obj.GetComponent <cell> ();
                    cCell.Initialise(Size, ii, jj, Width, Height);
                    mCells.Add(cCell);
                }
            }

            //Run chosen algorithm - switch statement
            switch (Algorithm)
            {
            case SelectAlgorithm.None:
                break;

            case SelectAlgorithm.RecursiveBacktracker:
                recursiveBacktracker();
                break;

            case SelectAlgorithm.Prims:
                primsAlgorithm();
                break;

            case SelectAlgorithm.Kuskals:
                kruskalsAlgorithm();
                break;

            default:
                this.ClearMaze();
                return;
            }

            //Remove deadends (if applicable)
            braid();


            //Create walls
            mWalls = new List <GameObject> ();
            foreach (cell cCell in mCells)
            {
                if (cCell != null)
                {
                    List <GameObject> walls = cCell.CreateWalls(WallPrefab);
                    foreach (GameObject wall in walls)
                    {
                        mWalls.Add(wall);
                    }
                }
            }
        }
    }