private void DigInnerArea(Rect rect, int sizeInCellX, ref DynamicBuffer <EntityBufferElement> cellsBuffer)
        {
            List <int2> innerPositions = rect.GetInnerPositions();

            foreach (int2 pos in innerPositions)
            {
                SetWall(ref cellsBuffer, sizeInCellX, pos, false);
            }
        }
        private void GenerateDungeon(ref DungeonComponent dungeon, ref DynamicBuffer <EntityBufferElement> cellsBuffer)
        {
            Debug.Assert(dungeon.IsPendingGenerate, "This dungeon is not pending generate.");

            dungeon.IsPendingGenerate = false;

            // Set all cells as wall
            SetWallAll(ref cellsBuffer, true);

            // Rooms
            Rect                    fullRect = new Rect(int2.zero, dungeon.SizeInCell.x, dungeon.SizeInCell.y);
            RectNode                root     = RectNode.CreateBspTree(fullRect, dungeon.MaxRoomLengthInCells, dungeon.MinSplitRatio, dungeon.MaxSplitRatio);
            List <RectNode>         leafs    = root.GetLeafs();
            Dictionary <Rect, Room> rooms    = new Dictionary <Rect, Room>();

            foreach (RectNode leaf in leafs)
            {
                Room room = GenerateRoom(leaf.Rect, dungeon.MinRoomLengthInCells, dungeon.SizeInCell.x, ref cellsBuffer);
                rooms.Add(leaf.Rect, room);
            }

            // Paths
            Stack <RectNode> nodeStack = new Stack <RectNode>();

            nodeStack.Push(root);
            while (nodeStack.Count > 0)
            {
                RectNode node = nodeStack.Pop();
                if (node.LeftNode == null && node.RightNode == null)
                {
                    continue;
                }

                Room roomLeft  = rooms[node.LeftNode.GetRandomLeaf().Rect];
                Room roomRight = rooms[node.RightNode.GetRandomLeaf().Rect];
                GeneratePath(roomLeft, roomRight, dungeon.SizeInCell.x, ref cellsBuffer);

                nodeStack.Push(node.LeftNode);
                nodeStack.Push(node.RightNode);
            }

            // Extra paths
            if (root != null && root.LeftNode != null && root.RightNode != null)
            {
                for (int count = 0; count < dungeon.ExtraPathNum; count++)
                {
                    Room roomLeft  = rooms[root.LeftNode.GetRandomLeaf().Rect];
                    Room roomRight = rooms[root.RightNode.GetRandomLeaf().Rect];
                    GeneratePath(roomLeft, roomRight, dungeon.SizeInCell.x, ref cellsBuffer);
                }
            }
        }
        private Room GenerateRoom(Rect area, int minLength, int sizeInCellX, ref DynamicBuffer <EntityBufferElement> cellsBuffer)
        {
            int  width        = Random.Range(Mathf.Min(minLength, area.Width), area.Width + 1);
            int  height       = Random.Range(Mathf.Min(minLength, area.Height), area.Height + 1);
            int2 lowerLeftPos = area.LowerLeftPos + new int2(Random.Range(0, area.Width - width), Random.Range(0, area.Height - height));
            Rect roomRect     = new Rect(lowerLeftPos, width, height);

            DigInnerArea(roomRect, sizeInCellX, ref cellsBuffer);

            Room room = new Room(roomRect);

            return(room);
        }