Implemeted dungeon map serialization.
This commit is contained in:
parent
fd2a28afe5
commit
0ff1e92fb9
9 changed files with 1267 additions and 6 deletions
|
|
@ -4,6 +4,9 @@ using DungeonMapGenerator;
|
|||
|
||||
class Program
|
||||
{
|
||||
private const string SAVED_DUNGEONS_PATH = @"C:\Users\User\repos\PuzzleGame\Dungeons";
|
||||
private const string DUNGEON_NAME = "dungeon";
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Create an instance of your DungeonMapGenerator class
|
||||
|
|
@ -11,7 +14,8 @@ class Program
|
|||
generator.GenerateDungeon(25, .5f);
|
||||
|
||||
// Call the method you want to run
|
||||
var map = generator.GenerateDungeon(25, .5f);
|
||||
DungeonMap map = generator.GenerateDungeon(25, .5f);
|
||||
DungeonMapSerializer.SerializeToFile(map, SAVED_DUNGEONS_PATH, DUNGEON_NAME );
|
||||
|
||||
// Print the map to the console (assuming it returns a string or something printable)
|
||||
Console.WriteLine(map.GetMapAsString());
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -2,25 +2,34 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DungeonMapGenerator
|
||||
{
|
||||
public class DungeonMap
|
||||
{
|
||||
[JsonProperty("Width")]
|
||||
private int _width;
|
||||
[JsonProperty("Height")]
|
||||
private int _height;
|
||||
[JsonProperty("MonsterRooms")]
|
||||
private List<Room> _monsterRooms = new List<Room>();
|
||||
[JsonProperty("EntranceRooms")]
|
||||
private List<Room> _entranceRooms = new List<Room>();
|
||||
[JsonProperty("NormalRooms")]
|
||||
private List<Room> _normalRooms = new List<Room>();
|
||||
[JsonProperty("BossRoom")]
|
||||
private Room _bossRoom;
|
||||
private HashSet<Point> _unoccupiedPoints = new HashSet<Point>();
|
||||
private HashSet<Point> _occupiedPoints = new HashSet<Point>();
|
||||
|
||||
public DungeonMap(){ }
|
||||
|
||||
public DungeonMap(int width, int height)
|
||||
{
|
||||
_width = width;
|
||||
this._width = width;
|
||||
_height = height;
|
||||
_unoccupiedPoints.AddRange(Enumerable.Range(0, _width)
|
||||
_unoccupiedPoints.AddRange(Enumerable.Range(0, this._width)
|
||||
.SelectMany(x => Enumerable.Range(0, _height)
|
||||
.Select(y => new Point(x,y))));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,25 @@ namespace DungeonMapGenerator
|
|||
public class DungeonMapSerializer
|
||||
{
|
||||
// Serialize DungeonMap to JSON file
|
||||
public static void SerializeToFile(DungeonMap dungeonMap, string filePath)
|
||||
public static void SerializeToFile(DungeonMap dungeonMap, string directoryPath, string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Convert DungeonMap object to JSON string
|
||||
string baseFilePath = Path.Combine(directoryPath, $"{name}.json");
|
||||
string filePath = baseFilePath;
|
||||
int counter = 1;
|
||||
|
||||
// Check if the file exists and rename it
|
||||
while (File.Exists(filePath))
|
||||
{
|
||||
filePath = Path.Combine(directoryPath, $"{name}{counter:00}.json"); // e.g., name01.json, name02.json
|
||||
counter++;
|
||||
}
|
||||
|
||||
// Serialize the object
|
||||
string json = JsonConvert.SerializeObject(dungeonMap, Formatting.Indented);
|
||||
|
||||
// Write JSON string to a file
|
||||
// Write JSON to the new file
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace DungeonMapGenerator
|
||||
{
|
||||
|
|
@ -11,6 +13,7 @@ namespace DungeonMapGenerator
|
|||
Left,
|
||||
Right
|
||||
}
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum RoomType
|
||||
{
|
||||
Normal,
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue