Example usage for com.badlogic.gdx.maps.tiled TiledMapTileLayer TiledMapTileLayer

List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileLayer TiledMapTileLayer

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps.tiled TiledMapTileLayer TiledMapTileLayer.

Prototype

public TiledMapTileLayer(int width, int height, int tileWidth, int tileHeight) 

Source Link

Document

Creates TiledMap layer

Usage

From source file:ca.hiphiparray.amazingmaze.MapFactory.java

License:Open Source License

/**
 * Return a map generated with the {@link MapFactory}'s parameters.
 *
 * @return a tiled map.//from  w ww . j ava2  s .c  om
 */
public TiledMap generateMap() {
    TiledMap map = new TiledMap();
    map.getTileSets().addTileSet(assets.tiles);

    TiledMapTileLayer backgroundLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE,
            MazeScreen.TILE_SIZE);
    backgroundLayer.setName(BACKGROUND_LAYER);
    for (int c = 0; c < backgroundLayer.getWidth(); c++) {
        for (int r = 0; r < backgroundLayer.getHeight(); r++) {
            Cell cell = new Cell();
            cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BACKGROUND)));
            backgroundLayer.setCell(c, r, cell);
        }
    }
    map.getLayers().add(backgroundLayer);

    final int gateSpace = 2;
    final int extraRoom = 3;
    List<Integer> splits = generateWireLocations();

    TiledMapTileLayer objectLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE,
            MazeScreen.TILE_SIZE);
    objectLayer.setName(OBJECT_LAYER);
    TiledMapTileLayer wireLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE,
            MazeScreen.TILE_SIZE);
    wireLayer.setName(WIRE_LAYER);
    for (int col : splits) { // Place the middle barriers and the unknown wires.
        boolean upperOutput = random.nextBoolean();
        Circuit upperGate = new Circuit(upperOutput, random);
        Circuit lowerGate = new Circuit(!upperOutput, random);
        Point highLocation = new Point(col, height - gateSpace);
        Point lowLocation = new Point(col, gateSpace - 1);

        if (Circuit.evaluateGate(upperGate.getGate(), upperGate.isInputA(), upperGate.isInputB())) {
            gateOn.add(upperGate);
        } else {
            gateOn.add(lowerGate);
        }

        placeUpperCircuit(objectLayer, upperGate, highLocation);
        placeLowerCircuit(objectLayer, lowerGate, lowLocation);
        int barrierLoc = randomInt(gateSpace + extraRoom, height - (gateSpace + extraRoom));
        Cell cell = new Cell();
        cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BARRIER)));
        objectLayer.setCell(col, barrierLoc, cell);
        for (int r = barrierLoc - 1; r >= gateSpace; r--) { // Place the lower wires.
            WireCell wire = new WireCell(!upperOutput);
            wire.setTile(assets.tiles
                    .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.UNKNOWN)));
            wireLayer.setCell(col, r, wire);
        }
        for (int r = barrierLoc + 1; r < height - gateSpace; r++) { // Place the upper wires.
            WireCell wire = new WireCell(upperOutput);
            wire.setTile(assets.tiles
                    .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.UNKNOWN)));
            wireLayer.setCell(col, r, wire);
        }
    }
    for (int c = 0; c < width; c++) {
        if (!splits.contains(c)) {
            Cell cell = new Cell();
            cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BARRIER)));
            objectLayer.setCell(c, gateSpace, cell);
            cell = new Cell();
            cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BARRIER)));
            objectLayer.setCell(c, height - gateSpace - 1, cell);
        }
    }

    map.getLayers().add(objectLayer);
    map.getLayers().add(wireLayer);

    TiledMapTileLayer powerUpLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE,
            MazeScreen.TILE_SIZE);
    powerUpLayer.setName(ITEM_LAYER);
    for (int c = 1; c < width; c++) {
        if (!splits.contains(c)) {
            if (random.nextDouble() <= 0.25) {
                placeFish(powerUpLayer, c, gateSpace);
                c++;
            } else if (random.nextDouble() <= 0.1) {
                placeCheese(powerUpLayer, c, gateSpace);
                c++;
            }
        }
    }
    map.getLayers().add(powerUpLayer);

    return map;
}

From source file:com.badlogic.gdx.tests.bench.TiledMapBench.java

