Search in sources :

Example 6 with MapLayer

use of com.badlogic.gdx.maps.MapLayer in project Entitas-Java by Rubentxu.

the class Box2DMapObjectParser method getHierarchy.

/**
     * @param map the {@link 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 Map}
     */
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;
}
Also used : MapLayer(com.badlogic.gdx.maps.MapLayer)

Example 7 with MapLayer

use of com.badlogic.gdx.maps.MapLayer in project libgdx by libgdx.

the class TiledMapPacker method stripUnusedTiles.

private void stripUnusedTiles() {
    int mapWidth = map.getProperties().get("width", Integer.class);
    int mapHeight = map.getProperties().get("height", Integer.class);
    int numlayers = map.getLayers().getCount();
    int bucketSize = mapWidth * mapHeight * numlayers;
    Iterator<MapLayer> it = map.getLayers().iterator();
    while (it.hasNext()) {
        MapLayer layer = it.next();
        // some layers can be plain MapLayer instances (ie. object groups), just ignore them
        if (layer instanceof TiledMapTileLayer) {
            TiledMapTileLayer tlayer = (TiledMapTileLayer) layer;
            for (int y = 0; y < mapHeight; ++y) {
                for (int x = 0; x < mapWidth; ++x) {
                    if (tlayer.getCell(x, y) != null) {
                        TiledMapTile tile = tlayer.getCell(x, y).getTile();
                        if (tile instanceof AnimatedTiledMapTile) {
                            AnimatedTiledMapTile aTile = (AnimatedTiledMapTile) tile;
                            for (StaticTiledMapTile t : aTile.getFrameTiles()) {
                                addTile(t, bucketSize);
                            }
                        }
                        // Adds non-animated tiles and the base animated tile
                        addTile(tile, bucketSize);
                    }
                }
            }
        }
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) MapLayer(com.badlogic.gdx.maps.MapLayer) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 8 with MapLayer

use of com.badlogic.gdx.maps.MapLayer in project libgdx by libgdx.

the class OrthoCachedTiledMapRenderer method render.

@Override
public void render() {
    if (!cached) {
        cached = true;
        count = 0;
        spriteCache.clear();
        final float extraWidth = viewBounds.width * overCache;
        final float extraHeight = viewBounds.height * overCache;
        cacheBounds.x = viewBounds.x - extraWidth;
        cacheBounds.y = viewBounds.y - extraHeight;
        cacheBounds.width = viewBounds.width + extraWidth * 2;
        cacheBounds.height = viewBounds.height + extraHeight * 2;
        for (MapLayer layer : map.getLayers()) {
            spriteCache.beginCache();
            if (layer instanceof TiledMapTileLayer) {
                renderTileLayer((TiledMapTileLayer) layer);
            } else if (layer instanceof TiledMapImageLayer) {
                renderImageLayer((TiledMapImageLayer) layer);
            }
            spriteCache.endCache();
        }
    }
    if (blending) {
        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    }
    spriteCache.begin();
    MapLayers mapLayers = map.getLayers();
    for (int i = 0, j = mapLayers.getCount(); i < j; i++) {
        MapLayer layer = mapLayers.get(i);
        if (layer.isVisible()) {
            spriteCache.draw(i);
            renderObjects(layer);
        }
    }
    spriteCache.end();
    if (blending)
        Gdx.gl.glDisable(GL20.GL_BLEND);
}
Also used : TiledMapImageLayer(com.badlogic.gdx.maps.tiled.TiledMapImageLayer) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) MapLayer(com.badlogic.gdx.maps.MapLayer) MapLayers(com.badlogic.gdx.maps.MapLayers)

Example 9 with MapLayer

use of com.badlogic.gdx.maps.MapLayer in project libgdx by libgdx.

