Beispiel #1
0
    //method executed on thread
    private void Generating()
    {
        Debug.Log("thread begin");

        grid = new MinorGrid[majorDimension, majorDimension];

        //filling grid with base pattern
        for (int y = 0; y < majorDimension; y++)
        {
            for (int x = 0; x < majorDimension; x++)
            {
                if (x % 2 != 0 && y % 2 != 0)
                {
                    grid[x, y] = new MinorGrid(new IntVector2(x, y), minorDimension, LabirynthCell.TYPE.WALKABLE, randomNumbersGenerator, minorRepeatChance);
                }
                else
                {
                    grid[x, y] = new MinorGrid(new IntVector2(x, y), minorDimension, LabirynthCell.TYPE.WALL, randomNumbersGenerator, minorRepeatChance);
                }
            }
        }



        List <MinorGrid> walkedMajorCells = new List <MinorGrid>();

        //set first cell as walked
        grid[cursor.x, cursor.y].type = LabirynthCell.TYPE.PATH;
        walkedMajorCells.Add(grid[cursor.x, cursor.y]);

        //generate minorLabirynth on first majorCell
        grid[cursor.x, cursor.y].Generate();


        int majorTimeout = (int)Mathf.Pow(majorDimension, 2) * 2; //timeout variable to stop while loop if something goes wrong

        while (walkedMajorCells.Count > 0 && majorTimeout > 0)
        {
            int repeat = randomNumbersGenerator.GetRandomNumber(0, 101);     //get random number to draw if generator should make next step from last cursor position or draw new position from walkedCells
            int randomWalked;
            if (repeat < majorRepeatChance)
            {
                randomWalked = walkedMajorCells.Count - 1;
                cursor       = walkedMajorCells[randomWalked].position;
            }
            else
            {
                randomWalked = randomNumbersGenerator.GetRandomNumber(0, walkedMajorCells.Count);
                cursor       = walkedMajorCells[randomWalked].position;
            }


            //get all neighbours of spot that is pointed by curosr
            List <MinorGrid> neighbours = GetNeighbours(cursor);


            //remove from walked cells if haven`t got any walkable neighbour
            if (neighbours.Count <= 0)
            {
                walkedMajorCells.RemoveAt(randomWalked);
                continue;
            }

            //draw neighbour to next step
            int       randomNeighbour   = randomNumbersGenerator.GetRandomNumber(0, neighbours.Count);
            MinorGrid selectedNeighbour = neighbours[randomNeighbour];

            //get direction of step to selected neighbour
            IntVector2 stepDirection = new IntVector2(selectedNeighbour.position.x - cursor.x, selectedNeighbour.position.y - cursor.y);

            //create list of cells walked by this step to generate minorLabirynths on them
            List <MinorGrid> minorLabirynthsToGenerate = new List <MinorGrid>();

            //setting neighbour as walked, add it to walked cells and add it to list of cells to generate minorLabirynth
            walkedMajorCells.Add(selectedNeighbour);
            grid[selectedNeighbour.position.x, selectedNeighbour.position.y].type = LabirynthCell.TYPE.PATH;
            minorLabirynthsToGenerate.Add(selectedNeighbour);

            //calculate position of cell between current cell and selected neighbour
            IntVector2 w = new IntVector2((cursor.x + selectedNeighbour.position.x) / 2, (cursor.y + selectedNeighbour.position.y) / 2);

            //setting between cell as walked and add it to list of cells to generate minorLabirynth
            grid[w.x, w.y].type = LabirynthCell.TYPE.PATH;
            minorLabirynthsToGenerate.Add(grid[w.x, w.y]);

            //generate minorLabirynths on new walked cells
            for (int i = 0; i < minorLabirynthsToGenerate.Count; i++)
            {
                minorLabirynthsToGenerate[i].Generate();
            }


            int firstDoor;
            int secondDoor;

            //drawing first door position between majorCells
            firstDoor = randomNumbersGenerator.GetRandomNumber(1, minorDimension - 2);
            if (firstDoor % 2 == 0)
            {
                if (firstDoor + 1 <= minorDimension - 2)
                {
                    firstDoor += 1;
                }
                else if (firstDoor - 1 >= 1)
                {
                    firstDoor -= 1;
                }
            }

            //drawing second door position between majorCells
            secondDoor = randomNumbersGenerator.GetRandomNumber(1, minorDimension - 2);
            if (secondDoor % 2 == 0)
            {
                if (secondDoor + 1 <= minorDimension - 2)
                {
                    secondDoor += 1;
                }
                else if (secondDoor - 1 >= 1)
                {
                    secondDoor -= 1;
                }
            }

            //setting doors in new walked cells
            if (stepDirection.x != 0)
            {
                if (stepDirection.x < 0)
                {
                    grid[neighbours[randomNeighbour].position.x, neighbours[randomNeighbour].position.y].minorGrid[minorDimension - 1, secondDoor].type = LabirynthCell.TYPE.PATH;

                    grid[cursor.x, cursor.y].minorGrid[0, firstDoor].type = LabirynthCell.TYPE.PATH;

                    grid[w.x, w.y].minorGrid[0, secondDoor].type = LabirynthCell.TYPE.PATH;
                    grid[w.x, w.y].minorGrid[minorDimension - 1, firstDoor].type = LabirynthCell.TYPE.PATH;
                }
                else if (stepDirection.x > 0)
                {
                    grid[neighbours[randomNeighbour].position.x, neighbours[randomNeighbour].position.y].minorGrid[0, secondDoor].type = LabirynthCell.TYPE.PATH;

                    grid[cursor.x, cursor.y].minorGrid[minorDimension - 1, firstDoor].type = LabirynthCell.TYPE.PATH;

                    grid[w.x, w.y].minorGrid[0, firstDoor].type = LabirynthCell.TYPE.PATH;
                    grid[w.x, w.y].minorGrid[minorDimension - 1, secondDoor].type = LabirynthCell.TYPE.PATH;
                }
            }
            else if (stepDirection.y != 0)
            {
                if (stepDirection.y < 0)
                {
                    grid[neighbours[randomNeighbour].position.x, neighbours[randomNeighbour].position.y].minorGrid[secondDoor, minorDimension - 1].type = LabirynthCell.TYPE.PATH;

                    grid[cursor.x, cursor.y].minorGrid[firstDoor, 0].type = LabirynthCell.TYPE.PATH;

                    grid[w.x, w.y].minorGrid[secondDoor, 0].type = LabirynthCell.TYPE.PATH;
                    grid[w.x, w.y].minorGrid[firstDoor, minorDimension - 1].type = LabirynthCell.TYPE.PATH;
                }
                else if (stepDirection.y > 0)
                {
                    grid[neighbours[randomNeighbour].position.x, neighbours[randomNeighbour].position.y].minorGrid[secondDoor, 0].type = LabirynthCell.TYPE.PATH;

                    grid[cursor.x, cursor.y].minorGrid[firstDoor, minorDimension - 1].type = LabirynthCell.TYPE.PATH;

                    grid[w.x, w.y].minorGrid[firstDoor, 0].type = LabirynthCell.TYPE.PATH;
                    grid[w.x, w.y].minorGrid[secondDoor, minorDimension - 1].type = LabirynthCell.TYPE.PATH;
                }
            }


            //decrement timeout variable
            majorTimeout--;

            if (majorTimeout <= 0)
            {
                Debug.LogWarning("MAJOR GENERATING TIMEOUT");
                return;
            }
        }

        //set flag for spawning on main thread
        generatingDone = true;

        Debug.Log("generated");
    }
