Difference between revisions of "Using a tmx map in monogame"
(→Load Content) |
|||
Line 31: | Line 31: | ||
tilesetTilesWide = tileset.Width / tileWidth; | tilesetTilesWide = tileset.Width / tileWidth; | ||
tilesetTilesHigh = tileset.Height / tileHeight; | tilesetTilesHigh = tileset.Height / tileHeight; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | =Draw the Map= | ||
+ | In the draw method, add the following code to draw each tile of the map: | ||
+ | <syntaxhighlight lang=csharp> | ||
+ | spriteBatch.Begin(); | ||
+ | |||
+ | for (var i = 0; i < map.Layers[0].Tiles.Count; i++) | ||
+ | { | ||
+ | int gid = map.Layers[0].Tiles[i].Gid; | ||
+ | |||
+ | // Empty tile, do nothing | ||
+ | if (gid == 0) | ||
+ | { | ||
+ | |||
+ | } | ||
+ | else | ||
+ | { | ||
+ | int tileFrame = gid - 1; | ||
+ | int column = tileFrame % tilesetTilesWide; | ||
+ | int row = (int)Math.Floor((double)tileFrame / (double)tilesetTilesWide); | ||
+ | |||
+ | float x = (i % map.Width) * map.TileWidth; | ||
+ | float y = (float)Math.Floor(i / (double)map.Width) * map.TileHeight; | ||
+ | |||
+ | Rectangle tilesetRec = new Rectangle(tileWidth * column, tileHeight * row, tileWidth, tileHeight); | ||
+ | |||
+ | spriteBatch.Draw(tileset, new Rectangle((int)x, (int)y, tileWidth, tileHeight), tilesetRec, Color.White); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | spriteBatch.End(); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 14:10, 23 September 2017
Install TiledSharp
The easiest method is to create your MonoGame project then:
- Click on project & select Nuget Package Manager
- Search online for TiledSharp & Install
- In the Game1.cs of your project add
using TiledSharp;
Declarations
Add the following declaration within the main class in Game1.cs:
TmxMap map;
Texture2D tileset;
int tileWidth;
int tileHeight;
int tilesetTilesWide;
int tilesetTilesHigh;
Load Content
Download the files here. You will need to copy these into the content folder of your project, you will also need to add the png file to the content pipeline and build it. Without the xnb file an error will be thrown.
In the LoadContent method add the following to load your map & tiles, it also sets the size of the tiles:
map = new TmxMap("Content/exampleMap.tmx");
tileset = Content.Load<Texture2D>(map.Tilesets[0].Name.ToString());
tileWidth = map.Tilesets[0].TileWidth;
tileHeight = map.Tilesets[0].TileHeight;
tilesetTilesWide = tileset.Width / tileWidth;
tilesetTilesHigh = tileset.Height / tileHeight;
Draw the Map
In the draw method, add the following code to draw each tile of the map:
spriteBatch.Begin();
for (var i = 0; i < map.Layers[0].Tiles.Count; i++)
{
int gid = map.Layers[0].Tiles[i].Gid;
// Empty tile, do nothing
if (gid == 0)
{
}
else
{
int tileFrame = gid - 1;
int column = tileFrame % tilesetTilesWide;
int row = (int)Math.Floor((double)tileFrame / (double)tilesetTilesWide);
float x = (i % map.Width) * map.TileWidth;
float y = (float)Math.Floor(i / (double)map.Width) * map.TileHeight;
Rectangle tilesetRec = new Rectangle(tileWidth * column, tileHeight * row, tileWidth, tileHeight);
spriteBatch.Draw(tileset, new Rectangle((int)x, (int)y, tileWidth, tileHeight), tilesetRec, Color.White);
}
}
spriteBatch.End();