Exemple #1
0
        // Start is called before the first frame update
        void Start()
        {
            timer_nextUpdate = 0;

            mazeMap         = new CellArray2D_BitArray(xSize, ySize, 2);
            generateProcess = new Process_2DGenerateBacktrack(mazeMap, 0, 0);
            printProcess    = new Process_2DPrintByCurrCell(mazeMap, (Process_2DGenerateBacktrack)generateProcess, cellSpacing, cellValParentObj, currentLocationParentObj, cellValPrintPrefab, currentLocationPrefab);

            generateCouldStep = !generateOnStartup;

            if (generateOnStartup)
            {
                generateProcess.RunWhileHasSteps();
                RePrintMapObjects();
#if LOG_DEBUG
                Debug.Log(mazeMap.ToString());
#endif
            }
            else
            {
                if (printMaze)
                {
                    printProcess.FirstPrint();
                }
            }
        }
        // Move in the specified direction
        private void MoveInDir(MoveDirs2D moveDir, CellArray2D_BitArray theMap)
        {
            // Setting theMap[] vals to true when wall is broken down
            switch (moveDir)
            {
            case MoveDirs2D.UpX:
                currCellX += 1;
                theMap[currCellX, currCellY, 0] = true;
                break;

            case MoveDirs2D.DownX:
                theMap[currCellX, currCellY, 0] = true;
                currCellX -= 1;
                break;

            case MoveDirs2D.UpY:
                currCellY += 1;
                theMap[currCellX, currCellY, 1] = true;
                break;

            case MoveDirs2D.DownY:
                theMap[currCellX, currCellY, 1] = true;
                currCellY -= 1;
                break;
            }
        }
Exemple #3
0
        public Process_2DPrintWithObjects(CellArray2D_BitArray theMap, GameObject theParentObj, GameObject thePrintObj, float theSpacing)
        {
            map = theMap;

            parentObj = theParentObj;
            printObj  = thePrintObj;

            spacing = theSpacing;
        }
        public Process_2DGenerateBacktrack(CellArray2D_BitArray theMap, int startCellX, int startCellY)
        {
            currCellX = startCellX;
            currCellY = startCellY;

            map          = theMap;
            previousPath = new Stack <MoveDirs2D> ();
            isVisited    = new CellArray2D_BitArray(theMap.sizeX, theMap.sizeY, 1);
            rand         = new Random();
            dirs         = new MoveDirs2D[] { MoveDirs2D.UpX, MoveDirs2D.DownX, MoveDirs2D.UpY, MoveDirs2D.DownY };
        }
Exemple #5
0
        public Process_2DPrintByCurrCell(CellArray2D_BitArray theInGenMap, Process_2DGenerateBacktrack theCurrCellGen, float theSpacing, GameObject theWallParentObj, GameObject theCurrLocParentObj, GameObject thePrintPrefab, GameObject theLocPrefab)
        {
            inGenMap    = theInGenMap;
            objMap      = new CellArray2D_GameObj(theInGenMap.sizeX, theInGenMap.sizeY, 2);
            currCellGen = theCurrCellGen;

            spacing = theSpacing;

            wallParentObj    = theWallParentObj;
            currLocParentObj = theCurrLocParentObj;
            printPrefab      = thePrintPrefab;
            locPrefab        = theLocPrefab;
        }
Exemple #6
0
        private void Update()
        {
            if (!shouldUpdate)
            {
                return;
            }
            if (!generateCouldStep)
            {
                return;
            }

#if STOPWATCH
            if (!watch.IsRunning)
            {
                watch.Start();
            }
#endif

            timer_nextUpdate += Time.deltaTime;
            if (timer_nextUpdate < timeBetweenUpdates)
            {
                return;
            }
            timer_nextUpdate -= timeBetweenUpdates;

            for (int i = 0; i < stepsPerUpdate; i++)
            {
                if (printMaze)
                {
                    printProcess.RunProcess();
                }

                generateCouldStep = generateCouldStep && generateProcess.RunProcess();

                if (!generateCouldStep)
                {
#if LOG_DEBUG
                    Debug.Log(mazeMap.ToString());
#endif
#if STOPWATCH
                    Debug.Log($"Execution Time: {watch.ElapsedMilliseconds} ms");
#endif
                    mazeMap = null;
                    break;
                }
            }
        }
Exemple #7
0
        private void PrintBorders(CellArray2D_BitArray theMap)
        {
            int xCount = theMap.sizeX * 2;
            int yCount = theMap.sizeY * 2;

            // UpX Border, +1 to cover corner in UpX,UpY (upper right, last cell)
            for (int x = 0; x < xCount + 1; x++)
            {
                GameObject xBorder = GameObject.Instantiate(printObj, parentObj.transform);
                xBorder.transform.localPosition = new Vector3(x * spacing, yCount * spacing);
            }

            // UpY Border
            for (int y = 0; y < yCount; y++)
            {
                GameObject xBorder = GameObject.Instantiate(printObj, parentObj.transform);
                xBorder.transform.localPosition = new Vector3(xCount * spacing, y * spacing);
            }
        }
Exemple #8
0
        private void PrintCell(CellArray2D_BitArray theMap, int x, int y)
        {
            float cellLocX = x * spacing * 2;
            float cellLocY = y * spacing * 2;

            // Instantiate cell's game objects
            GameObject solidCorner = GameObject.Instantiate(printObj, parentObj.transform);

            solidCorner.transform.localPosition = new Vector3(cellLocX, cellLocY);

            if (!theMap[x, y, 0])
            {
                GameObject firstCorner = GameObject.Instantiate(printObj, parentObj.transform);
                firstCorner.transform.localPosition = new Vector3(cellLocX, cellLocY + spacing);
            }

            if (!theMap[x, y, 1])
            {
                GameObject secondCorner = GameObject.Instantiate(printObj, parentObj.transform);
                secondCorner.transform.localPosition = new Vector3(cellLocX + spacing, cellLocY);
            }
        }
        // Returns false if moveDir goes out of bounds or toward a visited cell
        private bool CanMoveInDir(MoveDirs2D moveDir, CellArray2D_BitArray theMap, CellArray2D_BitArray isVisited)
        {
            switch (moveDir)
            {
            case MoveDirs2D.UpX:
                return(!(currCellX + 1 >= theMap.sizeX) &&
                       !(isVisited[currCellX + 1, currCellY, 0]));

            case MoveDirs2D.DownX:
                return(!(currCellX - 1 < 0) &&
                       !(isVisited[currCellX - 1, currCellY, 0]));

            case MoveDirs2D.UpY:
                return(!(currCellY + 1 >= theMap.sizeY) &&
                       !(isVisited[currCellX, currCellY + 1, 0]));

            case MoveDirs2D.DownY:
                return(!(currCellY - 1 < 0) &&
                       !(isVisited[currCellX, currCellY - 1, 0]));
            }

            return(false);
        }