Difference between revisions of "Using TiledLib"
(→Install TiledLib) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
=Pro's= | =Pro's= | ||
* Last update less than a year ago | * Last update less than a year ago | ||
− | |||
* Example of how to draw | * Example of how to draw | ||
=Con's= | =Con's= | ||
+ | * Complex Draw method required | ||
+ | * Doesn't cope well with multiple tilesets in a map | ||
* Poor readme with little information on how to use | * Poor readme with little information on how to use | ||
=Documentation= | =Documentation= | ||
https://github.com/Ragath/TiledLib.Net?tab=readme-ov-file | https://github.com/Ragath/TiledLib.Net?tab=readme-ov-file | ||
+ | |||
+ | =Drawing a Map= | ||
+ | You will need to create the following variables within `Game1.cs`: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | Map map; | ||
+ | Texture2D ts; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | You will then need to load these in, so add this code to the LoadContent method of the `Game1.cs`: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | string filename = "Content/real.tmx"; | ||
+ | var stream = File.OpenRead(filename); | ||
+ | map = Map.FromStream(stream, ts => File.OpenRead(Path.Combine(Path.GetDirectoryName(filename), ts.Source))); | ||
+ | ts = Content.Load<Texture2D>(map.Tilesets[0].ImagePath.Split(".")[0]); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Finally in the Draw method of `Game1.cs` add the following code: | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | foreach (var layer in map.Layers.OfType<TileLayer>()) | ||
+ | { | ||
+ | for (int y = 0, i = 0; y < layer.Height; y++) | ||
+ | for (int x = 0; x < layer.Width; x++, i++) | ||
+ | { | ||
+ | var gid = layer.Data[i]; | ||
+ | if (gid == 0) | ||
+ | continue; | ||
+ | else | ||
+ | { | ||
+ | var tileset = map.Tilesets.Single(ts => gid >= ts.FirstGid && ts.FirstGid + ts.TileCount > gid); | ||
+ | var tile = tileset[gid]; | ||
+ | |||
+ | Rectangle tilesetRec = new Rectangle(tile.Left, tile.Top, 32, 32); | ||
+ | |||
+ | _spriteBatch.Draw(ts, new Rectangle((int)x*32, (int)y*32, 32, 32), tilesetRec, Color.White); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> |
Latest revision as of 12:15, 24 May 2024
Install TiledLib
The easiest method is to create your MonoGame project then:
- Click on project & select Nuget Package Manager
- Search online for TiledLib & Install
- In the Game1.cs of your project add:
using TiledLib;
You can also get the source code from the link below, and copy the `.cs` files into the same folder as your `Game1.cs` or in a new folder within this.
Pro's
- Last update less than a year ago
- Example of how to draw
Con's
- Complex Draw method required
- Doesn't cope well with multiple tilesets in a map
- Poor readme with little information on how to use
Documentation
https://github.com/Ragath/TiledLib.Net?tab=readme-ov-file
Drawing a Map
You will need to create the following variables within `Game1.cs`:
Map map;
Texture2D ts;
You will then need to load these in, so add this code to the LoadContent method of the `Game1.cs`:
string filename = "Content/real.tmx";
var stream = File.OpenRead(filename);
map = Map.FromStream(stream, ts => File.OpenRead(Path.Combine(Path.GetDirectoryName(filename), ts.Source)));
ts = Content.Load<Texture2D>(map.Tilesets[0].ImagePath.Split(".")[0]);
Finally in the Draw method of `Game1.cs` add the following code:
foreach (var layer in map.Layers.OfType<TileLayer>())
{
for (int y = 0, i = 0; y < layer.Height; y++)
for (int x = 0; x < layer.Width; x++, i++)
{
var gid = layer.Data[i];
if (gid == 0)
continue;
else
{
var tileset = map.Tilesets.Single(ts => gid >= ts.FirstGid && ts.FirstGid + ts.TileCount > gid);
var tile = tileset[gid];
Rectangle tilesetRec = new Rectangle(tile.Left, tile.Top, 32, 32);
_spriteBatch.Draw(ts, new Rectangle((int)x*32, (int)y*32, 32, 32), tilesetRec, Color.White);
}
}
}