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 }
public void Run() { var levelDescription = GetLevelDescription(); var generator = new GraphBasedGeneratorGrid2D <int>(levelDescription); var layout = generator.GenerateLayout(); var drawer = new DungeonDrawer <int>(); var bitmap = drawer.DrawLayout(layout, new DungeonDrawerOptions() { Height = 1000, Width = 1000, }); bitmap.Save("minimum_room_distance.png"); }
public void generateBasicRoom() { if (roomProperties.category == RoomProperty.RoomCategory.enemyRoom) { GetComponent <BoxCollider2D>().size = new Vector2(roomProperties.basicDim.x - 2, roomProperties.basicDim.y - 2); GetComponent <BoxCollider2D>().offset = new Vector2((float)roomProperties.basicDim.x / 2, (float)roomProperties.basicDim.y / 2); } else { GetComponent <BoxCollider2D>().enabled = false; } DungeonDrawer.drawRoom(dungeonAsset, new Vector2Int((int)transform.position.x, (int)transform.position.y), roomProperties, Utils.GlobalDirection.NORTH); generateDoors(); }
private void mainPictureBox_Paint(object sender, PaintEventArgs e) { if (layoutToDraw == null) { return; } if (exportShownLayoutsCheckbox.Checked) { DumpSvg(); } UpdateLayoutType(); var showNames = showRoomNamesCheckbox.Checked; var useOldPaperStyle = useOldPaperStyleCheckbox.Checked; var fixedFontSize = fixedFontSizeCheckbox.Checked ? (int?)fixedFontSizeValue.Value : null; if (firstChainToDraw == null) { firstChainToDraw = layoutToDraw; RecomputeFixedScaleAndOffset(); } else { var previousRoomsCount = firstChainToDraw.Rooms.Count(); var currentRoomsCount = layoutToDraw.Rooms.Count(); if (previousRoomsCount == currentRoomsCount) { firstChainToDraw = layoutToDraw; RecomputeFixedScaleAndOffset(); } } if (useOldPaperStyle) { //var bitmap = oldMapDrawer.DrawLayout(layoutToDraw, mainPictureBox.Width, mainPictureBox.Height, showNames, fixedFontSize); //e.Graphics.DrawImage(bitmap, new Point(0, 0)); throw new NotSupportedException(); } else { //Bitmap bitmap; //if (fixedPositionsAndScaleCheckbox.Checked) //{ // bitmap = wfLayoutDrawer.DrawLayout(layoutToDraw, mainPictureBox.Width, mainPictureBox.Height, showNames, fixedFontSize, fixedScale, fixedOffset); //} //else //{ // bitmap = wfLayoutDrawer.DrawLayout(layoutToDraw, mainPictureBox.Width, mainPictureBox.Height, showNames, fixedFontSize); //} var layoutDrawer = new DungeonDrawer <int>(); var bitmap = layoutDrawer.DrawLayout(layoutToDraw, new DungeonDrawerOptions() { Width = mainPictureBox.Width, Height = mainPictureBox.Height, ShowRoomNames = showNames, PaddingPercentage = 0.05f, EnableHatching = true, }); // e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.DrawImage(bitmap, new Point(0, 0)); } }
private void AddResults <TRoom>(StringBuilder output, IExampleGrid2D <TRoom> example) { var sourceCode = sourceCodeParser.GetMethod("GetResults", false); var results = example.GetResults().ToList(); var codeBlockHandler = new CodeBlockHandler(output); output.AppendLine("## Results"); output.AppendLine(); var resultsCounter = 0; foreach (var line in sourceCode) { var trimmed = line.Trim(); if (trimmed.StartsWith("//md")) { codeBlockHandler.Exit(); if (trimmed.Length > 5) { trimmed = trimmed.Remove(0, 5); } else { trimmed = trimmed.Remove(0, 4); } output.AppendLine(trimmed); } else if (trimmed.Contains("yield") && !trimmed.Contains("yield break")) { codeBlockHandler.Exit(); var levelDescription = results[resultsCounter]; var initStopwatch = new Stopwatch(); initStopwatch.Start(); var generator = new GraphBasedGeneratorGrid2D <TRoom>(levelDescription); generator.InjectRandomGenerator(new Random(0)); initStopwatch.Stop(); var layoutDrawer = new GraphBasedGenerator.Grid2D.Drawing.SVGLayoutDrawer <TRoom>(); var oldMapDrawer = new DungeonDrawer <TRoom>(); var times = new List <long>(); for (int i = 0; i < 4; i++) { var generatorStopwatch = new Stopwatch(); generatorStopwatch.Start(); var level = generator.GenerateLayout(); generatorStopwatch.Stop(); times.Add(generatorStopwatch.ElapsedMilliseconds); Console.WriteLine(generatorStopwatch.ElapsedMilliseconds + initStopwatch.ElapsedMilliseconds); var svg = layoutDrawer.DrawLayout(level, 800, forceSquare: true, flipY: true, fixedFontSize: 30); File.WriteAllText(Path.Combine(AssetsFolder, $"{resultsCounter}_{i}.svg"), svg); var bitmap = oldMapDrawer.DrawLayout(level, new DungeonDrawerOptions() { Width = 2000, Height = 2000, PaddingPercentage = 0.1f, }); bitmap.Save(Path.Combine(AssetsFolder, $"{resultsCounter}_{i}.png")); } var summaryDrawer = new GeneratorSummaryDrawer <TRoom>(); var summary = summaryDrawer.Draw(levelDescription, 5000, generator); summary.Save(Path.Combine(AssetsFolder, $"{resultsCounter}_summary.png")); output.AppendLine(); output.AppendLine("<Gallery cols={2}>"); for (int i = 0; i < 4; i++) { output.AppendLine( $"<GalleryImage src={{require('./{example.Options.DocsFileName}/{resultsCounter}_{i}.png').default}} />"); } output.AppendLine("</Gallery>"); output.AppendLine(); output.AppendLine("<div style={{ textAlign: 'center', marginTop: '-15px' }}>"); output.AppendLine(); output.AppendLine($"*Average time to generate the level: {((times.Average() + initStopwatch.ElapsedMilliseconds) / 1000).ToString("F", CultureInfo.InvariantCulture)}s ({((initStopwatch.ElapsedMilliseconds) / 1000d).ToString("F", CultureInfo.InvariantCulture)}s init, {((times.Average()) / 1000).ToString("F", CultureInfo.InvariantCulture)}s generation itself)*"); output.AppendLine(); output.AppendLine("</div>"); output.AppendLine(); resultsCounter++; } else if (codeBlockHandler.IsInside) { output.AppendLine(line); } else if (!string.IsNullOrEmpty(trimmed)) { codeBlockHandler.Enter(); output.AppendLine(line); } else { output.AppendLine(); } } codeBlockHandler.Exit(); }
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]. }
/// <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 }