Example #1
0
        public void Run()
        {
            // Create square room template
            var squareRoomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetSquare(8),
                new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1)
                );

            // Create rectangle room template
            var rectangleRoomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetRectangle(6, 10),
                new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1)
                );

            // Create a room description which says that the room is not a corridor and that it can use the two room templates
            var roomDescription = new RoomDescriptionGrid2D(
                isCorridor: false,
                roomTemplates: new List <RoomTemplateGrid2D>()
            {
                squareRoomTemplate, rectangleRoomTemplate
            }
                );

            // Create an instance of the level description
            var levelDescription = new LevelDescriptionGrid2D <int>();

            // Add 4 rooms to the level, use the room description that we created beforehand
            levelDescription.AddRoom(0, roomDescription);
            levelDescription.AddRoom(1, roomDescription);
            levelDescription.AddRoom(2, roomDescription);
            levelDescription.AddRoom(3, roomDescription);

            // Add connections between the rooms - the level graph will be a cycle with 4 vertices
            levelDescription.AddConnection(0, 1);
            levelDescription.AddConnection(0, 3);
            levelDescription.AddConnection(1, 2);
            levelDescription.AddConnection(2, 3);

            // Create an instance of the generate and generate a layout
            var generator = new GraphBasedGeneratorGrid2D <int>(levelDescription);
            var layout    = generator.GenerateLayout();

            // Export the resulting layout as PNG
            var drawer = new DungeonDrawer <int>();

            drawer.DrawLayoutAndSave(layout, "simple_layout.png", new DungeonDrawerOptions()
            {
                Width  = 2000,
                Height = 2000,
            });

            var layout2 = generator.GenerateLayout();

            // Export the resulting layout as PNG
            drawer.DrawLayoutAndSave(layout, "simple_layout_2.png", new DungeonDrawerOptions()
            {
                Width  = 2000,
                Height = 2000,
            });
        }
        /// <summary>
        /// Run the generator.
        /// </summary>
        public void Run()
        {
            var levelDescription = GetLevelDescription();

            var generator = new GraphBasedGeneratorGrid2D <Room>(levelDescription);
            var layout    = generator.GenerateLayout();

            var drawer = new DungeonDrawer <Room>();

            drawer.DrawLayoutAndSave(layout, "basics.png", new DungeonDrawerOptions()
            {
                Width  = 1000,
                Height = 1000,
            });

            #region no-clean

            var roomTemplates = levelDescription
                                .GetGraph().Vertices
                                .Select(levelDescription.GetRoomDescription)
                                .Where(x => x.IsCorridor == false)
                                .SelectMany(x => x.RoomTemplates)
                                .Distinct()
                                .ToList();
            var corridorRoomTemplates = levelDescription
                                        .GetGraph().Vertices
                                        .Select(levelDescription.GetRoomDescription)
                                        .Where(x => x.IsCorridor)
                                        .SelectMany(x => x.RoomTemplates)
                                        .Distinct()
                                        .ToList();
            var roomTemplatesDrawer = new RoomTemplateDrawer();
            var roomTemplatesBitmap = roomTemplatesDrawer.DrawRoomTemplates(roomTemplates, new DungeonDrawerOptions()
            {
                Width             = 2000,
                Height            = 1600,
                PaddingPercentage = 0.05f,
                FontSize          = 2,
                EnableHatching    = false,
            });
            roomTemplatesBitmap.Save(ExamplesGenerator.AssetsFolder + "/room_templates.png");

            var corridorRoomTemplatesBitmap = roomTemplatesDrawer.DrawRoomTemplates(corridorRoomTemplates, new DungeonDrawerOptions()
            {
                Width           = 1200,
                Height          = 350,
                FontSize        = 0.7f,
                PaddingAbsolute = 100,
                ShowRoomNames   = false,
                EnableHatching  = false,
            });
            corridorRoomTemplatesBitmap.Save(ExamplesGenerator.AssetsFolder + "/corridor_room_templates.png");

            #endregion
        }
