Exemplo n.º 1
0
        static void PlaceRooms(IMapGrid map)
        {
            for (int i = 0; i < m_params.RoomsNumber; i++) {
                List<MapGridTypes.Room> sills = GetRooms(map);
                sills.Shuffle();

                if (sills.Count == 0)
                    continue;

                MapGridTypes.Room r = sills.First();

                // alloc room
                for (int x = r.Pos.X - 1; x < (r.Pos.X + r.Width + 1); x++)
                    for (int y = r.Pos.Y - 1; y <  (r.Pos.Y + r.Height + 1); y++)
                        if (x == (r.Pos.X - 1) || x == (r.Pos.X + r.Width) ||
                            y == (r.Pos.Y - 1) || y == (r.Pos.Y + r.Height))
                            map.SetID (x, y, MapGridTypes.ID.Blocked);
                        else
                            map.SetID (x, y, MapGridTypes.ID.Room);

                // open room
                int doors = 2;
                while (doors > 0) {
                    Direction dir = Utils.RandomEnumValue<Direction>();
                    MapGridTypes.Door door = null;
                    if (dir == Direction.Right) { // right
                        int y = r.Pos.Y + Utils.Rand.Next(r.Height/2)*2;
                        if (map.GetID (r.Pos.X + r.Width + 1, y) == MapGridTypes.ID.Empty
                            && map.GetID (r.Pos.X + r.Width + 2, y) == MapGridTypes.ID.Empty
                            && map.GetID (r.Pos.X + r.Width, y - 1) != MapGridTypes.ID.Door
                            && map.GetID (r.Pos.X + r.Width, y + 1) != MapGridTypes.ID.Door) {
                            door = new MapGridTypes.Door (new Point(r.Pos.X + r.Width, y));
                        }
                    }
                    if (dir == Direction.Down) { // down
                        int x = r.Pos.X + Utils.Rand.Next(r.Width/2)*2;
                        if (map.GetID (x, r.Pos.Y + r.Height + 1) == MapGridTypes.ID.Empty
                            && map.GetID (x, r.Pos.Y + r.Height + 2) == MapGridTypes.ID.Empty
                            && map.GetID (x - 1, r.Pos.Y + r.Height) != MapGridTypes.ID.Door
                            && map.GetID (x + 1, r.Pos.Y + r.Height) != MapGridTypes.ID.Door) {
                            door = new MapGridTypes.Door (new Point (x, r.Pos.Y + r.Height));
                        }
                    }
                    if (dir == Direction.Left) { // left
                        int y = r.Pos.Y + Utils.Rand.Next(r.Height/2)*2;
                        if (map.GetID (r.Pos.X - 1, y) == MapGridTypes.ID.Empty
                            && map.GetID (r.Pos.X - 2, y) == MapGridTypes.ID.Empty
                            && map.GetID (r.Pos.X, y - 1) != MapGridTypes.ID.Door
                            && map.GetID (r.Pos.X, y + 1) != MapGridTypes.ID.Door) {
                            door = new MapGridTypes.Door (new Point (r.Pos.X, y));
                        }
                    }
                    if (dir == Direction.Up) { // up
                        int x = r.Pos.X + Utils.Rand.Next(r.Width/2)*2;
                        if (map.GetID (x, r.Pos.Y - 1) == MapGridTypes.ID.Empty
                            && map.GetID (x, r.Pos.Y - 2) == MapGridTypes.ID.Empty
                            && map.GetID (x - 1, r.Pos.Y) != MapGridTypes.ID.Door
                            && map.GetID (x + 1, r.Pos.Y) != MapGridTypes.ID.Door) {
                            door = new MapGridTypes.Door (new Point (x, r.Pos.Y));
                        }
                    }
                    if (door != null) {
                        r.Doors.Add(door);
                        map.SetID (door.Pos.X, door.Pos.Y, MapGridTypes.ID.Door);
                        doors--;
                    }
                }
                map.AddRoom(r);
            }
        }