// расставляет внутри комнаты случайные перегородки private void constructPartitionWalls(Room room) { setInnerSidesTo(room, SideState.Wall); var randFirstBlock = room.Blocks[_rand.Next(room.Blocks.Count)]; var addedBlocks = new List <RoomBlock>(); addedBlocks.Add(randFirstBlock); while (addedBlocks.Count < room.Blocks.Count) { var block = addedBlocks[_rand.Next(addedBlocks.Count)]; // найти все соседние блоки этой же комнаты, исключая уже добавленные блоки var shuffledSides = block.Sides.OrderBy(_ => _rand.Next()).ToArray(); for (int j = 0; j < shuffledSides.Length; j++) { Side currentSide = shuffledSides[j]; Vector2 neighbourPos = currentSide.ToPosition(); RoomBlock neighbourBlock = room.Blocks.FirstOrDefault(b => b.Position == neighbourPos); if (neighbourBlock != null && !addedBlocks.Contains(neighbourBlock)) { currentSide.State = SideState.None; neighbourBlock.GetSideByDirection(currentSide.Direction.GetOpposite()).State = SideState.None; addedBlocks.Add(neighbourBlock); break; } } } }
private void constructPartitionWallsAsMaze(Room room) { setInnerSidesTo(room, SideState.Wall); var randFirstBlock = room.Blocks[_rand.Next(room.Blocks.Count)]; Stack <RoomBlock> path = new Stack <RoomBlock>(); path.Push(randFirstBlock); Dictionary <RoomBlock, bool> visitedMap = room.Blocks.ToDictionary(b => b, v => false); visitedMap[randFirstBlock] = true; while (visitedMap.Values.Contains(false)) { var block = path.Peek(); var shuffledSides = block.Sides.OrderBy(_ => _rand.Next()).ToArray(); bool finded = false; for (int j = 0; j < shuffledSides.Length; j++) { Side currentSide = shuffledSides[j]; Vector2 neighbourPos = currentSide.ToPosition(); RoomBlock neighbourBlock = room.Blocks.FirstOrDefault(b => b.Position == neighbourPos); if (neighbourBlock != null && visitedMap[neighbourBlock] == false) { visitedMap[neighbourBlock] = true; path.Push(neighbourBlock); currentSide.State = SideState.None; neighbourBlock.GetSideByDirection(currentSide.Direction.GetOpposite()).State = SideState.None; finded = true; break; } } if (finded == false) { path.Pop(); } } }