Example #1
0
        public static RoomTemplateGrid2D ToOldRoomTemplate(this RoomTemplate roomTemplate)
        {
            var             doorMode    = roomTemplate.DoorsMode;
            IDoorModeGrid2D oldDoorMode = null;

            if (roomTemplate.RoomTemplateRepeatMode == null)
            {
                throw new NotSupportedException("Null repeat mode is currently not supported");
            }

            if (doorMode is SimpleDoorMode simpleDoorMode)
            {
                oldDoorMode = new SimpleDoorModeGrid2D(simpleDoorMode.DoorLength, simpleDoorMode.CornerDistance);
            }
            else if (doorMode is ManualDoorMode manualDoorMode)
            {
                oldDoorMode = new ManualDoorModeGrid2D(manualDoorMode.DoorPositions.Select(x => new DoorGrid2D(x.From, x.To)).ToList());
            }
            else
            {
                throw new ArgumentOutOfRangeException();
            }

            return(new RoomTemplateGrid2D(roomTemplate.Shape, oldDoorMode, allowedTransformations: roomTemplate.AllowedTransformations, repeatMode: roomTemplate.RoomTemplateRepeatMode, name: roomTemplate.Name));
        }
Example #2
0
    private RoomDescriptionGrid2D GetRoomDescription()
    {
        var roomsTemplates = new List <RoomTemplateGrid2D>();
        var doors          = new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 2);

        System.Random rnd             = new System.Random();
        var           roomWidthArray  = Enumerable.Range(roomMinWidth, roomMaxWidth - roomMinWidth + 1).ToArray();
        var           roomHieghtArray = Enumerable.Range(roomMinHeight, roomMaxHeight - roomMinHeight + 1).ToArray();

        var transformations = new List <TransformationGrid2D>()
        {
            TransformationGrid2D.Identity
        };

        for (var i = 0; i <= numberOfRooms; i++)
        {
            var roomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetRectangle(roomWidthArray.OrderBy(a => UnityEngine.Random.value).FirstOrDefault(), roomHieghtArray.OrderBy(a => UnityEngine.Random.value).FirstOrDefault()),
                doors,
                allowedTransformations: transformations);

            roomsTemplates.Add(roomTemplate);
        }

        var roomDescription = new RoomDescriptionGrid2D
                              (
            isCorridor: false,
            roomTemplates: roomsTemplates
                              );

        return(roomDescription);
    }
Example #3
0
    private RoomDescriptionGrid2D GetStartEndRoomDescription()
    {
        var roomsTemplates = new List <RoomTemplateGrid2D>();
        var doors          = new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 2);

        System.Random rnd = new System.Random();

        var transformations = new List <TransformationGrid2D>()
        {
            TransformationGrid2D.Identity
        };


        var roomTemplate = new RoomTemplateGrid2D(
            PolygonGrid2D.GetRectangle(roomMinWidth, roomMinHeight),
            doors,
            allowedTransformations: transformations);

        roomsTemplates.Add(roomTemplate);


        var roomDescription = new RoomDescriptionGrid2D
                              (
            isCorridor: false,
            roomTemplates: roomsTemplates
                              );

        return(roomDescription);
    }
        protected virtual RoomTemplateGrid2D GetRoomTemplate(PolygonGrid2D polygon, SimpleDoorModeGrid2D doorMode, string name)
        {
            var minScale = Math.Min(scale.X, scale.Y);

            doorMode = new SimpleDoorModeGrid2D(doorMode.DoorLength * minScale, doorMode.CornerDistance * minScale);

            return(new RoomTemplateGrid2D(polygon.Scale(scale), doorMode, allowedTransformations: transformations, name: name, repeatMode: repeatMode));
        }
        protected virtual List <RoomTemplateGrid2D> GetSmallRoomTemplates()
        {
            var roomTemplates = new List <RoomTemplateGrid2D>();
            var doorMode      = new SimpleDoorModeGrid2D(2, 1);

            roomTemplates.Add(GetSquareRoomTemplate(6, doorMode));
            roomTemplates.Add(GetSquareRoomTemplate(8, doorMode));
            roomTemplates.Add(GetRectangleRoomTemplate(6, 8, doorMode));

            return(roomTemplates);
        }
        protected virtual List <RoomTemplateGrid2D> GetMediumRoomTemplates()
        {
            var roomTemplates = new List <RoomTemplateGrid2D>();
            var doorMode      = new SimpleDoorModeGrid2D(2, 2);

            roomTemplates.Add(GetSquareRoomTemplate(12, doorMode));
            roomTemplates.Add(GetSquareRoomTemplate(14, doorMode));
            roomTemplates.Add(GetRectangleRoomTemplate(10, 14, doorMode));
            roomTemplates.Add(GetRectangleRoomTemplate(12, 15, doorMode));

            return(roomTemplates);
        }
