Difference between revisions of "Using TiledNet"
(→Drawing A Map) |
|||
Line 19: | Line 19: | ||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
− | foreach (var l in map.Layers) | + | foreach (var l in map.Layers) |
{ | { | ||
Revision as of 15:55, 13 February 2024
Install TiledNet
The easiest method is to create your MonoGame project then:
- Click on project & select Nuget Package Manager
- Search online for TiledNet & Install
- In the Game1.cs of your project add:
using Tiled;
Pro's
- Can Load & SAVE the map
Con's
- Archived project
- No example of how to draw the map
Documentation
https://github.com/napen123/Tiled.Net
Drawing A Map
Here is an example of the draw code required to render the map. The map doesn't seem to split the tile values into actual tiles it leaves them all in a single string.
foreach (var l in map.Layers)
{
if (l.GetType() == typeof(TiledTileLayer))
{
TiledTileLayer layer = l as TiledTileLayer;
string[] tiles = layer.Data.Data.Split(",");
tiles[0] = tiles[0].Substring(2);
for (int i = 0; i < tiles.Length; i++) // loop to cycle through each tile
{
int gid = Convert.ToInt32(tiles[i]);
// Empty tile, do nothing
if (gid == 0)
{
}
else
{
int tileFrame = gid-1;
int column = (tileFrame % 16);
int row = (int)Math.Floor((double)tileFrame / (double)16);
float x = ((i % map.Width)) * map.TileWidth;
float y = ((float)Math.Floor(i / (double)map.Width)) * map.TileHeight;
Rectangle tilesetRec = new Rectangle(map.TileWidth * column, map.TileHeight * row, map.TileWidth, map.TileHeight);
Game1._spriteBatch.Draw(tileset, new Rectangle((int)x, (int)y, map.TileWidth, map.TileHeight), tilesetRec, Color.White);
}
}
}
else if (l.GetType() == typeof(TiledObjectGroup))
{
TiledObjectGroup layer = l as TiledObjectGroup;
foreach(TiledObject to in layer.Objects)
{
_spriteBatch.Draw(pixel, new Rectangle((int)to.X, (int)to.Y, (int)to.Width, (int)to.Height), Color.White);
}
}
}