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

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

Introduction

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

Prototype

public MapLayers getLayers() 

Source Link

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 www  .j  a va2 s  .  co m*/
 */
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:ch.coldpixel.mario.Tools.B2WorldCreator.java

public B2WorldCreator(World world, TiledMap map) {
    //Create body and fixture variables
    BodyDef bdef = new BodyDef();
    PolygonShape shape = new PolygonShape();
    FixtureDef fdef = new FixtureDef();
    Body body;/*from  w w  w .j  av  a 2  s  . co  m*/

    //create ground bodies/fixture
    for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) { //get(2) = in TILED from below the 2nd object(starts with 0, so it gets ground)
        Rectangle rect = ((RectangleMapObject) object).getRectangle(); //Get Rectangle Object itself

        bdef.type = BodyDef.BodyType.StaticBody;//3 different bodys, static=selfexplaining, dynamic=typical player, kinematic=not effected by forces like gravity, but can manipulated with velocity, ex: moving plattforms
        bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM,
                (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM);

        body = world.createBody(bdef);//add body to our world

        shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM);
        fdef.shape = shape;
        body.createFixture(fdef);
    }

    //create pipe bodies/fixture
    for (MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        bdef.type = BodyDef.BodyType.StaticBody;
        bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM,
                (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM);

        body = world.createBody(bdef);

        shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM);
        fdef.shape = shape;
        body.createFixture(fdef);
    }

    //create brick bodies/fixture
    for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        new Brick(world, map, rect);

    }

    //create coin bodies/fixtures
    for (MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        new Coin(world, map, rect);

    }

}

From source file:com.agateau.pixelwheels.map.LapPositionTableIO.java

License:Open Source License

public static LapPositionTable load(TiledMap map) {
    MapLayer layer = map.getLayers().get("Sections");
    Assert.check(layer != null, "No 'Sections' layer found");
    MapObjects objects = layer.getObjects();

    Array<Line> lines = new Array<Line>();
    lines.ensureCapacity(objects.getCount());
    Set<String> names = new HashSet<String>();
    for (MapObject obj : objects) {
        String name = obj.getName();
        Assert.check(!name.isEmpty(), "Section line is missing a name");
        Assert.check(!names.contains(name), "Duplicate section line " + name);
        names.add(name);//from w  w w .j  av a 2 s .c om

        float order;
        try {
            order = Float.parseFloat(name);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid section name " + name);
        }
        Assert.check(obj instanceof PolylineMapObject,
                "'Sections' layer should only contain PolylineMapObjects");
        Polyline polyline = ((PolylineMapObject) obj).getPolyline();
        float[] vertices = polyline.getTransformedVertices();
        Assert.check(vertices.length == 4,
                "Polyline with name " + order + "should have 2 points, not " + (vertices.length / 2));
        Line line = new Line();
        line.x1 = vertices[0];
        line.y1 = vertices[1];
        line.x2 = vertices[2];
        line.y2 = vertices[3];
        line.order = order;
        lines.add(line);
    }
    lines.sort();

    LapPositionTable table = new LapPositionTable();
    for (int idx = 0; idx < lines.size; ++idx) {
        Line line1 = lines.get(idx);
        Line line2 = lines.get((idx + 1) % lines.size);
        float[] vertices = { line1.x1, line1.y1, line2.x1, line2.y1, line2.x2, line2.y2, line1.x2, line1.y2 };
        Polygon polygon = new Polygon(vertices);
        table.addSection(idx, polygon);
    }
    return table;
}

From source file:com.agateau.pixelwheels.tools.LapPositionTableGenerator.java

License:Apache License

public static void generateTable(FileHandle tmxFile, FileHandle tableFile) {
    TiledMap map = new TmxMapLoader().load(tmxFile.path());
    LapPositionTable table = LapPositionTableIO.load(map);

    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
    int width = layer.getWidth() * ((int) layer.getTileWidth());
    int height = layer.getHeight() * ((int) layer.getTileHeight());

    Pixmap pixmap = LapPositionTableIO.createPixmap(table, width, height);
    PixmapIO.writePNG(tableFile, pixmap);
}

From source file:com.agateau.pixelwheels.tools.MapScreenshotGenerator.java

License:Apache License

private static Pixmap generateScreenshot(FileHandle tmxFile) {
    TiledMap map = new TmxMapLoader().load(tmxFile.path());
    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
    int mapWidth = (int) (layer.getWidth() * layer.getTileWidth());
    int mapHeight = (int) (layer.getHeight() * layer.getTileHeight());

    FrameBuffer fbo = new FrameBuffer(Pixmap.Format.RGB888, mapWidth, mapHeight, false /* hasDepth */);
    OrthogonalTiledMapRenderer renderer = new OrthogonalTiledMapRenderer(map);

    OrthographicCamera camera = new OrthographicCamera();
    camera.setToOrtho(true /* yDown */, mapWidth, mapHeight);
    renderer.setView(camera);/*from w  ww .j a  va2  s .co  m*/

    fbo.begin();
    renderer.render();

    return ScreenUtils.getFrameBufferPixmap(0, 0, mapWidth, mapHeight);
}

