public void CreateDenseMaze(csDungeon dungeon) { Vector2 currentLocation = dungeon.PickRandomCellAndFlagItAsVisited(); csDungeonCell.DirectionType previousDirection = csDungeonCell.DirectionType.North; while (!dungeon.AllCellsAreVisited) { csDirectionPicker directionPicker = new csDirectionPicker(); directionPicker.Constructor(previousDirection, changeDirectionModifier); csDungeonCell.DirectionType direction = directionPicker.GetNextDirection(); while (!dungeon.HasAdjacentCellInDirection(currentLocation, direction) || dungeon.AdjacentCellInDirectionIsVisited(currentLocation, direction)) { if (directionPicker.HasNextDirection) { direction = directionPicker.GetNextDirection(); } else { currentLocation = dungeon.GetRandomVisitedCell(currentLocation); // Get a new previously visited location directionPicker = new csDirectionPicker(); directionPicker.Constructor(previousDirection, changeDirectionModifier); // Reset the direction picker direction = directionPicker.GetNextDirection(); // Get a new direction } } currentLocation = dungeon.CreateCorridor(currentLocation, direction); dungeon.FlagCellAsVisited(currentLocation); previousDirection = direction; } }
public void RemoveDeadEnds(csDungeon dungeon) { foreach (Vector2 deadEndLocation in dungeon.DeadEndCellLocations) { if (ShouldRemoveDeadend()) { Vector2 currentLocation = deadEndLocation; do { // Initialize the direction picker not to select the dead-end corridor direction csDirectionPicker directionPicker = new csDirectionPicker(); directionPicker.Constructor(dungeon[currentLocation].CalculateDeadEndCorridorDirection(), 100); csDungeonCell.DirectionType direction = directionPicker.GetNextDirection(); while (!dungeon.HasAdjacentCellInDirection(currentLocation, direction)) { if (directionPicker.HasNextDirection) { direction = directionPicker.GetNextDirection(); } else { throw new InvalidOperationException("This should not happen"); } } // Create a corridor in the selected direction currentLocation = dungeon.CreateCorridor(currentLocation, direction); } while (dungeon[currentLocation].IsDeadEnd); // Stop when you intersect an existing corridor. } } }
public void PlaceRoom(Vector2 location, csDungeonRoom room, csDungeon dungeon) { // Offset the room origin to the new location room.SetLocation(location); // Loop for each cell in the room foreach (Vector2 roomLocation in room.CellLocations) { // Translate the room cell location to its location in the dungeon Vector2 dungeonLocation = new Vector2(location.x + roomLocation.x, location.y + roomLocation.y); dungeon[dungeonLocation].NorthSide = room[roomLocation].NorthSide; dungeon[dungeonLocation].SouthSide = room[roomLocation].SouthSide; dungeon[dungeonLocation].WestSide = room[roomLocation].WestSide; dungeon[dungeonLocation].EastSide = room[roomLocation].EastSide; // Create room walls on map (either side of the wall) if ((roomLocation.x == 0) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.West))) { dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.West); } if ((roomLocation.x == room.Width - 1) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.East))) { dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.East); } if ((roomLocation.y == 0) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.North))) { dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.North); } if ((roomLocation.y == room.Height - 1) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.South))) { dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.South); } } dungeon.AddRoom(room); }