Example #7
0
        private RoomDescriptionGrid2D GetBasicRoomDescription()
        {
            var doors           = new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1);
            var transformations = new List <TransformationGrid2D>()
            {
                TransformationGrid2D.Identity,
                TransformationGrid2D.Rotate90
            };

            var squareRoom1 = new RoomTemplateGrid2D(
                PolygonGrid2D.GetSquare(8),
                doors,
                name: "Square 8x8",
                allowedTransformations: transformations
                );

            var squareRoom2 = new RoomTemplateGrid2D(
                PolygonGrid2D.GetSquare(6),
                doors,
                name: "Square 6x6",
                allowedTransformations: transformations
                );

            var rectangleRoom = new RoomTemplateGrid2D(
                PolygonGrid2D.GetRectangle(6, 10),
                doors,
                name: "Rectangle 6x10",
                allowedTransformations: transformations
                );

            return(new RoomDescriptionGrid2D
                   (
                       isCorridor: false,
                       roomTemplates: new List <RoomTemplateGrid2D>()
            {
                squareRoom1,
                squareRoom2,
                rectangleRoom
            }
                   ));
        }
Example #8
0
        /// <summary>
        /// Prepare level description.
        /// </summary>
        public LevelDescriptionGrid2D <int> GetLevelDescription()
        {
            //md In this example, we will generate a very simple level consisting of 5 rooms with rectangular shapes.

            //md ## Room templates
            //md First, we will create our room templates. To do that, we need to create a *polygon* that defines the outline of the room template and also provide a list of possible door positions.

            //md ### Outline
            //md In the *Grid2D* setting, the outline of a room template is an orthogonal polygon where each point has integer coordinates. In other words, it is a polygon that we can draw on an integer grid using 1x1 square tiles.

            //md The first outline that we create is a 6x10 rectangle:

            var squareRoomOutline = new PolygonGrid2DBuilder()
                                    .AddPoint(0, 0)
                                    .AddPoint(0, 10)
                                    .AddPoint(6, 10)
                                    .AddPoint(6, 0)
                                    .Build();

            //md > **Note:** Orthogonal (or rectilinear) polygon is a polygon of whose edge intersections are at right angles. When on an integer grid, each side of an orthogonal polygon is aligned with one of the axes.

            //md > **Note:** There are several ways of constructing polygons:
            //md    - `PolygonGrid2D.GetSquare(width)` for squares
            //md    - `PolygonGrid2D.GetRectangle(width, height)` for rectangles
            //md    - `PolygonGrid2DBuilder` with the `.AddPoint(x, y)` method
            //md    - or the `PolygonGrid2D(IEnumerable<Vector2Int> points)` constructor

            //md ### Doors
            //md In order to tell the generator how it can connect individual room templates, we need to specify all the available door positions. The main idea is that the more door positions we provide, the easier it is for the generator to find a valid layout. To define door positions, we use the `IDoorModeGrid2D` interface. The most simple *door mode* is the `SimpleDoorModeGrid2D` - it lets us specify the length of doors and how far from corners of the outline they must be. In this tutorial, we will use doors with length of 1 tile and at least 1 tile away from corners.

            var doors = new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1);

            //md > **Note:** There is also an additional door mode available - `ManualDoorModeGrid2D`. This mode lets you specify exactly which door positions are available. It is useful for example when we want to have doors only on the two opposite sides of a corridor.

            //md ### Allowed transformations
            //md Optionally, it is also possible to let the generator apply some transformations to the room, e.g. rotate it by 90 degrees or mirror it by the X axis. An advantage of this approach is that the algorithm automatically handles door positions and we do not have to manually define all the variations of the room template.

            var transformations = new List <TransformationGrid2D>()
            {
                TransformationGrid2D.Identity,
                TransformationGrid2D.Rotate90
            };

            //md ### Putting it all together
            //md We can now combine the *outline*, *door mode* and *allowed transformations* together to create our first room template. We also provide a *name* which is optional but it may come in handy if we need to debug something.

            var rectangleRoomTemplate = new RoomTemplateGrid2D(
                squareRoomOutline,
                doors,
                allowedTransformations: transformations,
                name: "Rectangle 6x10"
                );

            //md We can also create a room template in-place with a single expression.

            var squareRoomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetSquare(8),
                new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1),
                name: "Square 8x8"
                );

            //md Below we can see a visualization of the two room templates. Individual door positions are shown in red.

            //md ![](./basics/room_templates.png)

            //md ## Room description
            //md When we have our room templates ready, we need to create an instance of the `RoomDescriptionGrid2D` class which describes the properties of individual rooms in the level. In this tutorial, all the rooms use the same pool of room templates, so we can create only a single room description and reuse it. However, it is also possible to use different room description for different types of rooms. For example, we may want to have a boss room and a spawn room that should use different room templates than normal rooms.

            var roomDescription = new RoomDescriptionGrid2D
                                  (
                isCorridor: false,
                roomTemplates: new List <RoomTemplateGrid2D>()
            {
                rectangleRoomTemplate, squareRoomTemplate
            }
                                  );

            //md ## Level description
            //md The final step is to describe the structure of the level. We will use a very simple graph of rooms that we can see below:

            //md ![](./basics/graph.png)

            //md First, we have to create an instance of the `LevelDescriptionGrid2D<TRoom>` class. For simplicity, We will use `integers` to identify individual rooms. But it is also possible to use a custom room type by using a different generic type parameter.

            var levelDescription = new LevelDescriptionGrid2D <int>();

            //md Next, we add individual rooms to the level description.

            levelDescription.AddRoom(0, roomDescription);
            levelDescription.AddRoom(1, roomDescription);
            levelDescription.AddRoom(2, roomDescription);
            levelDescription.AddRoom(3, roomDescription);
            levelDescription.AddRoom(4, roomDescription);

            //md And lastly, we describe how should individual rooms be connected.

            levelDescription.AddConnection(0, 1);
            levelDescription.AddConnection(0, 3);
            levelDescription.AddConnection(0, 4);
            levelDescription.AddConnection(1, 2);
            levelDescription.AddConnection(2, 3);


            //md_sc method_content:Run

            return(levelDescription);
        }
            protected override RoomTemplateGrid2D GetRectangleRoomTemplate(int width, int height, SimpleDoorModeGrid2D doorMode)
            {
                if (doorMode.CornerDistance >= 2)
                {
                    var polygon = new PolygonGrid2DBuilder()
                                  .AddPoint(2, 0)
                                  .AddPoint(2, 1)
                                  .AddPoint(1, 1)
                                  .AddPoint(1, 2)
                                  .AddPoint(0, 2)
                                  .AddPoint(0, height - 2)
                                  .AddPoint(1, height - 2)
                                  .AddPoint(1, height - 1)
                                  .AddPoint(2, height - 1)
                                  .AddPoint(2, height)
                                  .AddPoint(width - 2, height)
                                  .AddPoint(width - 2, height - 1)
                                  .AddPoint(width - 1, height - 1)
                                  .AddPoint(width - 1, height - 2)
                                  .AddPoint(width, height - 2)
                                  .AddPoint(width, 2)
                                  .AddPoint(width - 1, 2)
                                  .AddPoint(width - 1, 1)
                                  .AddPoint(width - 2, 1)
                                  .AddPoint(width - 2, 0)
                                  .Build();

                    return(GetRoomTemplate(polygon,
                                           new SimpleDoorModeGrid2D(doorMode.DoorLength, doorMode.CornerDistance - 2),
                                           $"Deformed {width}x{height}"));
                }
                else
                {
                    return(base.GetRectangleRoomTemplate(width, height, doorMode));
                }
            }
 protected virtual RoomTemplateGrid2D GetRectangleRoomTemplate(int width, int height, SimpleDoorModeGrid2D doorMode)
 {
     return(GetRoomTemplate(PolygonGrid2D.GetRectangle(width, height), doorMode, $"Rectangle {width}x{width}"));
 }
 protected virtual RoomTemplateGrid2D GetSquareRoomTemplate(int width, SimpleDoorModeGrid2D doorMode)
 {
     return(GetRectangleRoomTemplate(width, width, doorMode));
 }