From source file:com.barconr.games.marblegame.Maze3Drenderer.java

License:Apache License

private void createModels(TiledMap tiledmap) {

    // Load the Tiled map layer and store some relevant variables
    TiledMapTileLayer tilelayer = (TiledMapTileLayer) (tiledmap.getLayers().get(0));
    layerHeight = tilelayer.getHeight();
    layerWidth = tilelayer.getWidth();//  w  ww .  ja v a  2s  .co  m

    tileWidth = tileHeight = CUBE_SIZE;

    for (int y_pos = 0; y_pos < layerHeight; y_pos++) {

        for (int x_pos = 0; x_pos < layerWidth; x_pos++) {

            //boolean impassibleBlock = tilelayer.getCell(x_pos, layerHeight-y_pos-1).getTile().getProperties().containsKey("block");
            boolean impassibleBlock = tilelayer.getCell(x_pos, y_pos).getTile().getProperties()
                    .containsKey("block");
            if (impassibleBlock) {
                //Draw a cube here
                float xcord = x_pos * tileWidth;
                float ycord = y_pos * tileHeight;

                addBox(new Vector3(xcord, ycord, 0));
                //               mazeBuilder.createMazePart(xcord,ycord,0);
            }

        }

    }

    //Background
    addBox(new Vector3((layerWidth * tileWidth) / 2, (layerHeight * tileHeight) / 2, -CUBE_SIZE),
            layerWidth * tileWidth, layerHeight * tileHeight, CUBE_SIZE, Color.GRAY);

}

From source file:com.barconr.games.marblegame.Physics2D.java

License:Apache License

public void createFixtures(TiledMap tiledmap) {
    int layerHeight = 0, layerWidth = 0;
    float tileWidth = 0, tileHeight = 0;

    ballsListener = new BallContactListener();

    // Load the Tiled map layer and store some relevant variables
    TiledMapTileLayer tilelayer = (TiledMapTileLayer) (tiledmap.getLayers().get(0));
    layerHeight = tilelayer.getHeight();
    layerWidth = tilelayer.getWidth();/*from   w w w  .  ja  v a 2  s .  c  o m*/

    maze = new Maze(layerWidth, layerHeight);

    tileHeight = tilelayer.getTileHeight();
    tileWidth = tilelayer.getTileWidth();

    System.out.println("Layer height (tiles) = " + layerHeight + " Layer width (tiles) = " + layerWidth
            + " Tile height = " + tileHeight + "tile width = " + tileWidth);
    System.out.println("Tile count = " + tilelayer.getObjects().getCount());
    System.out.println("layer height pixels = " + (layerHeight * tileHeight) + "LAYER WIDTH PIXELS : "
            + (layerWidth * tileWidth));

    //Create the box2d world
    world = new World(new Vector2(0, -9), true);
    world.setContactListener(ballsListener);

    // Loop through the grid reference
    for (int y_pos = 0; y_pos < layerHeight; y_pos++) {

        for (int x_pos = 0; x_pos < layerWidth; x_pos++) {

            boolean impassibleBlock = tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile()
                    .getProperties().containsKey("block");

            // If the tile square contains the reference "start"
            // Store this as the start position
            if (tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile().getProperties()
                    .containsKey("start")) {
                if (startPosition == null) {
                    System.out.println("x:" + x_pos * tileWidth + " y:" + y_pos * tileHeight);
                    startPosition = new Vector2(x_pos * tileWidth * Assets.METERS_PER_PIXEL,
                            (50 - y_pos) * tileHeight * Assets.METERS_PER_PIXEL);

                }

            }

            //Create a fixture for the end position
            if (tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile().getProperties()
                    .containsKey("end")) {
                //Draw box for fixture that is impassible
                PolygonShape squareShape = new PolygonShape();

                BodyDef squareBodyDef = new BodyDef();

                squareBodyDef.type = BodyType.StaticBody; //Static body which won't move
                //Box position
                squareBodyDef.position.set(new Vector2((x_pos * Assets.METERS_PER_PIXEL * tileWidth),
                        (layerHeight - y_pos - 1) * Assets.METERS_PER_PIXEL * tileHeight));
                //Correction for fact Box2Ds squares are half width/height from center point
                squareBodyDef.position.add(tileWidth / 2 * Assets.METERS_PER_PIXEL,
                        tileHeight / 2 * Assets.METERS_PER_PIXEL);

                Body squareBody = world.createBody(squareBodyDef);

                squareShape.setAsBox(tileWidth / 2 * Assets.METERS_PER_PIXEL,
                        tileHeight / 2 * Assets.METERS_PER_PIXEL);
                FixtureDef fixDefSquare = new FixtureDef();

                fixDefSquare.shape = squareShape;
                fixDefSquare.isSensor = true;

                fixDefSquare.density = 0.1f;
                fixDefSquare.restitution = 0.3f;

                Fixture endFix = squareBody.createFixture(fixDefSquare);
                endFix.setSensor(true);
                endFix.setUserData("exit");
            }

            if (impassibleBlock) {

                //Draw box for fixture that blocks

                maze.setMazeUnit(new MazeUnit("block"), x_pos, y_pos);
                //System.out.print("@");      // Draw ascii map in stdout

            } else {
                maze.setMazeUnit(new MazeUnit("passage"), x_pos, y_pos);
            }

        }
        //System.out.println();
    }

    // The players ball
    playerBallBodyDef = new BodyDef();
    playerBallBodyDef.type = BodyType.DynamicBody;

    if (startPosition == null) {
        playerBallBodyDef.position.set(3f, 3f);

    } else {
        playerBallBodyDef.position.set(startPosition.x, startPosition.y);
    }
    playerBallBodyDef.allowSleep = false;

    ballCircleBody = world.createBody(playerBallBodyDef);

    dynamicCircle = new CircleShape();
    dynamicCircle.setRadius(8 / Assets.PIXELS_PER_METER);

    FixtureDef ballFixtureDef = new FixtureDef();
    ballFixtureDef.shape = dynamicCircle;
    ballFixtureDef.density = 0.1f;
    ballFixtureDef.friction = 1f;
    //ballFixtureDef.

    ballFixtureDef.restitution = 0.5f;
    //ballCircleBody.setUserData("ball");
    Fixture fx = ballCircleBody.createFixture(ballFixtureDef);
    fx.setUserData("ball");

    maze.removeExtraBoxes();
    /*
    PolygonShape squareShape = new PolygonShape();
            
    */

    for (int i = 0; i < maze.width; i++) {
        for (int j = 0; j < maze.height; j++) {
            if (maze.mazeUnits[j][i].getType().equals("block")) {
                BodyDef squareBodyDef = new BodyDef();
                PolygonShape squareShape = new PolygonShape();
                squareBodyDef.type = BodyType.StaticBody; //Static body which won't move
                //Box position
                squareBodyDef.position.set(new Vector2((j * Assets.METERS_PER_PIXEL * tileWidth),
                        (layerHeight - i - 1) * Assets.METERS_PER_PIXEL * tileHeight));
                //Correction for fact Box2Ds squares are half width/height from center point
                squareBodyDef.position.add(tileWidth / 2 * Assets.METERS_PER_PIXEL,
                        tileHeight / 2 * Assets.METERS_PER_PIXEL);

                Body squareBody = world.createBody(squareBodyDef);
                //Size of box
                squareShape.setAsBox(tileWidth / 2 * Assets.METERS_PER_PIXEL,
                        tileHeight / 2 * Assets.METERS_PER_PIXEL);
                FixtureDef fixDefSquare = new FixtureDef();
                fixDefSquare.shape = squareShape;
                fixDefSquare.density = 0.1f;
                fixDefSquare.restitution = 0.3f;
                squareBody.createFixture(fixDefSquare);
            }
        }
    }

}

From source file:com.gdx.game.tools.b2WorldCreator.java

private void createGroundRed(World world, BodyDef bodyDef, TiledMap map) {
    for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set((rect.getX() + rect.getWidth() / 2) / GdxGame.PPM,
                (rect.getY() + rect.getHeight() / 2) / GdxGame.PPM);

        this.body = world.createBody(bodyDef);
        this.shape.setAsBox(rect.getWidth() / 2 / GdxGame.PPM, rect.getHeight() / 2 / GdxGame.PPM);
        this.fixtureDef.shape = shape;
        this.body.createFixture(fixtureDef);
    }//from w ww  .j  av a 2  s.c  o  m
}

From source file:com.gdx.game.tools.b2WorldCreator.java

private void createGroundGreen(World world, BodyDef bodyDef, TiledMap map) {
    // ground green
    for (MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set((rect.getX() + rect.getWidth() / 2) / GdxGame.PPM,
                (rect.getY() + rect.getHeight() / 2) / GdxGame.PPM);

        body = world.createBody(bodyDef);
        shape.setAsBox(rect.getWidth() / 2 / GdxGame.PPM, rect.getHeight() / 2 / GdxGame.PPM);
        fixtureDef.shape = shape;/*  www.  j  ava2 s. c o  m*/
        body.createFixture(fixtureDef);
    }
}

From source file:com.gdx.game.tools.b2WorldCreator.java

private void createGroundBlue(World world, BodyDef bodyDef, TiledMap map) {
    // ground blue
    for (MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        this.bodyDef.type = BodyDef.BodyType.StaticBody;
        this.bodyDef.position.set((rect.getX() + rect.getWidth() / 2) / GdxGame.PPM,
                (rect.getY() + rect.getHeight() / 2) / GdxGame.PPM);

        this.body = world.createBody(bodyDef);
        this.shape.setAsBox(rect.getWidth() / 2 / GdxGame.PPM, rect.getHeight() / 2 / GdxGame.PPM);
        this.fixtureDef.shape = shape;
        this.body.createFixture(fixtureDef);
    }/*from w ww .j a  va 2 s .  c om*/
}