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
|
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)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
// Create an instance of your DungeonMapGenerator class
|
// Create an instance of your DungeonMapGenerator class
|
||||||
|
|
@ -11,7 +14,8 @@ class Program
|
||||||
generator.GenerateDungeon(25, .5f);
|
generator.GenerateDungeon(25, .5f);
|
||||||
|
|
||||||
// Call the method you want to run
|
// 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)
|
// Print the map to the console (assuming it returns a string or something printable)
|
||||||
Console.WriteLine(map.GetMapAsString());
|
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.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace DungeonMapGenerator
|
namespace DungeonMapGenerator
|
||||||
{
|
{
|
||||||
public class DungeonMap
|
public class DungeonMap
|
||||||
{
|
{
|
||||||
|
[JsonProperty("Width")]
|
||||||
private int _width;
|
private int _width;
|
||||||
|
[JsonProperty("Height")]
|
||||||
private int _height;
|
private int _height;
|
||||||
|
[JsonProperty("MonsterRooms")]
|
||||||
private List<Room> _monsterRooms = new List<Room>();
|
private List<Room> _monsterRooms = new List<Room>();
|
||||||
|
[JsonProperty("EntranceRooms")]
|
||||||
private List<Room> _entranceRooms = new List<Room>();
|
private List<Room> _entranceRooms = new List<Room>();
|
||||||
|
[JsonProperty("NormalRooms")]
|
||||||
private List<Room> _normalRooms = new List<Room>();
|
private List<Room> _normalRooms = new List<Room>();
|
||||||
|
[JsonProperty("BossRoom")]
|
||||||
private Room _bossRoom;
|
private Room _bossRoom;
|
||||||
private HashSet<Point> _unoccupiedPoints = new HashSet<Point>();
|
private HashSet<Point> _unoccupiedPoints = new HashSet<Point>();
|
||||||
private HashSet<Point> _occupiedPoints = new HashSet<Point>();
|
private HashSet<Point> _occupiedPoints = new HashSet<Point>();
|
||||||
|
|
||||||
|
public DungeonMap(){ }
|
||||||
|
|
||||||
public DungeonMap(int width, int height)
|
public DungeonMap(int width, int height)
|
||||||
{
|
{
|
||||||
_width = width;
|
this._width = width;
|
||||||
_height = height;
|
_height = height;
|
||||||
_unoccupiedPoints.AddRange(Enumerable.Range(0, _width)
|
_unoccupiedPoints.AddRange(Enumerable.Range(0, this._width)
|
||||||
.SelectMany(x => Enumerable.Range(0, _height)
|
.SelectMany(x => Enumerable.Range(0, _height)
|
||||||
.Select(y => new Point(x,y))));
|
.Select(y => new Point(x,y))));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,25 @@ namespace DungeonMapGenerator
|
||||||
public class DungeonMapSerializer
|
public class DungeonMapSerializer
|
||||||
{
|
{
|
||||||
// Serialize DungeonMap to JSON file
|
// Serialize DungeonMap to JSON file
|
||||||
public static void SerializeToFile(DungeonMap dungeonMap, string filePath)
|
public static void SerializeToFile(DungeonMap dungeonMap, string directoryPath, string name)
|
||||||
{
|
{
|
||||||
try
|
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);
|
string json = JsonConvert.SerializeObject(dungeonMap, Formatting.Indented);
|
||||||
|
|
||||||
// Write JSON string to a file
|
// Write JSON to the new file
|
||||||
File.WriteAllText(filePath, json);
|
File.WriteAllText(filePath, json);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
namespace DungeonMapGenerator
|
namespace DungeonMapGenerator
|
||||||
{
|
{
|
||||||
|
|
@ -11,6 +13,7 @@ namespace DungeonMapGenerator
|
||||||
Left,
|
Left,
|
||||||
Right
|
Right
|
||||||
}
|
}
|
||||||
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
public enum RoomType
|
public enum RoomType
|
||||||
{
|
{
|
||||||
Normal,
|
Normal,
|
||||||
|
|
|
||||||
Binary file not shown.
1234
Dungeons/dungeon.json
Normal file
1234
Dungeons/dungeon.json
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue