Example usage for com.badlogic.gdx.maps MapLayer getName

List of usage examples for com.badlogic.gdx.maps MapLayer getName

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps MapLayer getName.

Prototype

public String getName() 

Source Link

Usage

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

License:Open Source License

private Array<TiledMapTileLayer> findLayersMatching(String match) {
    Array<TiledMapTileLayer> array = new Array<TiledMapTileLayer>();
    for (int idx = 0; idx < mMap.getLayers().getCount(); ++idx) {
        MapLayer layer = mMap.getLayers().get(idx);
        if (layer.getName().startsWith(match)) {
            array.add((TiledMapTileLayer) layer);
        }//from  w  w w  .  java2s .  c  o  m
    }
    return array;
}

From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java

License:Apache License

/**
 * Reads a file describing the collision boundaries that should be set
 * per-tile and adds static bodies to the boxd world.
 * /*from  w  ww . j  av a2s  .co m*/
 * @param collisionsFile
 * @param world
 * @param pixelsPerMeter
 *            the pixels per meter scale used for this world
 */
public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter, int level) {
    /**
     * Detect the tiles and dynamically create a representation of the map
     * layout, for collision detection. Each tile has its own collision
     * rules stored in an associated file.
     * 
     * The file contains lines in this format (one line per type of tile):
     * tileNumber XxY,XxY XxY,XxY
     * 
     * Ex:
     * 
     * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31
     * 
     * For a 32x32 tileset, the above describes one line segment for tile #3
     * and two for tile #4. Tile #3 has a line segment across the top. Tile
     * #1 has a line segment across most of the top and a line segment from
     * the top to the bottom, 30 pixels in.
     */

    // FileHandle fh = Gdx.files.internal(collisionsFile);
    // String collisionFile = fh.readString();
    // String lines[] = collisionFile.split("\\r?\\n");

    // HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new
    // HashMap<Integer, ArrayList<LineSegment>>();

    // /**
    // * Some locations on the map (perhaps most locations) are "undefined",
    // * empty space, and will have the tile type 0. This code adds an empty
    // * list of line segments for this "default" tile.
    // */
    // tileCollisionJoints.put(Integer.valueOf(0), new
    // ArrayList<LineSegment>());

    // for (int n = 0; n < lines.length; n++) {
    // String cols[] = lines[n].split(" ");
    // int tileNo = Integer.parseInt(cols[0]);
    //
    // ArrayList<LineSegment> tmp = new ArrayList<LineSegment>();
    //
    // for (int m = 1; m < cols.length; m++) {
    // String coords[] = cols[m].split(",");
    //
    // String start[] = coords[0].split("x");
    // String end[] = coords[1].split("x");
    //
    // tmp.add(new LineSegment(Integer.parseInt(start[0]),
    // Integer.parseInt(start[1]),
    // Integer.parseInt(end[0]), Integer.parseInt(end[1]), ""));
    // }
    //
    // tileCollisionJoints.put(Integer.valueOf(tileNo), tmp);
    // }

    ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>();

    for (int l = 0; l < getMap().getLayers().getCount(); l++) {
        MapLayer nextLayer = getMap().getLayers().get(l);
        if (!nextLayer.getName().equals("AINODEMAP") && !nextLayer.getName().equals("DOORS")) {
            TiledMapTileLayer layer = (TiledMapTileLayer) nextLayer;
            if (layer.getProperties().containsKey("collide")) {
                for (int y = 0; y < layer.getHeight(); y++) {
                    for (int x = 0; x < layer.getWidth(); x++) {
                        Cell tile = layer.getCell(x, y);
                        if (tile != null) {
                            int tileID = tile.getTile().getId();

                            int start = 0;
                            String type = "";
                            if (tile.getTile().getProperties().containsKey("type")) {
                                type = (String) getMap().getTileSets().getTile(tileID).getProperties()
                                        .get("type");
                            }
                            if (layer.getProperties().containsKey("WALLS")) {
                                type = "WALLS";
                                addOrExtendCollisionLineSegment(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 32,
                                        collisionLineSegments, type);
                            } else if (layer.getProperties().containsKey("STAIRS")) {
                                if (!type.equals("LEFTSTAIRS")) {
                                    type = "STAIRS";
                                    addOrExtendCollisionLineSegment(x * 32, y * 32, x * 32 + 32, y * 32 + 32,
                                            collisionLineSegments, type);
                                } else {
                                    addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32,
                                            collisionLineSegments, type);
                                }
                            } else if (layer.getProperties().containsKey("PLATFORMS")) {
                                type = "PLATFORMS";
                                addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32,
                                        collisionLineSegments, type);
                            }
                        }
                    }
                }
            }
        } else

        {
            MapObjects objects = nextLayer.getObjects();
            for (MapObject o : objects) {
                RectangleMapObject rect = (RectangleMapObject) o;
                if (rect.getProperties().containsKey("set") || rect.getProperties().containsKey("to_level")) {
                    Rectangle shape = rect.getRectangle();

                    BodyDef nodeBodyDef = new BodyDef();
                    nodeBodyDef.type = BodyDef.BodyType.StaticBody;
                    nodeBodyDef.position.set((shape.x + shape.width * 0.5f) * Util.PIXEL_TO_BOX,
                            (shape.y + shape.height * 0.5f) * Util.PIXEL_TO_BOX);

                    Body nodeBody = GamePlayManager.world.createBody(nodeBodyDef);
                    if (!nextLayer.getName().equals("DOORS")) {
                        String set = (String) rect.getProperties().get("set");
                        nodeBody.setUserData(new AINode(Integer.parseInt(set)));
                    } else {
                        if (rect.getProperties().containsKey("to_level")
                                && rect.getProperties().containsKey("exitX")
                                && rect.getProperties().containsKey("exitY")) {
                            String to_level = (String) rect.getProperties().get("to_level");
                            String xPos = (String) rect.getProperties().get("exitX");
                            String yPos = (String) rect.getProperties().get("exitY");
                            nodeBody.setUserData(new Door(to_level, xPos, yPos));
                        } else {
                            nodeBody.setUserData(new Door("9999", "1024", "256"));

                        }
                    }

                    PolygonShape nodeShape = new PolygonShape();

                    nodeShape.setAsBox(shape.width * 0.5f * Util.PIXEL_TO_BOX,
                            shape.height * 0.5f * Util.PIXEL_TO_BOX);
                    FixtureDef fixture = new FixtureDef();
                    fixture.shape = nodeShape;
                    fixture.isSensor = true;
                    fixture.density = 0;
                    fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS;
                    fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
                    nodeBody.createFixture(fixture);
                    nodeShape.dispose();
                }
            }
        }
    }

    int platnum = 0;
    for (LineSegment lineSegment : collisionLineSegments)

    {
        BodyDef groundBodyDef = new BodyDef();
        groundBodyDef.type = BodyDef.BodyType.StaticBody;
        groundBodyDef.position.set(0, 0);
        Body groundBody = GamePlayManager.world.createBody(groundBodyDef);
        if (lineSegment.type.equals("STAIRS") || lineSegment.type.equals("LEFTSTAIRS")) {
            groundBody.setUserData(
                    new Stairs("stairs_" + level + "_" + platnum, lineSegment.start(), lineSegment.end()));
        } else if (lineSegment.type.equals("WALLS")) {
            groundBody.setUserData(new Entity().setType(EntityType.WALL));
        } else {
            Platform plat = new Platform("plat_" + level + "_" + platnum, lineSegment.start(),
                    lineSegment.end());
            groundBody.setUserData(plat);
            if (GamePlayManager.lowestPlatInLevel == null
                    || lineSegment.start().y < GamePlayManager.lowestPlatInLevel.getDownPosY()) {
                GamePlayManager.lowestPlatInLevel = plat;
            }
        }
        EdgeShape environmentShape = new EdgeShape();

        environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter),
                lineSegment.end().scl(1 / pixelsPerMeter));
        FixtureDef fixture = new FixtureDef();
        fixture.shape = environmentShape;
        fixture.isSensor = true;
        fixture.density = 1f;
        fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS;
        fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER | Util.CATEGORY_PARTICLES;
        groundBody.createFixture(fixture);
        environmentShape.dispose();
    }

    /**
     * Drawing a boundary around the entire map. We can't use a box because
     * then the world objects would be inside and the physics engine would
     * try to push them out.
     */

    TiledMapTileLayer layer = (TiledMapTileLayer) getMap().getLayers().get(3);

    BodyDef bodydef = new BodyDef();
    bodydef.type = BodyType.StaticBody;
    bodydef.position.set(0, 0);
    Body body = GamePlayManager.world.createBody(bodydef);

    // left wall
    EdgeShape mapBounds = new EdgeShape();
    if (level == 1)

    {
        mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
        body.setUserData(new Entity().setType(EntityType.WALL));
        Fixture fixture = body.createFixture(mapBounds, 0);
        Filter filter = new Filter();
        filter.categoryBits = Util.CATEGORY_PLATFORMS;
        filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
        fixture.setFilterData(filter);
    } else

    {
        mapBounds.set(new Vector2(0f * Util.PIXEL_TO_BOX, 0.0f),
                new Vector2(0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
        body.setUserData(new LevelWall(level - 1));
        Fixture fixture = body.createFixture(mapBounds, 0);
        Filter filter = new Filter();
        filter.categoryBits = Util.CATEGORY_PLATFORMS;
        filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
        fixture.setFilterData(filter);
    }

    // right wall
    body = GamePlayManager.world.createBody(bodydef);
    body.setUserData(new LevelWall(level + 1));

    EdgeShape mapBounds2 = new EdgeShape();
    mapBounds2.set(new Vector2((layer.getWidth() * 32), 0.0f).scl(Util.PIXEL_TO_BOX),
            new Vector2((layer.getWidth() * 32), layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
    Fixture fixture = body.createFixture(mapBounds2, 0);
    Filter filter = new Filter();
    filter.categoryBits = Util.CATEGORY_PLATFORMS;
    filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
    fixture.setFilterData(filter);

    // roof
    body = GamePlayManager.world.createBody(bodydef);
    body.setUserData(new Platform("roof_" + level, new Vector2(0.0f, layer.getHeight() * 32),
            new Vector2(layer.getWidth() * 32, layer.getHeight())));
    EdgeShape mapBounds3 = new EdgeShape();
    mapBounds3.set(new Vector2(0.0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX),
            new Vector2(layer.getWidth() * 32, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
    fixture = body.createFixture(mapBounds3, 0);
    fixture.setFilterData(filter);

    mapBounds.dispose();
    mapBounds2.dispose();
    mapBounds3.dispose();

}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * @param map the {@link Map} which hierarchy to print
 * @return a human readable {@link String} containing the hierarchy of the {@link MapObjects} of the given {@link Map}
 *///from   w w w  .j  a  v a  2 s. c o m
public String getHierarchy(Map map) {
    String hierarchy = map.getClass().getSimpleName() + "\n";

    Iterator<String> keys = map.getProperties().getKeys();
    while (keys.hasNext()) {
        String key = keys.next();
        hierarchy += key + ": " + map.getProperties().get(key) + "\n";
    }

    for (MapLayer layer : map.getLayers()) {
        hierarchy += "\t" + layer.getName() + " (" + layer.getClass().getSimpleName() + "):\n";
        String layerHierarchy = getHierarchy(layer).replace("\n", "\n\t\t");
        layerHierarchy = layerHierarchy.endsWith("\n\t\t")
                ? layerHierarchy.substring(0, layerHierarchy.lastIndexOf("\n\t\t"))
                : layerHierarchy;
        hierarchy += !layerHierarchy.equals("") ? "\t\t" + layerHierarchy : layerHierarchy;
    }

    return hierarchy;
}

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

/**
 * @param map the {@link com.badlogic.gdx.maps.Map} which hierarchy to print
 * @return a human readable {@link String} containing the hierarchy of the {@link com.badlogic.gdx.maps.MapObjects} of the given {@link com.badlogic.gdx.maps.Map}
 *//*from  w w w . ja va 2s  . c o  m*/
public String getHierarchy(Map map) {
    String hierarchy = map.getClass().getName() + "\n", key, layerHierarchy;

    Iterator<String> keys = map.getProperties().getKeys();
    while (keys.hasNext())
        hierarchy += (key = keys.next()) + ": " + map.getProperties().get(key) + "\n";

    for (MapLayer layer : map.getLayers()) {
        hierarchy += "\t" + layer.getName() + " (" + layer.getClass().getName() + "):\n";
        layerHierarchy = getHierarchy(layer).replace("\n", "\n\t\t");
        layerHierarchy = layerHierarchy.endsWith("\n\t\t")
                ? layerHierarchy.substring(0, layerHierarchy.lastIndexOf("\n\t\t"))
                : layerHierarchy;
        hierarchy += !layerHierarchy.equals("") ? "\t\t" + layerHierarchy : layerHierarchy;
    }

    return hierarchy;
}

From source file:com.jupiter.europa.screen.LevelRenderer.java

License:Open Source License

@Override
public void render() {
    beginRender(); // Built - in method

    // Render each layer, in order
    for (MapLayer layer : this.map.getLayers()) {
        if (layer.isVisible()) {
            if (layer instanceof TiledMapTileLayer) {
                this.renderTileLayer((TiledMapTileLayer) layer);
            } else {
                for (MapObject object : layer.getObjects()) {
                    this.renderObject(object);
                }//from  www .  j a v a  2 s .c o  m
            }
        }

        // Render Entities
        if (layer.getName().equals(Level.ENTITY_LAYER_NAME)) {
            this.entityLayerRenderer.render(this.getBatch());
        }
    }

    endRender(); // Built-in method
}

From source file:com.jupiter.europa.world.Level.java

License:Open Source License

private void setMap(TiledMap map) {
    this.map = map;

    // Get metrics
    this.tileWidth = map.getProperties().get(TILE_WIDTH_KEY, Integer.class);
    this.tileHeight = map.getProperties().get(TILE_HEIGHT_KEY, Integer.class);
    this.mapWidth = map.getProperties().get(MAP_WIDTH_KEY, Integer.class);
    this.mapHeight = map.getProperties().get(MAP_HEIGHT_KEY, Integer.class);
    this.pixelWidth = this.getTileWidth() * this.getMapWidth();
    this.pixelHeight = this.getTileHeight() * this.getMapHeight();

    // Set all layers visible, except for the informational layers
    for (MapLayer layer : this.map.getLayers()) {
        String layerName = layer.getName();
        if (layerName.equalsIgnoreCase(ENTITY_LAYER_NAME)) {
            // Get the entity layer from the map
            this.entityLayer = new EntityLayer(layer, new Size(this.getMapWidth(), this.getMapHeight()), this);
        } else if (layerName.equalsIgnoreCase(COLLISION_LAYER_NAME)) {
            this.collision = this.getCollisionFrom(layer);
        } else if (layerName.equalsIgnoreCase(ZONE_LAYER_NAME)) {

        } else {//from   w  w w.jav  a2  s  .  c om
            layer.setVisible(true);
        }
    }

    // Get the type of music from the map
    if (map.getProperties().containsKey(MUSIC_PROPERTY)) {
        this.musicType = map.getProperties().get(MUSIC_PROPERTY, String.class);
    }
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.MapUtils.java

License:Apache License

/** @see #readableHierarchy(com.badlogic.gdx.maps.Map, int) */
public static String readableHierarchy(MapLayer layer, int indent) {
    String hierarchy = "";
    for (int i = 0; i < indent; i++)
        hierarchy += '\t';
    hierarchy += ClassReflection.getSimpleName(layer.getClass());
    if (layer instanceof TiledMapTileLayer) {
        TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
        hierarchy += " (size: " + tileLayer.getWidth() + 'x' + tileLayer.getHeight() + ", tile size: "
                + tileLayer.getTileWidth() + 'x' + tileLayer.getTileHeight() + ')';
    } else/*ww  w . j a v a 2 s.  c om*/
        hierarchy += ' ' + layer.getName();
    hierarchy += '\n';
    hierarchy += readableHierarchy(layer.getProperties(), indent + 1);
    hierarchy += readableHierarchy(layer.getObjects(), indent + 1);
    return hierarchy;
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.TmxMapWriter.java

License:Apache License

/** @param layer the {@link MapLayer} to write in TMX format
 *  @return this {@link TmxMapWriter} */
public TmxMapWriter tmx(MapLayer layer) throws IOException {
    element("objectgroup");
    attribute("name", layer.getName());
    tmx(layer.getProperties());/*  www .  j ava 2 s.c o m*/
    tmx(layer.getObjects());
    pop();
    return this;
}

From source file:de.fhkoeln.game.utils.libgdx.maps.MapUtils.java

License:Apache License

/** @see #readableHierarchy(com.badlogic.gdx.maps.Map, int) */
public static String readableHierarchy(MapLayer layer, int indent) {
    String hierarchy = "";
    for (int i = 0; i < indent; i++)
        hierarchy += '\t';
    hierarchy += layer.getClass().getSimpleName();
    if (layer instanceof TiledMapTileLayer) {
        TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
        hierarchy += " (size: " + tileLayer.getWidth() + 'x' + tileLayer.getHeight() + ", tile size: "
                + tileLayer.getTileWidth() + 'x' + tileLayer.getTileHeight() + ')';
    } else//  w ww.ja va  2s  .  c  o  m
        hierarchy += ' ' + layer.getName();
    hierarchy += '\n';
    hierarchy += readableHierarchy(layer.getProperties(), indent + 1);
    hierarchy += readableHierarchy(layer.getObjects(), indent + 1);
    return hierarchy;
}

From source file:net.dermetfan.gdx.maps.MapUtils.java

License:Apache License

/** @see #readableHierarchy(com.badlogic.gdx.maps.Map, int) */
public static String readableHierarchy(MapLayer layer, int indent) {
    StringBuilder hierarchy = new StringBuilder();
    for (int i = 0; i < indent; i++)
        hierarchy.append('\t');
    hierarchy.append(ClassReflection.getSimpleName(layer.getClass()));
    if (layer instanceof TiledMapTileLayer) {
        TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
        hierarchy.append(" (size: ").append(tileLayer.getWidth()).append('x').append(tileLayer.getHeight())
                .append(", tile size: ").append(tileLayer.getTileWidth()).append('x')
                .append(tileLayer.getTileHeight()).append(')');
    } else//w ww  . jav  a 2 s  .  c  o m
        hierarchy.append(' ').append(layer.getName());
    hierarchy.append('\n');
    hierarchy.append(readableHierarchy(layer.getProperties(), indent + 1));
    hierarchy.append(readableHierarchy(layer.getObjects(), indent + 1));
    return hierarchy.toString();
}