Beispiel #1
0
    /// <summary>
    /// Generates the maze with the chosen algorithm, the size will be based
    /// on the current value of the 'row slider' in the main UI
    /// TODO: make algorithm to use variable
    /// </summary>
    void GenerateMaze()
    {
        // Let's start by determining the size of the maze
        if (this.TryCalculateGridSize(out int xSize, out int ySize))
        {
            // now that we've determined the size, let's create all the walls of the maze
            this._allWalls = this.CreateAllWalls(xSize, ySize);
            // after creating all the walls, calculate the positions of the spaces in between (the 'cells')
            List <Vector3> allCellPositions = this.GetAllCellPositions(this._allWalls);

            // use a selected algorithm to destroy the walls to make the maze
            IMazeAlgorithm mazeAlgorithm = new DepthFirstMazeAlgorithm();

            MazeAlgorithmResult result = this.CreateMaze(mazeAlgorithm, allCellPositions);

            // The maze is created, let's add the player and the prize
            Vector3 playerStartPosition = new Vector3(result.StartPosition.x + this._wallCentrePoint, result.StartPosition.y + this._wallCentrePoint);
            // Set the player to the position the mazeAlgorithm provided
            PlayerObject.transform.position = playerStartPosition;
            // Add the Action to be triggered when the Player collides with the Prize
            this.PlayerObject.OnPrizeFound = HandlePrizeFound;

            Vector3 prizePosition = new Vector3(result.EndPosition.x + this._wallCentrePoint, result.EndPosition.y + this._wallCentrePoint);
            // Set the prize to the position the mazeAlgorithm provided
            this.PlayerObject.Prize = Instantiate(PrizeObject, prizePosition, Quaternion.identity);
        }
    }
Beispiel #2
0
    /// <summary>
    /// Uses the given algorithm to retrieve which walls should be destroyed to create the maze
    /// Based on this result, it will Destroy the walls that need to be destroyed,
    /// and set the Player and Prize object on the calculated start- and end position respectively
    /// </summary>
    /// <param name="mazeAlgorithmToUse">Implementation of the algorithm to calculate a path in the maze</param>
    /// <param name="allCellPositions">All available cell coordinates in between the created walls</param>
    MazeAlgorithmResult CreateMaze(IMazeAlgorithm mazeAlgorithmToUse, List <Vector3> allCellPositions)
    {
        MazeAlgorithmResult result = mazeAlgorithmToUse.GenerateMaze(allCellPositions);

        if (result.HasWallsToDestroy)
        {
            // if true, we've found some walls to destroy to create a path in the maze

            // Let's get the x-walls (flat ones with rotation = 0), and the y-walls (upright ones with rotation = 90)
            List <GameObject> xWalls = this._allWalls.Where(wall => wall.transform.rotation.eulerAngles.Equals(new Vector3(0, 0, 0))).ToList();
            List <GameObject> yWalls = this._allWalls.Where(wall => wall.transform.rotation.eulerAngles.Equals(new Vector3(0, 0, 90))).ToList();

            var xWallsToDestroy = result.XWallPositionsToDestroy;
            foreach (var xWallPosition in xWallsToDestroy)
            {
                // take off the (i.e. 0.5) offset used to center the wall
                Vector3 positionWithoutOffset = new Vector3(xWallPosition.x + this._wallCentrePoint, xWallPosition.y, xWallPosition.z);
                // try to find the X-Wall GameObject based on the position the algorithm returned
                var xWallToDestroy = xWalls.FirstOrDefault(wall => wall.transform.position.Equals(positionWithoutOffset));
                // If it's there (as it should be), let's remove it from the scene
                if (xWallToDestroy != null)
                {
                    Destroy(xWallToDestroy);
                }
            }

            var yWallsToDestroy = result.YWallPositionsToDestroy;
            foreach (var yWallPosition in yWallsToDestroy)
            {
                // take off the (i.e. 0.5) offset used to center the wall
                Vector3 positionWithoutOffset = new Vector3(yWallPosition.x, yWallPosition.y + this._wallCentrePoint, yWallPosition.z);
                // try to find the Y-Wall GameObject based on the position the algorithm returned
                var yWallToDestroy = yWalls.FirstOrDefault(wall => wall.transform.position.Equals(positionWithoutOffset));
                // If it's there (as it should be), let's remove it from the scene
                if (yWallToDestroy != null)
                {
                    Destroy(yWallToDestroy);
                }
            }
        }
        else
        {
            // something went terribly wrong
            throw new UnassignedReferenceException("Algorithm found no walls to destroy!");
        }

        // return the result for further handling of positioning the Player and Prize
        return(result);
    }