Example #3
0
        public void Run()
        {
            #region hidden

            var levelDescription = new BasicsExample().GetLevelDescription();

            #endregion

            //md In this tutorial, we will see how to save and load level descriptions and generated layouts.

            //md ## Level descriptions (JSON)
            //md To save a level description to a JSON file, we can call the [`SaveToJson()`][LevelDescriptionGrid2D.SaveToJson(String, Boolean)] method:

            levelDescription.SaveToJson("levelDescription.json");

            //md And to load a level description from a JSON file, we can use the [`LoadFromJson()`][LevelDescriptionGrid2D.LoadFromJson(String)] method:

            levelDescription = LevelDescriptionGrid2D <int> .LoadFromJson("levelDescription.json");

            //md By default, the JSON serializer is configured to preserve references to objects. That means that when it first encounters an object, it assigns a unique id to id and when it encounters the same object later, it only references that id and does not serialize the object itself. This is good for when we want load the level description back in C# later, but it may cause problems if we want to use the JSON outside C#. Therefore, it is possible to disable this feature:

            levelDescription.SaveToJson("levelDescription.json", preserveReferences: false);

            //md > **Note:**: API reference for the `LevelDescriptionGrid2D` class can be found [here][LevelDescriptionGrid2D].

            //md ## Layouts (JSON)

            #region hidden

            levelDescription = new BasicsExample().GetLevelDescription();
            var generator = new GraphBasedGeneratorGrid2D <int>(levelDescription);
            var layout    = generator.GenerateLayout();

            #endregion

            //md It is possible to save a generated layout to JSON and then load it back:

            layout.SaveToJson("layout.json");
            layout = LayoutGrid2D <int> .LoadFromJson("layout.json");

            //md And it is also possible to disable the `preserveReferences` feature:

            layout.SaveToJson("layout.json", preserveReferences: false);

            //md > **Note:**: API reference for the `LayoutGrid2D` class can be found [here][LayoutGrid2D].

            //md ## Layouts (PNG)

            #region hidden

            layout = generator.GenerateLayout();

            #endregion

            //md It is possible to save a generated layout as a PNG. To do that, we have to create an instance of the `DungeonDrawer` class:

            var dungeonDrawer = new DungeonDrawer <int>();

            //md Then we can save the layout as a PNG:

            dungeonDrawer.DrawLayoutAndSave(layout, "dungeon.png", new DungeonDrawerOptions()
            {
                Width  = 2000,
                Height = 2000,
            });

            //md The dungeon drawer produces images that can be seen in all the examples in this documentation. The API reference of the `DungeonDrawerOptions` class can be found [here][DungeonDrawerOptions].
        }
Example #4
0
        /// <summary>
        /// Run the generator and export the result.
        /// </summary>
        public void Run()
        {
            //md ## Generating the level
            //md To generate the level, we need to create an instance of the `GraphBasedGenerator<TRoom>` class. As we use integers to identify individual rooms, we will substitute the generic type parameter with `int` and pass the level description to the constructor of the generator.

            //md_hide-next
            var levelDescription = GetLevelDescription();

            var generator = new GraphBasedGeneratorGrid2D <int>(levelDescription);

            //md When we have an instance of the generator, we simply call the `GenerateLayout()` method and wait until the generator finds a valid layout based on our level description.

            var layout = generator.GenerateLayout();

            //md The result contains information about all the rooms in the level such as outline of the room or its position.

            //md ## Saving the result
            //md If we want to quickly visualize the result, we can use the `DungeonDrawer<TRoom>` class and export the layout as a PNG image.

            var drawer = new DungeonDrawer <int>();

            drawer.DrawLayoutAndSave(layout, "basics.png", new DungeonDrawerOptions()
            {
                Width  = 2000,
                Height = 2000,
            });

            #region hidden no-clean

            var roomTemplates = levelDescription
                                .GetGraph().Vertices
                                .Select(levelDescription.GetRoomDescription)
                                .Where(x => x.IsCorridor == false)
                                .SelectMany(x => x.RoomTemplates)
                                .Distinct()
                                .ToList();
            var roomTemplatesDrawer = new RoomTemplateDrawer();
            var roomTemplatesBitmap = roomTemplatesDrawer.DrawRoomTemplates(roomTemplates, new DungeonDrawerOptions()
            {
                Width           = 1200,
                Height          = 600,
                PaddingAbsolute = 100,
                FontSize        = 1,
                EnableHatching  = false
            });
            roomTemplatesBitmap.Save(ExamplesGenerator.AssetsFolder + "/room_templates.png");

            var graphDrawer = new GraphDrawer <int>();
            var graphBitmap = graphDrawer.DrawGraph(levelDescription, new Dictionary <int, Vector2Int>()
            {
                { 0, new Vector2Int(0, 0) },
                { 1, new Vector2Int(0, -1) },
                { 2, new Vector2Int(1, -1) },
                { 3, new Vector2Int(1, 0) },
                { 4, new Vector2Int(-1, 0) },
            }, new DungeonDrawerOptions()
            {
                Width           = 2500,
                Height          = 700,
                PaddingAbsolute = 80,
                FontSize        = 3,
            });
            graphBitmap.Save(ExamplesGenerator.AssetsFolder + "/graph.png");

            #endregion
        }