Beispiel #2
0
    public void Generate()
    {
        //filling grid with base pattern
        for (int y = 0; y < minorDimension; y++)
        {
            for (int x = 0; x < minorDimension; x++)
            {
                if (x % 2 != 0 && y % 2 != 0)
                {
                    minorGrid[x, y] = new LabirynthCell(new IntVector2(x, y), LabirynthCell.TYPE.WALKABLE);
                }
                else
                {
                    minorGrid[x, y] = new LabirynthCell(new IntVector2(x, y), LabirynthCell.TYPE.WALL);
                }
            }
        }



        List <LabirynthCell> walkedMinorCells = new List <LabirynthCell>();

        //set first cell as walked
        minorGrid[cursor.x, cursor.y].type = LabirynthCell.TYPE.PATH;
        walkedMinorCells.Add(minorGrid[cursor.x, cursor.y]);


        int minorTimeout = (int)Mathf.Pow(minorDimension, 2) * 2;//timeout variable to stop while loop if something goes wrong

        while (walkedMinorCells.Count > 0 && minorTimeout > 0)
        {
            int repeat = randomNumbersGenerator.GetRandomNumber(0, 101);     //get random number to draw if generator should make next step from last cursor position or draw new position from walkedCells
            int randomWalked;
            if (repeat < minorRepeatChance)
            {
                randomWalked = walkedMinorCells.Count - 1;
                cursor       = walkedMinorCells[randomWalked].position;
            }
            else
            {
                randomWalked = randomNumbersGenerator.GetRandomNumber(0, walkedMinorCells.Count);
                cursor       = walkedMinorCells[randomWalked].position;
            }

            //get all neighbours of spot that is pointed by curosr
            List <LabirynthCell> neighbours = GetNeighbours(cursor);

            //remove from walked cells if haven`t got any walkable neighbour
            if (neighbours.Count <= 0)
            {
                walkedMinorCells.RemoveAt(randomWalked);
                continue;
            }

            //draw neighbour to next step
            int           randomNeighbour   = randomNumbersGenerator.GetRandomNumber(0, neighbours.Count);
            LabirynthCell selectedNeighbour = neighbours[randomNeighbour];

            //setting neighbour as walked and add it to walked cells
            walkedMinorCells.Add(selectedNeighbour);
            minorGrid[selectedNeighbour.position.x, selectedNeighbour.position.y].type = LabirynthCell.TYPE.PATH;

            //calculate position of cell between current cell and selected neighbour and setting between cell as walked
            IntVector2 w = new IntVector2((cursor.x + selectedNeighbour.position.x) / 2, (cursor.y + selectedNeighbour.position.y) / 2);
            minorGrid[w.x, w.y].type = LabirynthCell.TYPE.PATH;

            //decrement timeout variable
            minorTimeout--;

            if (minorTimeout <= 0)
            {
                Debug.LogWarning("MINOR GENERATING TIMEOUT");
                return;
            }
        }
    }