示例#1
0
        public ValueMap Generate()
        {
            // initialize
            maze   = ValueMap.CreateInstance(size.x, size.y);
            random = new System.Random(seed.GetHashCode());
            List <Vector2Int> activeCells = new List <Vector2Int>();

            // add the start cell to the active list
            activeCells.Add(start);
            maze[start.x, start.y] = 1;

            // while active list is not empty
            while (activeCells.Count > 0)
            {
                // select cell from active list
                // randomly selected based on waviness
                // higher waviness produces long winding passages with fewer dead ends, and vice versa
                int cellIndex = random.NextDouble() > waviness?random.Next(activeCells.Count) : activeCells.Count - 1;

                var cell = activeCells[cellIndex];

                // get untraversed neighbours of selected cell
                var neighbours = GetNeighbours(cell.x, cell.y);

                if (neighbours.Count > 0)
                {
                    // randomly select a neighbour and connect it to the selected cell
                    var neighbour = neighbours[random.Next(neighbours.Count)];
                    maze.ConnectCells(cell, neighbour, 1);

                    // add that neighbour to the active list
                    activeCells.Add(neighbour);
                }
                else
                {
                    // if no traversable neighbours, remove cell from active list
                    activeCells.Remove(cell);
                }
            }

            return(maze);
        }
示例#2
0
        public ValueMap Generate(Vector2Int start)
        {
            tree = ScriptableObject.CreateInstance <ValueMap>();
            tree.Initialize(size.x, size.y);

            var leaves = SpawnLeaves();

            List <Branch> branches = new List <Branch>();

            branches.Add(new Branch(start, null));

            int step = 0;

            while (true)
            {
                step++;
                if (step > maxSteps)
                {
                    break;
                }

                List <Vector2> leavesToRemove = new List <Vector2>();

                foreach (var leaf in leaves)
                {
                    Branch closest = null;
                    float  closestBranchDistance = float.MaxValue;
                    for (int i = 0; i < branches.Count; i++)
                    {
                        var direction = leaf - branches[i].position;
                        var distance  = direction.magnitude;

                        if (distance < maxDistance)
                        {
                            if (distance < closestBranchDistance)
                            {
                                closest = branches[i];
                                closestBranchDistance = distance;
                            }
                        }
                        if (distance < minDistance)
                        {
                            leavesToRemove.Add(leaf);
                        }
                    }

                    if (closest != null)
                    {
                        var direction = leaf - closest.position;

                        closest.direction += direction.normalized;
                        closest.growCount++;
                    }
                }

                foreach (var leaf in leavesToRemove)
                {
                    leaves.Remove(leaf);
                }

                List <Branch> newBranches = new List <Branch>();
                foreach (var branch in branches)
                {
                    if (branch.growCount > 0 && branch.growCount != branch.prevGrowCount)
                    {
                        Vector2 newDirection = branch.direction;

                        newDirection /= branch.growCount;
                        newDirection  = newDirection.normalized;

                        newBranches.Add(new Branch(branch.position + (newDirection * stepSize), branch));
                        ;
                        branch.prevGrowCount = branch.growCount;
                        branch.growCount     = 0;
                        branch.ResetDirection();
                    }
                }
                branches.AddRange(newBranches);

                if (newBranches.Count == 0)
                {
                    break;
                }
            }

            foreach (var branch in branches)
            {
                if (branch.parent != null)
                {
                    tree.ConnectCells(branch.parent.position, branch.position, 1);
                }
            }

            return(tree);
        }