License:Apache License

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 320, 320);
    camera.update();//  w  ww .  jav a  2  s  .c o  m

    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);

    font = new BitmapFont();
    batch = new SpriteBatch();

    {
        tiles = new Texture(Gdx.files.internal("data/maps/tiled/tiles.png"));
        TextureRegion[][] splitTiles = TextureRegion.split(tiles, 32, 32);
        map = new TiledMap();
        MapLayers layers = map.getLayers();
        for (int l = 0; l < 20; l++) {
            TiledMapTileLayer layer = new TiledMapTileLayer(150, 100, 32, 32);
            for (int x = 0; x < 150; x++) {
                for (int y = 0; y < 100; y++) {
                    int ty = (int) (Math.random() * splitTiles.length);
                    int tx = (int) (Math.random() * splitTiles[ty].length);
                    Cell cell = new Cell();
                    cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx]));
                    layer.setCell(x, y, cell);
                }
            }
            layers.add(layer);
        }
    }

    renderer = new OrthogonalTiledMapRenderer(map);

}

From source file:de.bitowl.advent.game2.MyAtlasTmxMapLoader.java

License:Apache License

protected void loadTileLayer(TiledMap map, Element element) {
    if (element.getName().equals("layer")) {
        String name = element.getAttribute("name", null);
        int width = element.getIntAttribute("width", 0);
        int height = element.getIntAttribute("height", 0);
        int tileWidth = element.getParent().getIntAttribute("tilewidth", 0);
        int tileHeight = element.getParent().getIntAttribute("tileheight", 0);
        boolean visible = element.getIntAttribute("visible", 1) == 1;
        float opacity = element.getFloatAttribute("opacity", 1.0f);
        TiledMapTileLayer layer = new TiledMapTileLayer(width, height, tileWidth, tileHeight);
        layer.setVisible(visible);/* w  w w  .  j  a v  a 2s.c  o m*/
        layer.setOpacity(opacity);
        layer.setName(name);

        TiledMapTileSets tilesets = map.getTileSets();

        Element data = element.getChildByName("data");
        String encoding = data.getAttribute("encoding", null);
        String compression = data.getAttribute("compression", null);
        if (encoding == null) { // no 'encoding' attribute means that the encoding is XML
            throw new GdxRuntimeException("Unsupported encoding (XML) for TMX Layer Data");
        }
        if (encoding.equals("csv")) {
            String[] array = data.getText().split(",");
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int id = (int) Long.parseLong(array[y * width + x].trim());

                    final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                    final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                    final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                    id = id & ~MASK_CLEAR;

                    tilesets.getTile(id);

                    TiledMapTile tile = tilesets.getTile(id);
                    if (tile != null) {
                        Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally);
                        cell.setTile(tile);
                        layer.setCell(x, yUp ? height - 1 - y : y, cell);
                    }
                }
            }
        } else {
            if (encoding.equals("base64")) {
                byte[] bytes = Base64Coder.decode(data.getText());
                if (compression == null) {
                    int read = 0;
                    for (int y = 0; y < height; y++) {
                        for (int x = 0; x < width; x++) {

                            int id = unsignedByteToInt(bytes[read++]) | unsignedByteToInt(bytes[read++]) << 8
                                    | unsignedByteToInt(bytes[read++]) << 16
                                    | unsignedByteToInt(bytes[read++]) << 24;

                            final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                            final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                            final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                            id = id & ~MASK_CLEAR;

                            tilesets.getTile(id);
                            TiledMapTile tile = tilesets.getTile(id);
                            if (tile != null) {
                                Cell cell = createTileLayerCell(flipHorizontally, flipVertically,
                                        flipDiagonally);
                                cell.setTile(tile);
                                layer.setCell(x, yUp ? height - 1 - y : y, cell);
                            }
                        }
                    }
                } else if (compression.equals("gzip")) {
                    GZIPInputStream GZIS = null;
                    try {
                        GZIS = new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length);
                    } catch (IOException e) {
                        throw new GdxRuntimeException(
                                "Error Reading TMX Layer Data - IOException: " + e.getMessage());
                    }

                    byte[] temp = new byte[4];
                    for (int y = 0; y < height; y++) {
                        for (int x = 0; x < width; x++) {
                            try {
                                GZIS.read(temp, 0, 4);
                                int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8
                                        | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;

                                final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                                final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                                final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                                id = id & ~MASK_CLEAR;

                                tilesets.getTile(id);
                                TiledMapTile tile = tilesets.getTile(id);
                                if (tile != null) {
                                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically,
                                            flipDiagonally);
                                    cell.setTile(tile);
                                    layer.setCell(x, yUp ? height - 1 - y : y, cell);
                                }
                            } catch (IOException e) {
                                throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                            }
                        }
                    }
                } else if (compression.equals("zlib")) {
                    Inflater zlib = new Inflater();

                    byte[] temp = new byte[4];

                    zlib.setInput(bytes, 0, bytes.length);

                    for (int y = 0; y < height; y++) {
                        for (int x = 0; x < width; x++) {
                            try {
                                zlib.inflate(temp, 0, 4);
                                int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8
                                        | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24;

                                final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                                final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                                final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0);

                                id = id & ~MASK_CLEAR;

                                tilesets.getTile(id);
                                if (id != 0) {
                                    String inset = "NONE";
                                    if (tilesets.getTileSet(0).getTile(id) != null) {
                                        inset = tilesets.getTileSet(0).getName();
                                    } else if (tilesets.getTileSet(1).getTile(id) != null) {
                                        inset = tilesets.getTileSet(1).getName();
                                    }
                                    System.out.println("need tile " + id + inset);
                                }

                                TiledMapTile tile = tilesets.getTile(id);
                                if (tile != null) {
                                    Cell cell = createTileLayerCell(flipHorizontally, flipVertically,
                                            flipDiagonally);
                                    cell.setTile(tile);
                                    layer.setCell(x, yUp ? height - 1 - y : y, cell);
                                }

                            } catch (DataFormatException e) {
                                throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                            }
                        }
                    }
                }
            } else {
                // any other value of 'encoding' is one we're not aware of, probably a feature of a future version of Tiled
                // or another editor
                throw new GdxRuntimeException("Unrecognised encoding (" + encoding + ") for TMX Layer Data");
            }
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(layer.getProperties(), properties);
        }
        map.getLayers().add(layer);
    }
}

