Difference between revisions of "Using TiledNet"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Drawing A Map)
(Drawing A Map)
Line 64: Line 64:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
To fix this you could construct the Tiles data in load content using something like this:
+
To fix this you could construct the Tiles data in the LoadContent method using something like this:
  
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>

Revision as of 17:07, 13 February 2024

Install TiledNet

The easiest method is to create your MonoGame project then:

  1. Click on project & select Nuget Package Manager
  2. Search online for TiledNet & Install
  3. 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);
                    }
                }
            }

To fix this you could construct the Tiles data in the LoadContent method using something like this:

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);
                    foreach(string i in tiles)
                    {
                        TiledTileData temp = new TiledTileData();
                        temp.GlobalId = Convert.ToInt32(i);
                        layer.Data.Tiles.Add(temp); 
                    }
                }
            }

Then the draw method could be just this:

            foreach (var l in map.Layers)
            {
                if (l.GetType() == typeof(TiledTileLayer))
                {
                    TiledTileLayer layer = l as TiledTileLayer;

                    for (int i = 0; i < layer.Data.Tiles.Count; i++) // loop to cycle through each tile
                    {
                        int gid = Convert.ToInt32(layer.Data.Tiles[i].GlobalId);

                        // 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);
                    }
                }
            }