Example #1
0
        private void generateNextLevel()
        {
            baseMazeEngine.destroyImmediateMazeGeometry();

            List <QVector2IntDir> exitPositionList = baseMazeEngine.getExitPositionList();

            if (exitPositionList.Count > 1)
            {
                exitPositionList.RemoveAt(0);
                baseMazeEngine.setExitPositionList(exitPositionList);
            }

            List <QVector2Int> obstaclePositionList = new List <QVector2Int>();

            if (prevMazeEngine == null)
            {
                prevRect = new QRectInt(QMath.getRandom(1, baseMazeEngine.getMazeWidth() - CHILD_MAZE_SIZE - 2),
                                        QMath.getRandom(1, baseMazeEngine.getMazeHeight() - CHILD_MAZE_SIZE - 2), CHILD_MAZE_SIZE, CHILD_MAZE_SIZE);
                obstaclePositionList.AddRange(rectToList(prevRect));
                prevMazeEngine = createChildMaze(prevRect, childMazeEngine_1);
                prevMazeEngine.generateMaze();

                player.setPosition(prevMazeEngine.transform.TransformPoint(prevMazeEngine.getFinishPositionList()[0].toVector3()));
            }
            else
            {
                prevMazeEngine.destroyImmediateMazeGeometry();
                prevRect       = nextRect;
                prevMazeEngine = nextMazeEngine;
                obstaclePositionList.AddRange(rectToList(prevRect));
            }

            nextRect = new QRectInt(QMath.getRandom(1, baseMazeEngine.getMazeWidth() - CHILD_MAZE_SIZE - 2),
                                    QMath.getRandom(1, baseMazeEngine.getMazeHeight() - CHILD_MAZE_SIZE - 2), CHILD_MAZE_SIZE, CHILD_MAZE_SIZE);
            while (isRectNear(prevRect, nextRect))
            {
                nextRect.x = QMath.getRandom(1, baseMazeEngine.getMazeWidth() - CHILD_MAZE_SIZE - 2);
                nextRect.y = QMath.getRandom(1, baseMazeEngine.getMazeHeight() - CHILD_MAZE_SIZE - 2);
            }

            obstaclePositionList.AddRange(rectToList(nextRect));

            baseMazeEngine.setObstaclePositionList(obstaclePositionList);
            nextMazeEngine = createChildMaze(nextRect, prevMazeEngine == childMazeEngine_1 ? childMazeEngine_2 : childMazeEngine_1);
            nextMazeEngine.generateMaze();
            List <QVector2IntDir> nextMazeEngineFinishPositionList = nextMazeEngine.getFinishPositionList();

            finishTransform.parent        = nextMazeEngine.getMazeData()[nextMazeEngineFinishPositionList[0].x][nextMazeEngineFinishPositionList[0].y].geometry.transform;
            finishTransform.localPosition = new Vector3();

            player.setGoal(nextMazeEngine.transform.TransformPoint(nextMazeEngineFinishPositionList[0].toVector3()), goalReachedHandler);

            baseMazeEngine.generateMaze();

            currentLevel++;
            levelText.text = "LEVEL: " + currentLevel;
        }
        void generateNewMaze()
        {
            mazeEngine.destroyImmediateMazeGeometry();
            mazeEngine.generateMaze();

            List <QVector2IntDir> finishPointList = mazeEngine.getFinishPositionList();

            for (int i = 0; i < finishPointList.Count; i++)
            {
                QVector2IntDir finishPosition        = finishPointList[i];
                GameObject     finishTriggerInstance = (GameObject)GameObject.Instantiate(finishTriggerPrefab);
                finishTriggerInstance.transform.parent        = mazeEngine.transform;
                finishTriggerInstance.transform.localPosition = new Vector3(finishPosition.x * mazeEngine.getMazePieceWidth(), 0.01f, -finishPosition.y * mazeEngine.getMazePieceHeight());
            }

            QFinishTrigger[] finishTriggerArray = FindObjectsOfType <QFinishTrigger>();
            if (finishTriggerArray != null)
            {
                for (int i = 0; i < finishTriggerArray.Length; i++)
                {
                    finishTriggerArray[i].triggerHandlerEvent += finishHandler;
                }
            }

            List <QVector2IntDir> startPointList = mazeEngine.getStartPositionList();

            QFPSController fpsController = FindObjectOfType <QFPSController>();

            if (fpsController != null)
            {
                if (startPointList.Count == 0)
                {
                    fpsController.gameObject.transform.position = new Vector3(0, 1, 0);
                }
                else
                {
                    QVector2IntDir startPoint = startPointList[0];
                    fpsController.gameObject.transform.position = new Vector3(startPoint.x * mazeEngine.getMazePieceWidth(), 0.9f, -startPoint.y * mazeEngine.getMazePieceHeight());
                    fpsController.setRotation(Quaternion.AngleAxis((int)startPoint.direction * 90, Vector3.up));
                }
            }

            currentLevel++;
            levelText.text = "LEVEL: " + currentLevel;
        }
Example #3
0
    private void AddFinishPoints()
    {
        //The maze engine provides a method called getFinishPositionList, which returns a list of finish points
        //Get the list of finish points from the mazeEngine and store them in the variable finishPoints
        List <QVector2IntDir> finishPoints = mazeEngine.getFinishPositionList();

        //Loop through the finish points and instantiate them
        foreach (QVector2IntDir finishPoint in finishPoints)
        {
            //Instantiate a new game object
            GameObject goFinish = Instantiate(finishPrefab) as GameObject;

            //Set the object's parent to the maze so it is correctly positioned within the maze
            goFinish.transform.parent = mazeEngine.transform;

            //Position the finish object relative to its parent (local position)
            //Scale the x and z position according to the maze's width and height
            //The y-position is fixed at 0.01f;
            float x = finishPoint.x * mazeEngine.getMazePieceWidth();
            float z = -finishPoint.y * mazeEngine.getMazePieceHeight();
            goFinish.transform.localPosition = new Vector3(x, 0.01f, z);
        }
    }