From source file:fr.plafogaj.game.engine.TiledMapConfig.java

License:Open Source License

public TiledMapConfig(String tmxFile) {
    m_map = new TmxMapLoader().load(tmxFile);

    m_fgLayer = (TiledMapTileLayer) m_map.getLayers().get("fg");
    m_middleLayer = (TiledMapTileLayer) m_map.getLayers().get("middle");
    m_collisionLayer = (TiledMapTileLayer) m_map.getLayers().get("collisionLayer");
    m_bg_1Layer = (TiledMapTileLayer) m_map.getLayers().get("bg_1");
    m_bg_2Layer = (TiledMapTileLayer) m_map.getLayers().get("bg_2");
    m_bg_3Layer = (TiledMapTileLayer) m_map.getLayers().get("bg_3");
    m_bg_4Layer = (TiledMapTileLayer) m_map.getLayers().get("bg_4");
    m_enemiesObjects = m_map.getLayers().get("enemies").getObjects();
    m_enemiesLayerFake = new TiledMapTileLayer(m_fgLayer.getWidth(), m_fgLayer.getHeight(), 32, 32);
    m_endObject = (EllipseMapObject) m_map.getLayers().get("end").getObjects().get(0);
    m_endObjectCoord = new Vector2(m_endObject.getEllipse().x * TILE_UNIT_SCALE,
            m_endObject.getEllipse().y * TILE_UNIT_SCALE);

    m_mapRenderer = new OrthogonalTiledMapRenderer(m_map, TILE_UNIT_SCALE);

    m_camera = new TiledMapOrthographicCamera();
    m_camera.setToOrtho(false, 45, 25);// w  ww .j a  v a 2 s . com
    m_camera.update();
}

From source file:org.lightjason.examples.pokemon.simulation.environment.CEnvironment.java

License:LGPL

@Override
public final TiledMap map() {
    // create background checkerboard with a tile map
    final Pixmap l_pixmap = new Pixmap(2 * m_cellsize, m_cellsize, Pixmap.Format.RGBA8888);
    l_pixmap.setColor(new Color(0.8f, 0.1f, 0.1f, 0.5f));
    l_pixmap.fillRectangle(0, 0, m_cellsize, m_cellsize);
    l_pixmap.setColor(new Color(0.5f, 0.5f, 0.5f, 0.5f));
    l_pixmap.fillRectangle(m_cellsize, 0, m_cellsize, m_cellsize);

    final Texture l_texture = new Texture(l_pixmap);
    final TiledMapTile l_region1 = new StaticTiledMapTile(
            new TextureRegion(l_texture, 0, 0, m_cellsize, m_cellsize));
    final TiledMapTile l_region2 = new StaticTiledMapTile(
            new TextureRegion(l_texture, m_cellsize, 0, m_cellsize, m_cellsize));

    // create tilemap
    final TiledMap l_map = new TiledMap();
    final TiledMapTileLayer l_layer = new TiledMapTileLayer(m_column, m_row, m_cellsize, m_cellsize);
    l_map.getLayers().add(l_layer);/*w w  w.j a v a 2  s.co m*/

    IntStream.range(0, m_column).forEach(x -> {
        IntStream.range(0, m_row).forEach(y -> {
            final TiledMapTileLayer.Cell l_cell = new TiledMapTileLayer.Cell();
            l_layer.setCell(x, y, l_cell);
            l_cell.setTile(
                    y % 2 != 0 ? x % 2 != 0 ? l_region1 : l_region2 : x % 2 != 0 ? l_region2 : l_region1);
        });
    });

    return l_map;
}