Search in sources :

Example 1 with HexagonalTiledMapRenderer

use of com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer in project libgdx by libgdx.

the class HexagonalTiledMapTest method create.

@Override
public void create() {
    super.create();
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 480, 480);
    camera.update();
    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);
    hexture = new Texture(Gdx.files.internal("data/maps/tiled/hex/hexes.png"));
    TextureRegion[][] hexes = TextureRegion.split(hexture, 112, 97);
    map = new TiledMap();
    MapLayers layers = map.getLayers();
    TiledMapTile[] tiles = new TiledMapTile[3];
    tiles[0] = new StaticTiledMapTile(new TextureRegion(hexes[0][0]));
    tiles[1] = new StaticTiledMapTile(new TextureRegion(hexes[0][1]));
    tiles[2] = new StaticTiledMapTile(new TextureRegion(hexes[1][0]));
    for (int l = 0; l < 1; l++) {
        TiledMapTileLayer layer = new TiledMapTileLayer(45, 30, 112, 97);
        for (int y = 0; y < 30; y++) {
            for (int x = 0; x < 45; x++) {
                int id = (int) (Math.random() * 3);
                Cell cell = new Cell();
                cell.setTile(tiles[id]);
                layer.setCell(x, y, cell);
            }
        }
        layers.add(layer);
    }
    renderer = new HexagonalTiledMapRenderer(map);
}
Also used : OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) OrthoCamController(com.badlogic.gdx.tests.utils.OrthoCamController) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) TiledMap(com.badlogic.gdx.maps.tiled.TiledMap) HexagonalTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) MapLayers(com.badlogic.gdx.maps.MapLayers)

Example 2 with HexagonalTiledMapRenderer

use of com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer in project libgdx by libgdx.

the class TiledMapLayerOffsetTest method render.

@Override
public void render() {
    if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)) {
        if (mapType != 0) {
            if (renderer instanceof Disposable)
                ((Disposable) renderer).dispose();
            mapType = 0;
            map = assetManager.get(MAP_ORTHO);
            renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);
        }
    } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)) {
        if (mapType != 1) {
            if (renderer instanceof Disposable)
                ((Disposable) renderer).dispose();
            mapType = 1;
            map = assetManager.get(MAP_ORTHO);
            renderer = new OrthoCachedTiledMapRenderer(map, 1f / 32f);
            ((OrthoCachedTiledMapRenderer) renderer).setBlending(true);
        }
    } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_3)) {
        if (mapType != 2) {
            if (renderer instanceof Disposable)
                ((Disposable) renderer).dispose();
            mapType = 2;
            map = assetManager.get(MAP_ISO);
            renderer = new IsometricTiledMapRenderer(map, 1f / 48f);
        }
    } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_4)) {
        if (mapType != 3) {
            if (renderer instanceof Disposable)
                ((Disposable) renderer).dispose();
            mapType = 3;
            map = assetManager.get(MAP_ISO_STAG);
            renderer = new IsometricStaggeredTiledMapRenderer(map, 1f / 48f);
        }
    } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_5)) {
        if (mapType != 4) {
            if (renderer instanceof Disposable)
                ((Disposable) renderer).dispose();
            mapType = 4;
            map = assetManager.get(MAP_HEX_X);
            renderer = new HexagonalTiledMapRenderer(map, 1f / 48f);
        }
    } else if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_6)) {
        if (mapType != 5) {
            if (renderer instanceof Disposable)
                ((Disposable) renderer).dispose();
            mapType = 5;
            map = assetManager.get(MAP_HEX_Y);
            renderer = new HexagonalTiledMapRenderer(map, 1f / 48f);
        }
    }
    Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    // add margin to view bounds so it is easy to see any issues with clipping, calculated same way as
    // BatchTiledMapRenderer#setView (OrthographicCamera)
    final float margin = 3;
    final float width = camera.viewportWidth * camera.zoom - margin * 2;
    final float height = camera.viewportHeight * camera.zoom - margin * 2;
    final float w = width * Math.abs(camera.up.y) + height * Math.abs(camera.up.x);
    final float h = height * Math.abs(camera.up.y) + width * Math.abs(camera.up.x);
    final float x = camera.position.x - w / 2;
    final float y = camera.position.y - h / 2;
    renderer.setView(camera.combined, x, y, w, h);
    renderer.render();
    shapeRenderer.setProjectionMatrix(camera.combined);
    shapeRenderer.begin(ShapeType.Line);
    shapeRenderer.setColor(Color.RED);
    shapeRenderer.rect(x, y, w, h);
    shapeRenderer.end();
    batch.begin();
    font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
    font.draw(batch, "Switch type with 1-6", Gdx.graphics.getHeight() - 100, 50);
    font.draw(batch, renderer.getClass().getSimpleName(), Gdx.graphics.getHeight() - 100, 20);
    batch.end();
}
Also used : Disposable(com.badlogic.gdx.utils.Disposable) OrthogonalTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer) IsometricTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer) IsometricStaggeredTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.IsometricStaggeredTiledMapRenderer) OrthoCachedTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.OrthoCachedTiledMapRenderer) HexagonalTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer)

Aggregations

HexagonalTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer)2 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 Texture (com.badlogic.gdx.graphics.Texture)1 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)1 MapLayers (com.badlogic.gdx.maps.MapLayers)1 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)1 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)1 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)1 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)1 IsometricStaggeredTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.IsometricStaggeredTiledMapRenderer)1 IsometricTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer)1 OrthoCachedTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.OrthoCachedTiledMapRenderer)1 OrthogonalTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer)1 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)1 OrthoCamController (com.badlogic.gdx.tests.utils.OrthoCamController)1 Disposable (com.badlogic.gdx.utils.Disposable)1