コード例 #1
0
    /********************
    * Door creation
    ********************/
    public void CreateDoors(VirtualMap map, VirtualRoom r, RoomGenerator roomGenerator)
    {
        // Create a list of border floors (close to the borders of the room)
        List <CellLocation> borderFloors = new List <CellLocation>();

        for (int i = 0; i < r.Width; i++)
        {
            for (int j = 0; j < r.Height; j++)
            {
                if (i == 0 || j == 0 || i == r.Width - 1 || j == r.Height - 1)
                {
                    CellLocation l = new CellLocation(r.leftCorner.x + 2 * i, r.leftCorner.y + 2 * j);
                    borderFloors.Add(l);
                }
            }
        }

        // For each border floor, check if we are connecting to something on the other side
        List <CellLocation>             outsideBorderFloors = new List <CellLocation>();
        List <CellLocation>             insideBorderFloors  = new List <CellLocation>();
        List <VirtualMap.DirectionType> borderDirections    = new List <VirtualMap.DirectionType>();

        CellLocation target_passage;
        CellLocation target_floor;

        foreach (CellLocation l in borderFloors)
        {
            foreach (VirtualMap.DirectionType dir in map.directions)
            {
                target_passage = map.GetNeighbourCellLocation(l, dir);
                target_floor   = map.GetTargetLocation(l, dir);
                if (!map.LocationIsOutsideBounds(target_floor) &&
                    map.GetCell(target_passage).IsWall() &&
                    !map.IsSurroundedByWalls(target_floor))
                {
                    outsideBorderFloors.Add(target_floor);
                    insideBorderFloors.Add(l);
                    borderDirections.Add(dir);
                }
            }
        }

        // We now create a door for each outside border floor, making sure to avoid re-creating doors if the floors are already connected
        List <CellLocation> unremovedFloors = new List <CellLocation>(outsideBorderFloors);

        for (int i = 0; i < outsideBorderFloors.Count; i++)
        {
            CellLocation l = outsideBorderFloors[i];
            // If not already removed (but we may not skip if we request more doors than needed)
            if (unremovedFloors.Contains(l) || DungeonGenerator.Random.Instance.Next(0, 100) < doorsDensityModifier)
            {
                CreateDoor(map, r, roomGenerator, insideBorderFloors[i], l, borderDirections[i]);
                unremovedFloors.Remove(l);

                // We also remove the other connected cells
                for (int j = unremovedFloors.Count - 1; j >= 0; j--)
                {
                    CellLocation other_l    = unremovedFloors[j];
                    bool         existsPath = map.ExistsPathBetweenLocations(l, other_l);
                    if (existsPath)
                    {
                        unremovedFloors.Remove(other_l);
                    }
                }
            }
        }
    }