1
\$\begingroup\$

Using libgdx here. I've just finished learning some of the basics of creating a 2D environment and using an OrthographicCamera to view it. The tutorials I went through, however, hardcoded their tiled map in, and none made mention of how to do it any other way. By tiled map, I mean like Final Fantasy 1, where the world map is a grid of squares, each with a different texture.

So for example, I've got a 6 tile x 6 tile map, using the following code:

Array<Tile> tiles = new Array<Tile>();

tiles.add(new Tile(new Vector2(0,5), TileType.FOREST));
tiles.add(new Tile(new Vector2(1,5), TileType.FOREST));
tiles.add(new Tile(new Vector2(2,5), TileType.FOREST));
tiles.add(new Tile(new Vector2(3,5), TileType.GRASS));
tiles.add(new Tile(new Vector2(4,5), TileType.STONE));
tiles.add(new Tile(new Vector2(5,5), TileType.STONE));

//... x5 more times.

Given the random nature of the environment, for loops don't really help as I have to start and finish a loop before I was able to do enough to make it worth setting up the loop. I can see how a loop might be helpful for like tiling an ocean or something, but not in the above case.

The above code DOES get me my final desired output, however, if I were to decide I wanted to move a piece or swap two pieces out, oh boy, what a nightmare, even with just a 6x6 test piece, much less a 1000x1000 world map. There must be a better way of doing this.

Someone on some post somewhere (can't find it now, of course) said to check out MapEditor. Looks legit. The question is, if that is the answer, how can I make something in MapEditor and have the output map plug in to a variable in my code? I need the tiles as objects in my code, because for example, I determine whether or not a tile is can be passed through or collided with based on my TileTyle enum variable. Are there alternative/language "native" (i.e. not using an outside tool) methods to doing this?

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

I believe that Tiled Map Editor (TME) can create files of different types, however the main feature that caught my eye was it's ability to export to JSON files.

JSON is somewhat similar to XML and you will find good supporting APIs for parsing JSON files in Java. See here.

Once you have created a map in TME you will be able to export it as one of these JSON files. Then you will be able to load and parse that file to create java objects. You might find it convenient to parse to some intermediate set of java objects, then extract from them only the information you want and use it to make YOUR tile objects.

See this question for some more information. One answer mentions this tool which might help you out. This tool will allow you to generate the Java Object equivalent to the JSON objects created by TME.

\$\endgroup\$
1
  • \$\begingroup\$ +1 for recommending TME & GSON, which is a great library for creating Java objects from JSON. However, it may be expensive due to having to use reflection, but usually worth it . \$\endgroup\$ Commented Aug 17, 2012 at 18:08
7
\$\begingroup\$

A map editor of some sort is what you are going to need (and want!) when your game moves more to the level design phase from the initial engine/coding phase.

If you are using any kind of map/level editor, you will need to code a class (or set of classes) that can:

  1. Read/Input the Data
  2. Translate the data into actual objects (and sets of objects). Your tiles look simple now, so the map file would just be X, Y pairs with a tiletype name you can translate into a Tile enum.

You have to understand the editor output and figure out how it relates to your game objects. Creating an editor on your own helps with that, but you may get stuck implementing "basic" quality-of-life features like undo/redo and forget about the actual game! Those kinds of things are already implemented in a 3rd part editor.

If you do create your own, just get it to the point where you can place tiles on any size map you want, get some output, and then import that into your game. Implement additional features as you develop them in your game. Get something simple working first!

One important take-away is whatever solution you use, keep a layer between your actual level/tile objects and whatever map format you are using. That way, if you decide to change the map format, it won't impact your game code as much.

\$\endgroup\$
5
\$\begingroup\$

LibGDX has now added support for loading and rendering Tiled Map Editor's own .tmx file format. This should most certainly be your preferred method of loading and rendering TME created tile maps in LibGDX now, rather than having to manually parse through the JSON/XML data yourself.

Link to LibGDX tutorial on using .tmx tile maps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .