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

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

Introduction

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

Prototype

public TiledMap() 

Source Link

Document

Creates an empty TiledMap.

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  w w  .  j a v a2  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();//from ww  w.  ja v  a 2s  . com

    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:com.betmansmall.game.gameLogic.mapLoader.MapLoader.java

License:Apache License

/** Loads the map data, given the XML root element and an {@link ImageResolver} used to return the tileset Textures
 * @param root the XML root element/*from w  w w .  j a  v  a2  s .  c  om*/
 * @param tmxFile the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver}
 * @return the {@link TiledMap} */
protected TiledMap loadTilemap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
    TiledMap map = new TiledMap();

    String mapOrientation = root.getAttribute("orientation", null);
    int mapWidth = root.getIntAttribute("width", 0);
    int mapHeight = root.getIntAttribute("height", 0);
    int tileWidth = root.getIntAttribute("tilewidth", 0);
    int tileHeight = root.getIntAttribute("tileheight", 0);
    int hexSideLength = root.getIntAttribute("hexsidelength", 0);
    String staggerAxis = root.getAttribute("staggeraxis", null);
    String staggerIndex = root.getAttribute("staggerindex", null);
    String mapBackgroundColor = root.getAttribute("backgroundcolor", null);

    MapProperties mapProperties = map.getProperties();
    if (mapOrientation != null) {
        mapProperties.put("orientation", mapOrientation);
    }
    mapProperties.put("width", mapWidth);
    mapProperties.put("height", mapHeight);
    mapProperties.put("tilewidth", tileWidth);
    mapProperties.put("tileheight", tileHeight);
    mapProperties.put("hexsidelength", hexSideLength);
    if (staggerAxis != null) {
        mapProperties.put("staggeraxis", staggerAxis);
    }
    if (staggerIndex != null) {
        mapProperties.put("staggerindex", staggerIndex);
    }
    if (mapBackgroundColor != null) {
        mapProperties.put("backgroundcolor", mapBackgroundColor);
    }
    mapTileWidth = tileWidth;
    mapTileHeight = tileHeight;
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    if (mapOrientation != null) {
        if ("staggered".equals(mapOrientation)) {
            if (mapHeight > 1) {
                mapWidthInPixels += tileWidth / 2;
                mapHeightInPixels = mapHeightInPixels / 2 + tileHeight / 2;
            }
        }
    }

    Element properties = root.getChildByName("properties");
    if (properties != null) {
        loadProperties(map.getProperties(), properties);
    }
    Array<Element> tilesets = root.getChildrenByName("tileset");
    for (Element element : tilesets) {
        loadTileSet(map, element, tmxFile, imageResolver);
        root.removeChild(element);
    }
    for (int i = 0, j = root.getChildCount(); i < j; i++) {
        Element element = root.getChild(i);
        String name = element.getName();
        if (name.equals("layer")) {
            loadTileLayer(map, element);
        } else if (name.equals("objectgroup")) {
            loadObjectGroup(map, element);
        } else if (name.equals("imagelayer")) {
            loadImageLayer(map, element, tmxFile, imageResolver);
        }
    }
    return map;
}

From source file:com.skettios.loader.TmxMapLoaderFixed.java

License:Apache License

/**
 * Loads the map data, given the XML root element and an {@link ImageResolver} used to return the tileset Textures
 *
 * @param root          the XML root element
 * @param tmxFile       the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver}
 * @return the {@link TiledMap}/*from   ww w . j  ava  2 s . co m*/
 */
protected TiledMap loadTilemap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
    TiledMap map = new TiledMap();

    String mapOrientation = root.getAttribute("orientation", null);
    int mapWidth = root.getIntAttribute("width", 0);
    int mapHeight = root.getIntAttribute("height", 0);
    int tileWidth = root.getIntAttribute("tilewidth", 0);
    int tileHeight = root.getIntAttribute("tileheight", 0);
    String mapBackgroundColor = root.getAttribute("backgroundcolor", null);

    MapProperties mapProperties = map.getProperties();
    if (mapOrientation != null) {
        mapProperties.put("orientation", mapOrientation);
    }
    mapProperties.put("width", mapWidth);
    mapProperties.put("height", mapHeight);
    mapProperties.put("tilewidth", tileWidth);
    mapProperties.put("tileheight", tileHeight);
    if (mapBackgroundColor != null) {
        mapProperties.put("backgroundcolor", mapBackgroundColor);
    }
    mapTileWidth = tileWidth;
    mapTileHeight = tileHeight;
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    if (mapOrientation != null) {
        if ("staggered".equals(mapOrientation)) {
            if (mapHeight > 1) {
                mapWidthInPixels += tileWidth / 2;
                mapHeightInPixels = mapHeightInPixels / 2 + tileHeight / 2;
            }
        }
    }

    Element properties = root.getChildByName("properties");
    if (properties != null) {
        loadProperties(map.getProperties(), properties);
    }
    Array<Element> tilesets = root.getChildrenByName("tileset");
    for (Element element : tilesets) {
        loadTileSet(map, element, tmxFile, imageResolver);
        root.removeChild(element);
    }
    for (int i = 0, j = root.getChildCount(); i < j; i++) {
        Element element = root.getChild(i);
        String name = element.getName();
        if (name.equals("layer")) {
            loadTileLayer(map, element);
        } else if (name.equals("objectgroup")) {
            loadObjectGroup(map, element);
        } else if (name.equals("imagelayer")) {
            loadImageLayer(map, element, tmxFile, imageResolver);
        }
    }
    return map;
}

From source file:com.tumblr.oddlydrawn.stupidworm.Level.java

License:Apache License

public void loadLevel() {
    TiledMap tiledMap;/*w  w  w. j ava  2s.c  o m*/
    TiledMapTileLayer layer;
    Cell cell;

    // Creates the map objects and loads the appropriate level.
    tiledMap = new TiledMap();
    cell = new Cell();
    tiledMap = new TmxMapLoader().load(level);

    // Gets the collision layer from the map.
    layer = (TiledMapTileLayer) tiledMap.getLayers().get(0);
    cell = layer.getCell(0, 0);
    int width = layer.getWidth();
    int height = layer.getHeight();

    // Goes through all the tiles in the layer, looking for tiles with walls
    // and a single tile with the start position for the worm.
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            cell = layer.getCell(x, y);
            if (cell != null) {
                if (hasCollides(cell)) {
                    levelArray[x][y] = 1;
                } else if (hasStart(cell)) {
                    startCoords.x = x * SIZE;
                    startCoords.y = y * SIZE;
                    levelArray[x][y] = 0;
                } else {
                    levelArray[x][y] = 0;
                }
            }
        }
    }
    tiledMap.dispose();
}

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

License:Apache License

protected TiledMap loadMap(Element root, FileHandle tmxFile, AtlasResolver resolver,
        AtlasTiledMapLoaderParameters parameter) {
    TiledMap map = new TiledMap();

    String mapOrientation = root.getAttribute("orientation", null);
    int mapWidth = root.getIntAttribute("width", 0);
    int mapHeight = root.getIntAttribute("height", 0);
    int tileWidth = root.getIntAttribute("tilewidth", 0);
    int tileHeight = root.getIntAttribute("tileheight", 0);
    String mapBackgroundColor = root.getAttribute("backgroundcolor", null);

    MapProperties mapProperties = map.getProperties();
    if (mapOrientation != null) {
        mapProperties.put("orientation", mapOrientation);
    }/* w ww  . java2  s .c om*/
    mapProperties.put("width", mapWidth);
    mapProperties.put("height", mapHeight);
    mapProperties.put("tilewidth", tileWidth);
    mapProperties.put("tileheight", tileHeight);
    if (mapBackgroundColor != null) {
        mapProperties.put("backgroundcolor", mapBackgroundColor);
    }

    mapTileWidth = tileWidth;
    mapTileHeight = tileHeight;
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    for (int i = 0, j = root.getChildCount(); i < j; i++) {
        Element element = root.getChild(i);
        String elementName = element.getName();
        if (elementName.equals("properties")) {
            loadProperties(map.getProperties(), element);
        } else if (elementName.equals("tileset")) {
            loadTileset(map, element, tmxFile, resolver, parameter);
        } else if (elementName.equals("layer")) {
            loadTileLayer(map, element);
        } else if (elementName.equals("objectgroup")) {
            loadObjectGroup(map, element);
        }
    }
    return map;
}

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);//from  w w  w  .j  a  v a  2  s.c o  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;
}