Playing with XNA yesterday made me wonder how easy it would be to build a Tile based game, such as a 2D ‘walk around’ adventure game (ie; Zelda, or the likes). I downloaded a copy of Tile Studio 2.55 not too long ago and have been staring at it’s icon on my desktop for quite some time now.
It came ‘out of the box’ with a handful of nice code generators, and it even included a .NET code generator … but it only supported the BooGame library, and generated Boo code.
So, I thought to myself, why not look for an XNA Exporter … so I loaded up the Tile Studio website, and found nothing there. I googled briefly and found nothing as well. So, I started on a little code adventure and decided to build my own. Now, keep in mind, I’m by no means an XNA Expert so my method for creating the tile map in C# may or may not be ‘the best’ or even ‘proper’. However, the map works … and I’m happy. I started on the project around 10:20am and am now writing this post at 11:50am … so, it wasn’t “too hard” and didn’t take too much time. The time spent also included learning the Tile Studio Definition language and how it worked as well.
So, onto some ‘proof’, here’s a few screenshots:
This is Tile Studio, with my little map loaded up
–the Map is 100×100, and the Tiles are 20×20.
Here is my Visual Studio project, after performing a code generation
in Tile Studio and refreshing the project directory and including the files.
And, here is my Map rendering to an XNA Game Window.
Now, in the project files shot you might notice that there are three files highlighted. The first is the exported Tile Map file from Tile Studio, the second is the actual Map definition (Tile2.cs) and the third is a helper class I wrote that is automatically exported for you that contains the MapDefinition and Tile classes.
And, here’s the best part, your not gonna believe it … or maybe you will. To load the map up, you simply do:
[csharp]//In Class scope
[csharp]// in LoadContent
map = new WatrRoad(Content.Load<Texture2D>(“WatrRoad”));[/csharp]
[csharp]// in Draw
And that’s it … the MapDefinition provides a ‘Draw’ method to loop through all the tiles and render them to the screen. I’ve not yet optimized this to store a cached copy, but I also haven’t tested it with any real ‘logic’ yet to try building a game. You can download a copy of the XNA C# Tile Studio Definition file from here, expect updates in the future to support more of Tile Studio’s feature set.
Currently Supported Features:
- Export Tile Sets to BMP and place in ‘Content’ sub-directory
- Export CS File containing all Maps
- Export TileStudioMap.cs containing MapDefinition and Tile helper classes
- Easy Map instantiation, just pass a reference to the Map’s Texture2D containing the Tiles
- Easy Map Rendering, just call the Map’s “Draw” method and pass the SpriteBatch