the class TiledMapAssetManagerTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 10, 10);
    camera.zoom = 2;
    camera.update();
    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);
    font = new BitmapFont();
    batch = new SpriteBatch();
    assetManager = new AssetManager();
    assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
    assetManager.load("data/maps/tiled/isometric_grass_and_water.tmx", TiledMap.class);
    assetManager.finishLoading();
    map = assetManager.get("data/maps/tiled/isometric_grass_and_water.tmx");
    renderer = new IsometricTiledMapRenderer(map, 1f / 64f);
    String mapCustomValue = map.getProperties().get(MAP_PROPERTY_NAME, String.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + MAP_PROPERTY_NAME + ", Value : " + mapCustomValue);
    if (!MAP_PROPERTY_VALUE.equals(mapCustomValue)) {
        throw new RuntimeException("Failed to get map properties");
    }
    boolean boolCustomValue = map.getProperties().get(BOOL_PROPERTY_NAME, Boolean.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + BOOL_PROPERTY_NAME + ", Value : " + boolCustomValue);
    if (boolCustomValue != BOOL_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get boolean map properties");
    }
    int intCustomValue = map.getProperties().get(INT_PROPERTY_NAME, Integer.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + INT_PROPERTY_NAME + ", Value : " + intCustomValue);
    if (intCustomValue != INT_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get int map properties");
    }
    float floatCustomValue = map.getProperties().get(FLOAT_PROPERTY_NAME, Float.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + FLOAT_PROPERTY_NAME + ", Value : " + floatCustomValue);
    if (floatCustomValue != FLOAT_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get float map properties");
    }
    TiledMapTileSet tileset = map.getTileSets().getTileSet(0);
    String tilesetCustomValue = tileset.getProperties().get(TILESET_PROPERTY_NAME, String.class);
    if (!TILESET_PROPERTY_VALUE.equals(tilesetCustomValue)) {
        throw new RuntimeException("Failed to get tileset properties");
    }
    TiledMapTile tile = tileset.getTile(1);
    String tileCustomValue = tile.getProperties().get(TILE_PROPERTY_NAME, String.class);
    if (!TILE_PROPERTY_VALUE.equals(tileCustomValue)) {
        throw new RuntimeException("Failed to get tile properties");
    }
    MapLayer layer = map.getLayers().get(0);
    String layerCustomValue = layer.getProperties().get(LAYER_PROPERTY_NAME, String.class);
    if (!LAYER_PROPERTY_VALUE.equals(layerCustomValue)) {
        throw new RuntimeException("Failed to get layer properties");
    }
}
Also used : TmxMapLoader(com.badlogic.gdx.maps.tiled.TmxMapLoader) AssetManager(com.badlogic.gdx.assets.AssetManager) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) MapLayer(com.badlogic.gdx.maps.MapLayer) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) OrthoCamController(com.badlogic.gdx.tests.utils.OrthoCamController) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) InternalFileHandleResolver(com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) IsometricTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 10 with MapLayer

use of com.badlogic.gdx.maps.MapLayer in project Catacomb-Snatch by Catacomb-Snatch.

the class TmxLevelGenerator method generate.

@Override
public Level generate(Campaign campaign) {
    Level level = null;
    final GeneratorStringOption empty = (GeneratorStringOption) getOption("emptyTile");
    final boolean fill = empty.value != null && Tiles.isRegistered(empty.value);
    for (MapLayer layer : map.getLayers()) {
        if (layer instanceof TiledMapTileLayer) {
            // Tile layer - the first one always decides the map size
            final TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
            if (level == null) {
                level = new Level(campaign, this, tileLayer.getWidth(), tileLayer.getHeight());
            }
            for (int x = 0; x < tileLayer.getWidth(); x++) {
                for (int y = 0; y < tileLayer.getHeight(); y++) {
                    final Cell cell = tileLayer.getCell(x, y);
                    final String type = (cell == null || cell.getTile() == null) ? (fill ? empty.value : null) : (String) cell.getTile().getProperties().get("type");
                    if (type != null) {
                        Tiles.createAndAdd(type, level, x, y);
                    }
                }
            }
        } else {
            // Object layer for additional entities (monsters, spawn points, triggers)
            final MapObjects objects = layer.getObjects();
            for (int i = 0; i < objects.getCount(); i++) {
                MapObject obj = objects.get(i);
                String type = obj.getProperties().get("type", String.class);
                if ("spawnPoint".equalsIgnoreCase(type)) {
                    float x = obj.getProperties().get("x", Float.class);
                    float y = obj.getProperties().get("y", Float.class);
                    spawns.add(new Vector2(x, y).add(0.5f, 0.5f));
                }
            }
        }
    }
    // Safety check
    if (level == null) {
        return null;
    }
    return level;
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Vector2(com.badlogic.gdx.math.Vector2) MapLayer(com.badlogic.gdx.maps.MapLayer) Level(net.catacombsnatch.game.world.level.Level) GeneratorStringOption(net.catacombsnatch.game.world.level.generator.options.GeneratorStringOption) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) MapObjects(com.badlogic.gdx.maps.MapObjects) MapObject(com.badlogic.gdx.maps.MapObject)

Aggregations

MapLayer (com.badlogic.gdx.maps.MapLayer)11 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)6 TiledMapImageLayer (com.badlogic.gdx.maps.tiled.TiledMapImageLayer)4 MapLayers (com.badlogic.gdx.maps.MapLayers)2 MapObject (com.badlogic.gdx.maps.MapObject)2 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)2 AssetManager (com.badlogic.gdx.assets.AssetManager)1 InternalFileHandleResolver (com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver)1 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)1 MapObjects (com.badlogic.gdx.maps.MapObjects)1 EllipseMapObject (com.badlogic.gdx.maps.objects.EllipseMapObject)1 PolygonMapObject (com.badlogic.gdx.maps.objects.PolygonMapObject)1 RectangleMapObject (com.badlogic.gdx.maps.objects.RectangleMapObject)1 TextureMapObject (com.badlogic.gdx.maps.objects.TextureMapObject)1 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)1 TiledMapTileSet (com.badlogic.gdx.maps.tiled.TiledMapTileSet)1 TmxMapLoader (com.badlogic.gdx.maps.tiled.TmxMapLoader)1