Difference between revisions of "Using TiledLib"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Pro's)
(Pro's)
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
# Search online for TiledLib & Install
 
# Search online for TiledLib & Install
 
# In the Game1.cs of your project add: <syntaxhighlight lang=csharp>using TiledLib;</syntaxhighlight>
 
# In the Game1.cs of your project add: <syntaxhighlight lang=csharp>using TiledLib;</syntaxhighlight>
 +
 +
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=
 
=Pro's=
 
* Last update less than a year ago
 
* Last update less than a year ago
* Available as a NuGet package
 
 
* Example of how to draw
 
* Example of how to draw
  
Line 15: Line 16:
 
=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:32, 14 February 2024

Install TiledLib

The easiest method is to create your MonoGame project then:

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

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