private LevelDescriptionGrid2D <RoomWrapper> GetWrappedLevelDescription(LevelDescriptionGrid2D <RoomBase> originalLevelDescription)
        {
            var levelDescription = new LevelDescriptionGrid2D <RoomWrapper>();

            var srcProperties = originalLevelDescription.GetType().GetProperties(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
            var dstProperties = levelDescription.GetType().GetProperties(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

            foreach (var srcProperty in srcProperties)
            {
                var dstProperty = dstProperties.First(x => x.Name == srcProperty.Name);

                if (dstProperty.CanWrite)
                {
                    dstProperty.SetValue(levelDescription, srcProperty.GetValue(originalLevelDescription));
                }
            }

            var id      = 0;
            var mapping = originalLevelDescription
                          .GetGraphWithoutCorridors()
                          .Vertices
                          .Select(x => (x, new RoomWrapper(id++, x.GetDisplayName())))
                          .ToDictionary(x => x.x, x => x.Item2);

            foreach (var pair in mapping)
            {
                levelDescription.AddRoom(pair.Value, originalLevelDescription.GetRoomDescription(pair.Key));
            }

            foreach (var edge in originalLevelDescription.GetGraphWithoutCorridors().Edges)
            {
                var from = mapping[edge.From];
                var to   = mapping[edge.To];

                levelDescription.AddConnection(from, to);
            }

            return(levelDescription);
        }
Esempio n. 2
0
        public static IMapDescription <TNode> GetMapDescription <TNode>(this LevelDescriptionGrid2D <TNode> levelDescription)
        {
            var mapDescription = new MapDescription <TNode>();
            var graph          = levelDescription.GetGraph();

            var corridorRoomDescriptions = new Dictionary <RoomDescriptionGrid2D, CorridorRoomDescription>();
            var roomTemplateMapping      = new Dictionary <RoomTemplateGrid2D, RoomTemplate>();

            foreach (var room in graph.Vertices)
            {
                var roomDescription = levelDescription.GetRoomDescription(room);

                if (roomDescription.IsCorridor)
                {
                    if (corridorRoomDescriptions.TryGetValue(roomDescription, out var cached))
                    {
                        mapDescription.AddRoom(room, cached);
                    }
                    else
                    {
                        var corridorRoomDescription = new CorridorRoomDescription(roomDescription.RoomTemplates.Select(x => GetOldRoomTemplate(x, roomTemplateMapping)).ToList());
                        corridorRoomDescriptions[roomDescription] = corridorRoomDescription;
                        mapDescription.AddRoom(room, corridorRoomDescription);
                    }
                }
                else
                {
                    mapDescription.AddRoom(room, new BasicRoomDescription(roomDescription.RoomTemplates.Select(x => GetOldRoomTemplate(x, roomTemplateMapping)).ToList()));
                }
            }

            foreach (var edge in graph.Edges)
            {
                mapDescription.AddConnection(edge.From, edge.To);
            }

            return(mapDescription);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public LayoutGrid2D <TNode> Convert(ILayout <RoomNode <TNode>, TConfiguration> layout, bool addDoors)
        {
            var rooms     = new List <LayoutRoomGrid2D <TNode> >();
            var roomsDict = new Dictionary <TNode, LayoutRoomGrid2D <TNode> >();

            foreach (var vertexAlias in layout.Graph.Vertices)
            {
                if (layout.GetConfiguration(vertexAlias, out var configuration))
                {
                    var vertex = vertexAlias.Room;
                    var roomTemplateInstance = configuration.RoomShape;

                    // Make sure that the returned shape has the same position as the original room template shape and is not moved to (0,0)
                    // TODO: maybe make a unit/integration test?
                    var transformation   = roomTemplateInstance.Transformations.GetRandom(Random);
                    var shape            = configuration.RoomShape.RoomShape;
                    var originalShape    = roomTemplateInstance.RoomTemplate.Outline;
                    var transformedShape = originalShape.Transform(transformation);
                    var offset           = transformedShape.BoundingRectangle.A - shape.BoundingRectangle.A;

                    var room = new LayoutRoomGrid2D <TNode>(vertex, transformedShape, configuration.Position - offset, MapDescription.GetRoomDescription(vertexAlias.Room).IsCorridor, roomTemplateInstance.RoomTemplate, MapDescription.GetRoomDescription(vertexAlias.Room), transformation);
                    rooms.Add(room);

                    if (!addDoors)
                    {
                        continue;
                    }

                    var doors = new List <LayoutDoorGrid2D <TNode> >();
                    room.Doors = doors;

                    roomsDict[vertex] = room;
                }
            }

            if (addDoors)
            {
                var generatedDoors = new HashSet <Tuple <TNode, TNode> >();

                foreach (var vertexAlias in layout.Graph.Vertices)
                {
                    var vertex = vertexAlias.Room;

                    if (layout.GetConfiguration(vertexAlias, out var configuration))
                    {
                        var neighbours = layout.Graph.GetNeighbours(vertexAlias);

                        foreach (var neighbourAlias in neighbours)
                        {
                            var neighbour = neighbourAlias.Room;

                            if (layout.GetConfiguration(neighbourAlias, out var neighbourConfiguration) && !generatedDoors.Contains(Tuple.Create(neighbour, vertex)))
                            {
                                var doorChoices  = GetDoors(configuration, neighbourConfiguration);
                                var randomChoice = doorChoices.GetRandom(Random);

                                roomsDict[vertex].Doors.Add(new LayoutDoorGrid2D <TNode>(vertex, neighbour, randomChoice + -1 * roomsDict[vertex].Position));
                                roomsDict[neighbour].Doors.Add(new LayoutDoorGrid2D <TNode>(neighbour, vertex, randomChoice + -1 * roomsDict[neighbour].Position));
                                generatedDoors.Add(Tuple.Create(vertex, neighbour));
                            }
                        }
                    }
                }
            }

            return(new LayoutGrid2D <TNode>(rooms));
        }