I'm working on the levels for my game so I've built these classes to read my maps:
Tiles.cs:
protected Texture2D texture;
private Rectangle rectangle;
public Rectangle Rectangle
{
get
{
return rectangle;
}
protected set
{
rectangle = value;
}
}
private static ContentManager content;
public static ContentManager Content
{
protected get
{
return content;
}
set
{
content = value;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Game1.DefaultColor);
}
CollisionTiles.cs:
public CollisionTiles(int i, Rectangle newRectangle)
{
texture = Content.Load<Texture2D>("Graphics/Tiles/tile_" + i);
Rectangle = newRectangle;
}
Map.cs:
private List<CollisionTiles> collisionTiles;
public List<CollisionTiles> CollisionTiles
{
get
{
return collisionTiles;
}
}
private int width, height;
public int Width
{
get
{
return width;
}
}
public int Height
{
get
{
return height;
}
}
public Map()
{
collisionTiles = new List<CollisionTiles>();
}
public void Generate(int[,] map, int size)
{
for (int x = 0; x < map.GetLength(1); x++)
{
for (int y = 0; y < map.GetLength(0); y++)
{
int number = map[y, x];
if (number > 0)
{
collisionTiles.Add(new CollisionTiles(number, new Rectangle(x * size, y * size, size, size)));
}
width = (x + 1) * size;
height = (y + 1) * size;
}
}
}
public int[,] LoadLevelData(string filename)
{
using (StreamReader streamReader = new StreamReader(filename))
{
JsonSerializer serializer = new JsonSerializer();
return (int[,])serializer.Deserialize(streamReader, typeof(int[,]));
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (CollisionTiles tile in collisionTiles)
{
tile.Draw(spriteBatch);
}
}
level_1.json:
[
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 2, 2],
[2, 2, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0, 2, 2],
[2, 2, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 2, 2],
[2, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2],
[2, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
]
I have the Newtonsoft.Json package added to my project. But since Monogame does not natively support JSON, rather its Content Pipeline supports XML. If I were to ship my game with the current level loading method, I would be forced to ship it with the levels as JSON files and not binary XNBs. That would enable users to edit the levels however they like and I don't want that.
So how can this code be converted to load XML levels instead of JSON? What approach would you take to deal with the problem? I've never used Monogame's/XNA's XML support in my projects before but using it for my levels would be a nice start. All help is appreciated.
Edit:
Usage:
int defaultTileSize, level_number;
Map map;
// constructor:
defaultTileSize = 64;
level_number = 1;
// LoadContent()
int[,] levelData = map.LoadLevelData("level_" + level_number + ".json");
map.Generate(levelData, defaultTileSize);
// Draw()
map.Draw(spriteBatch);