1
\$\begingroup\$

I want to draw line between two tile of my .tmx map in libgdx . I want to get a tile position but not in pixels , in maps grid numbers like : (0 , 6 ). or is there any way to change a tile color in libgdx. i can get tile like below:

layer=(TiledMapTileLayer)tiledMap.getLayers().get(0);
layer.getCell(row, col).getTile()

but can not to draw some thing on the tile.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You can use a ShapeRenderer to draw a line from one cell to another. If you set the Camera used by the ShapeRenderer then the coordinates will be tile coordinates (plus an adjustment of (0.5, 0.5) to get the center of the tile.

    shapeRenderer.setProjectionMatrix(camera.combined);
    shapeRenderer.begin(ShapeType.Line);
    shapeRenderer.setColor(Color.RED);
    Vector2 v0 = new Vector2(2, 4);
    Vector2 v1 = new Vector2(10, 8);
    shapeRenderer.line(
            v0.add(0.5f,  0.5f), // Add .5 to each coordinate to get the center of the tile 
            v1.add(0.5f,  0.5f));
    shapeRenderer.end();

To change the image of a TileMapTile an easy way is to replace the tile with another tile in the existing TileSet.

TiledMapTileLayer layer =  (TiledMapTileLayer)map.getLayers().get(0);
Cell cell = layer.getCell(1, 3);
TiledMapTile newTile = map.getTileSets().getTileSet(tilesetName).getTile(32);
cell.setTile(newTile);

This get the (relevant) MapLayer as a TiledMapTileLayer and get a Cell from that, then by getting a TiledMapTile using thegetTileSetsandgetTilemethods onTiledMap` a new tile can be set.

Complete libGDX example code (code assumes a map called test_map.tmx with a tileset called terrain in the assets folder).

    package com.bornander.sandbox;

    import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Input.Keys;
    import com.badlogic.gdx.assets.AssetManager;
    import com.badlogic.gdx.graphics.Color;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
    import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.maps.tiled.TiledMapTile;
    import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
    import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
    import com.badlogic.gdx.maps.tiled.TmxMapLoader;
    import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
    import com.badlogic.gdx.math.Vector2;

    public class SandboxGdxGame extends ApplicationAdapter {
        private AssetManager assetManager;
        private OrthographicCamera camera;
        private OrthogonalTiledMapRenderer mapRenderer;
        private TiledMap map;

        private ShapeRenderer shapeRenderer;

        @Override
        public void create () {
            String mapFile = "test_map.tmx"; 
            assetManager = new AssetManager();
            assetManager.setLoader(TiledMap.class, new TmxMapLoader());

             assetManager.load(mapFile, TiledMap.class);
             assetManager.finishLoading();
             map = (TiledMap)assetManager.get(mapFile);

            float viewportWidth = 32.0f; // Number of horizontal tiles to show
            float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
            camera = new OrthographicCamera(viewportWidth, viewportWidth / aspectRatio);
            camera.update();
            camera.position.set(camera.viewportWidth / 2.0f, camera.viewportHeight / 2.0f, 1.0f);

            float tileSizeInPixels = 32.0f;
            mapRenderer = new OrthogonalTiledMapRenderer(map, 1.0f / tileSizeInPixels);

            shapeRenderer = new ShapeRenderer();
        }

        @Override
        public void render () {
            Gdx.gl.glClearColor(1, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

            // When space is pressed, rewrite tile at (1,3) to be tile 32 from tileset 'tilesetName' 
            if (Gdx.input.isKeyJustPressed(Keys.SPACE) ) {
                String tilesetName = "terrain";
                TiledMapTileLayer layer =  (TiledMapTileLayer)map.getLayers().get(0);
                Cell cell = layer.getCell(1, 3);
                 TiledMapTile newTile = map.getTileSets().getTileSet(tilesetName).getTile(32);
                 cell.setTile(newTile);
            }

            // Render map
            camera.update();
            mapRenderer.setView(camera);
            mapRenderer.render();

            // Render line between center of tile (2,4) to center of tile (10, 8)
            shapeRenderer.setProjectionMatrix(camera.combined);
            shapeRenderer.begin(ShapeType.Line);
            shapeRenderer.setColor(Color.RED);
            Vector2 v0 = new Vector2(2, 4);
            Vector2 v1 = new Vector2(10, 8);
            shapeRenderer.line(
                    v0.add(0.5f,  0.5f), // Add .5 to each coordinate to get the center of the tile 
                    v1.add(0.5f,  0.5f));
            shapeRenderer.end();
        }
    }
\$\endgroup\$
0

You must log in to answer this question.

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