Example usage for com.badlogic.gdx.maps MapProperties containsKey

List of usage examples for com.badlogic.gdx.maps MapProperties containsKey

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps MapProperties containsKey.

Prototype

public boolean containsKey(String key) 

Source Link

Usage

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

License:Apache License

/** filters the tiles that are frames
 *  @param tiles all tiles/*from w  w  w .  j  av a2s . co  m*/
 *  @param animationKey the key used to tell if a tile is a frame
 *  @return an {@link ObjectMap} which values are tiles that are frames and which keys are their animation names */
public static ObjectMap<String, Array<StaticTiledMapTile>> filterFrames(TiledMapTile[] tiles,
        String animationKey) {
    ObjectMap<String, Array<StaticTiledMapTile>> animations = new ObjectMap<>();

    MapProperties tileProperties;
    String animationName;

    for (TiledMapTile tile : tiles) {
        if (!(tile instanceof StaticTiledMapTile))
            continue;

        tileProperties = tile.getProperties();

        if (tileProperties.containsKey(animationKey)) {
            animationName = tileProperties.get(animationKey, String.class);
            if (!animations.containsKey(animationName))
                animations.put(animationName, new Array<StaticTiledMapTile>(3));
            animations.get(animationName).add((StaticTiledMapTile) tile);
        }
    }

    return animations;
}

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

License:Apache License

/** @param set the {@link TiledMapTileSet} to write in TMX format
 *  @return this {@link TmxMapWriter} */
public TmxMapWriter tmx(TiledMapTileSet set) throws IOException {
    MapProperties props = set.getProperties();
    element("tileset");
    attribute("firstgid", getProperty(props, "firstgid", 1));
    attribute("name", set.getName());
    attribute("tilewidth", getProperty(props, "tilewidth", 0));
    attribute("tileheight", getProperty(props, "tileheight", 0));
    float spacing = getProperty(props, "spacing", Float.NaN), margin = getProperty(props, "margin", Float.NaN);
    if (!Float.isNaN(spacing))
        attribute("spacing", round(spacing));
    if (!Float.isNaN(margin))
        attribute("margin", round(margin));

    Iterator<TiledMapTile> iter = set.iterator();
    if (iter.hasNext()) {
        TiledMapTile tile = iter.next();
        element("tileoffset");
        attribute("x", round(tile.getOffsetX()));
        attribute("y", round(-tile.getOffsetY()));
        pop();//from  w w  w.j  a  v a2  s. co m
    }

    element("image");
    attribute("source", getProperty(props, "imagesource", ""));
    attribute("imagewidth", getProperty(props, "imagewidth", 0));
    attribute("imageheight", getProperty(props, "imageheight", 0));
    pop();

    iter = set.iterator();
    if (iter.hasNext()) {
        @SuppressWarnings("unchecked")
        Array<String> asAttributes = Pools.obtain(Array.class);
        asAttributes.clear();
        boolean elementEmitted = false;
        for (TiledMapTile tile = iter.next(); iter.hasNext(); tile = iter.next()) {
            MapProperties tileProps = tile.getProperties();
            for (String attribute : asAttributes)
                if (tileProps.containsKey(attribute)) {
                    if (!elementEmitted) {
                        element("tile");
                        elementEmitted = true;
                    }
                    attribute(attribute, tileProps.get(attribute));
                }
            tmx(tileProps, asAttributes);
        }
        asAttributes.clear();
        Pools.free(asAttributes);
        if (elementEmitted)
            pop();
    }

    pop();
    return this;
}

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

License:Apache License

/** @param object the {@link MapObject} to write in TMX format
 *  @return this {@link TmxMapWriter} */
public TmxMapWriter tmx(MapObject object) throws IOException {
    MapProperties props = object.getProperties();
    element("object");
    attribute("name", object.getName());
    if (props.containsKey("type"))
        attribute("type", getProperty(props, "type", ""));
    if (props.containsKey("gid"))
        attribute("gid", getProperty(props, "gid", 0));

    int objectX = getProperty(props, "x", 0);
    int objectY = getProperty(props, "y", 0);

    if (object instanceof RectangleMapObject) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();
        int height = round(rect.height);
        attribute("x", objectX).attribute("y", toYDown(objectY + height));
        attribute("width", round(rect.width)).attribute("height", height);
    } else if (object instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) object).getEllipse();
        int height = round(ellipse.height);
        attribute("x", objectX).attribute("y", toYDown(objectY + height));
        attribute("width", round(ellipse.width)).attribute("height", height);
        element("ellipse").pop();
    } else if (object instanceof CircleMapObject) {
        Circle circle = ((CircleMapObject) object).getCircle();
        attribute("x", objectX).attribute("y", objectY);
        attribute("width", round(circle.radius * 2)).attribute("height", round(circle.radius * 2));
        element("ellipse").pop();
    } else if (object instanceof PolygonMapObject) {
        attribute("x", objectX).attribute("y", toYDown(objectY));
        Polygon polygon = ((PolygonMapObject) object).getPolygon();
        element("polygon");
        attribute("points", points(GeometryUtils.toYDown(polygon.getVertices())));
        pop();//w w w  .j av a 2s .  c o m
    } else if (object instanceof PolylineMapObject) {
        attribute("x", objectX).attribute("y", toYDown(objectY));
        Polyline polyline = ((PolylineMapObject) object).getPolyline();
        element("polyline");
        attribute("points", points(GeometryUtils.toYDown(polyline.getVertices())));
        pop();
    }

    if (props.containsKey("rotation"))
        attribute("rotation", getProperty(props, "rotation", 0f));
    if (props.containsKey("visible"))
        attribute("visible", object.isVisible() ? 1 : 0);
    if (object.getOpacity() != 1)
        attribute("opacity", object.getOpacity());

    @SuppressWarnings("unchecked")
    Array<String> excludedKeys = Pools.obtain(Array.class);
    excludedKeys.clear();
    excludedKeys.add("type");
    excludedKeys.add("gid");
    excludedKeys.add("x");
    excludedKeys.add("y");
    excludedKeys.add("width");
    excludedKeys.add("height");
    excludedKeys.add("rotation");
    excludedKeys.add("visible");
    excludedKeys.add("opacity");
    tmx(props, excludedKeys);
    excludedKeys.clear();
    Pools.free(excludedKeys);

    pop();
    return this;
}

