Example #1
0
    public void PlaceDoors(csDungeon dungeon)
    {
        foreach (csDungeonRoom room in dungeon.Rooms)
        {
            bool hasNorthDoor = false;
            bool hasSouthDoor = false;
            bool hasWestDoor  = false;
            bool hasEastDoor  = false;

            foreach (Vector2 cellLocation in room.CellLocations)
            {
                // Translate the room cell location to its location in the dungeon
                Vector2 dungeonLocation = new Vector2(room.Bounds.x + cellLocation.x, room.Bounds.y + cellLocation.y);

                // Check if we are on the west boundary of our room
                // and if there is a corridor to the west
                if ((cellLocation.x == 0) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.West)) &&
                    (!hasWestDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.West);
                    hasWestDoor = true;
                }

                // Check if we are on the east boundary of our room
                // and if there is a corridor to the east
                if ((cellLocation.x == room.Width - 1) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.East)) &&
                    (!hasEastDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.East);
                    hasEastDoor = true;
                }

                // Check if we are on the north boundary of our room
                // and if there is a corridor to the north
                if ((cellLocation.y == 0) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.North)) &&
                    (!hasNorthDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.North);
                    hasNorthDoor = true;
                }


                // Check if we are on the south boundary of our room
                // and if there is a corridor to the south
                if ((cellLocation.y == room.Height - 1) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.South)) &&
                    (!hasSouthDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.South);
                    hasSouthDoor = true;
                }
            }
        }
    }
Example #2
0
    public int CalculateRoomPlacementScore(Vector2 location, csDungeonRoom room, csDungeon dungeon)
    {
        // Check if the room at the given location will fit inside the bounds of the map

        //if (dungeon.Bounds.Contains(new Rect(location.x, location.y, (float)room.Width + 1, (float)room.Height + 1)))
        if (dungeon.Bounds.Contains(location) &&
            dungeon.Bounds.Contains(new Vector2(location.x + room.Width + 1, location.y + room.Height + 1)))
        {
            int roomPlacementScore = 0;

            // 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);

                // Add 1 point for each adjacent corridor to the cell
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.North))
                {
                    roomPlacementScore++;
                }
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.South))
                {
                    roomPlacementScore++;
                }
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.West))
                {
                    roomPlacementScore++;
                }
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.East))
                {
                    roomPlacementScore++;
                }

                // Add 3 points if the cell overlaps an existing corridor
                if (dungeon[dungeonLocation].IsCorridor)
                {
                    roomPlacementScore += 3;
                }

                // Do not allow rooms to overlap!
                foreach (csDungeonRoom dungeonRoom in dungeon.Rooms)
                {
                    if (dungeonRoom.Bounds.Contains(dungeonLocation))
                    {
                        return(0);
                    }
                }
            }

            return(roomPlacementScore);
        }
        else
        {
            return(0);
        }
    }