13
\$\begingroup\$

I'm creating a simple top down Zelda-esque game in libgdx, and I want to implement tile-based dungeons that are procedurally generated, similar to this.

Libgdx does have a class called TiledMap which would seem to fit my needs, however documentation outside of the official docs suggests that TiledMaps can only be used in conjunction with .tmx files, or in other words pre-designed maps. I've done a fair amount of Googling to no avail, so I'm asking here as a last resort hoping that someone has experience using libgdx for dynamically-generated tile maps.

edit: Could I use StaticTiledMapTiles in conjunction with the tutorial I linked to above to achieve my goal?

Thanks!

\$\endgroup\$
2
  • \$\begingroup\$ Can't you write the data of the generated maps to a .tmx file and use that in LibGdx? \$\endgroup\$
    – Dries
    Dec 28 '13 at 23:26
  • \$\begingroup\$ That's interesting. I suppose I could, although I feel like that wouldn't be ideal for quick generation. \$\endgroup\$
    – camden
    Dec 30 '13 at 1:14
9
\$\begingroup\$

You can create tiled maps on without using .tmx.

TiledMap map = new TileMap();
MapLayers layers = map.getLayers();

TiledMapTileLayer layer1 = new TiledMapTileLayer(width, height, tile_width, tile_height);
Cell cell = new Cell();

cell.setTile(new StaticTiledMapTile(texture_region));
layer1.setCell(x, y, cell);

layers.addLayer(layer1);

Each of these classes are in the docs for libgdx.
\$\endgroup\$
1
2
\$\begingroup\$

Based on advice from /r/gamedev, I've decided to forgo the use of libgdx's TiledMap, etc. built-in classes and just build my own.

I can go into greater detail if requested, but here's what I've done, essentially.

1: Generate a dungeon using the algorithm I originally linked to, and store tile coordinates (not pixel coords) using a Coordinate class I made in a HashMap<Coord, MapTile>. MapTile is just a class that has data about the type of tile.

2: In the draw() function of my main class I added the following code

ConcurrentHashMap<Coord, MapTile> dungeonMap = dungeonGen.getMap();
        for(Entry<Coord, MapTile> entry : dungeonMap.entrySet()){
            Coord coord = entry.getKey();
            MapTile tile = entry.getValue();

            if((inCameraFrustum(coord.getX() * tileSize, coord.getY() * tileSize, 100))){
                game.batch.draw(dungeonGen.getTileTexture(tile),
                        coord.getX() * tileSize, coord.getY() * tileSize);
            }

        }

Which iterates over every Coordinate in the dungeonMap and draws the tile (if the coordinate is in the camera frustum) with a global tileSize offset.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.