Пример #1
0
        private bool IsValid(Room room, int comingFrom)
        {
            // Checking every direction
            for (int direction = 0; direction < 4; direction++)
            {
                if (comingFrom == direction)
                {
                    if (!room.HasDoorwayTo(direction))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (room.HasDoorwayTo(direction))
                    {
                        // Room has doorway to here
                        if (!DirectionIsLegal(room, direction))
                        {
                            // ... but we're out of bounds
                            return(false);
                        }

                        if (GetRoomInDirection(room, direction) != null)
                        {
                            // ... but the path is already taken
                            // If the blocking door also has a nicely fitting doorway (to the opposite direction so they meet), this is a match
                            if (!GetRoomInDirection(room, direction).HasDoorwayTo((direction + 2) % 4))
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        // Room does not have a doorway to specific direction: Now we have to check that the neighbouring room is not trying to access this room
                        if (GetRoomInDirection(room, direction) != null)
                        {
                            // There is a room here
                            // If the other room has a doorway to where we're generating, this is not a match
                            if (GetRoomInDirection(room, direction).HasDoorwayTo((direction + 2) % 4))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            // Wow, we got this far. In that case the room is valid.
            return(true);
        }
Пример #2
0
        private void ContinueFrom(Room room)
        {
            int x         = room.X;
            int z         = room.Z;
            int direction = _random.Next(0, 4);

            for (int i = 0; i < 4; i++)
            {
                if (room.HasDoorwayTo(direction % 4) && GetRoomInDirection(room, direction % 4) == null)
                {
                    // Generating a room
                    switch (direction % 4)
                    {
                    case South:
                        Generate(x, z - 1, North);
                        break;

                    case West:
                        Generate(x - 1, z, East);
                        break;

                    case North:
                        Generate(x, z + 1, South);
                        break;

                    case East:
                        Generate(x + 1, z, West);
                        break;
                    }
                }

                direction++;
            }
        }