From source file:org.saltosion.pixelprophecy.GameWorld.java

License:Open Source License

/**
 * Loads the TiledMap and creates required stuff for it (Box2D etc.)
 *//*  www  . ja  v a  2 s .com*/
private void initMap() {
    TiledMap currMap = this.mapLoader.getMap(currentMap);

    MapLayer objectLayer = currMap.getLayers().get(Globals.WALLS_LAYER);
    Array<RectangleMapObject> objects = objectLayer.getObjects().getByType(RectangleMapObject.class);
    Iterator<RectangleMapObject> objIter = objects.iterator();
    while (objIter.hasNext()) {
        RectangleMapObject obj = objIter.next();
        Rectangle rect = obj.getRectangle();
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set(rect.getX() * Globals.SPRITE_SCALE + rect.width * Globals.SPRITE_SCALE / 2,
                rect.getY() * Globals.SPRITE_SCALE + rect.height * Globals.SPRITE_SCALE / 2);

        Body body = world.createBody(bodyDef);

        PolygonShape polyShape = new PolygonShape();
        polyShape.setAsBox(rect.width * Globals.SPRITE_SCALE / 2, rect.height * Globals.SPRITE_SCALE / 2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = polyShape;
        fixtureDef.density = .5f;
        fixtureDef.friction = .4f;

        body.createFixture(fixtureDef);

        polyShape.dispose();
    }
    MapLayer entityLayer = currMap.getLayers().get(Globals.ENTITY_LAYER);
    MapObjects entities = entityLayer.getObjects();
    Iterator<MapObject> entityIter = entities.iterator();
    while (entityIter.hasNext()) {
        MapObject entityObj = entityIter.next();
        MapProperties properties = entityObj.getProperties();
        float x = properties.get("x", Float.class);
        float y = properties.get("y", Float.class);
        if (properties.containsKey("light-color") && properties.containsKey("light-distance")) { // This object emits light!
            String s = properties.get("light-color", String.class);
            float distance = Float.parseFloat(properties.get("light-distance", String.class));
            Color color = Globals.parseColor(s);

            PointLight pl = new PointLight(rayHandler, 1000, color, distance, x * Globals.SPRITE_SCALE,
                    y * Globals.SPRITE_SCALE);
            pl.setSoftnessLength(5f);
        }
        if (entityObj.getName().equals(spawnPointName) && player == null) {
            spawnPoint = new Vector2(x, y).scl(Globals.SPRITE_SCALE);
            player = initPlayer();
        }
    }
}

From source file:org.saltosion.pixelprophecy.systems.RenderingSystem.java

License:Open Source License

/**
 * Used to change the map//www.  j  a va  2  s .c o  m
 * @param map 
 */
public final void setMap(TiledMap map) {
    mapToRender = map;
    MapProperties properties = map.getProperties();
    mapRenderer = new OrthogonalTiledMapRenderer(mapToRender, Globals.SPRITE_SCALE);
    if (properties.containsKey("ambientlight")) {
        ambientLight = Globals.parseColor(properties.get("ambientlight", String.class));
    } else {
        ambientLight = new Color(0, 0, 0, 1);
    }
    rayHandler.setAmbientLight(ambientLight);
}

From source file:rosthouse.rosty.loader.MapLoader.java

private void loadCollisionLayer(MapLayer layer, PhysicsSystem physicsSystem, Engine engine) {
    for (MapObject object : layer.getObjects()) {
        MapProperties properties = object.getProperties();
        Entity mapObjectEntity = new Entity();
        engine.addEntity(mapObjectEntity);
        loadObject(object, physicsSystem, mapObjectEntity, false);
        if (properties.containsKey(MapObjects.TYPE.toString())) {
            String type = properties.get(MapObjects.TYPE.toString(), String.class);
            if (type.equals("WoodTile")) {
                ScriptComponent scriptComponent = new ScriptComponent();
                scriptComponent.addScript(GameConstants.START_COLLISION, new WoodScript());
                mapObjectEntity.add(scriptComponent);
            }/*from www .j  av a 2  s.  co  m*/
        }
        System.out.println(object);
    }
}