Search in sources :

Example 6 with StaticTiledMapTile

use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile in project AmazingMaze by TheVirtualMachine.

the class Assets method loadFish.

/**
	 * Load the fish with the given parameters.
	 *
	 * @param atlas the {@link TextureAtlas} to load from.
	 * @param colourName the name of the colour in the atlas.
	 * @param colourID the ID of the colour in {@link TileIDs}.
	 */
private void loadFish(TextureAtlas atlas, String colourName, int colourID) {
    StaticTiledMapTile fish = new StaticTiledMapTile(atlas.findRegion(FISH + colourName));
    fish.setId(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.FISH, colourID));
    tiles.putTile(fish.getId(), fish);
}
Also used : StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 7 with StaticTiledMapTile

use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile 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 StaticTiledMapTile

use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile in project libgdx by libgdx.

the class AtlasTmxMapLoader method loadTileset.

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);
        int offsetX = 0;
        int offsetY = 0;
        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;
        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }
        String atlasFilePath = map.getProperties().get("atlas", String.class);
        if (atlasFilePath == null) {
            FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
            if (atlasFile.exists())
                atlasFilePath = atlasFile.name();
        }
        if (atlasFilePath == null) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }
        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, atlasFilePath);
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        String regionsName = name;
        for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        MapProperties props = tileset.getProperties();
        tileset.setName(name);
        props.put("firstgid", firstgid);
        props.put("imagesource", imageSource);
        props.put("imagewidth", imageWidth);
        props.put("imageheight", imageHeight);
        props.put("tilewidth", tilewidth);
        props.put("tileheight", tileheight);
        props.put("margin", margin);
        props.put("spacing", spacing);
        if (imageSource != null && imageSource.length() > 0) {
            int lastgid = firstgid + ((imageWidth / tilewidth) * (imageHeight / tileheight)) - 1;
            for (AtlasRegion region : atlas.findRegions(regionsName)) {
                // handle unused tile ids
                if (region != null) {
                    int tileid = region.index + 1;
                    if (tileid >= firstgid && tileid <= lastgid) {
                        StaticTiledMapTile tile = new StaticTiledMapTile(region);
                        tile.setId(tileid);
                        tile.setOffsetX(offsetX);
                        tile.setOffsetY(flipY ? -offsetY : offsetY);
                        tileset.putTile(tileid, tile);
                    }
                }
            }
        }
        for (Element tileElement : element.getChildrenByName("tile")) {
            int tileid = firstgid + tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(tileid);
            if (tile == null) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    // Is a tilemap with individual images.
                    String regionName = imageElement.getAttribute("source");
                    regionName = regionName.substring(0, regionName.lastIndexOf('.'));
                    AtlasRegion region = atlas.findRegion(regionName);
                    if (region == null)
                        throw new GdxRuntimeException("Tileset region not found: " + regionName);
                    tile = new StaticTiledMapTile(region);
                    tile.setId(tileid);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(tileid, tile);
                }
            }
            if (tile != null) {
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");
        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();
        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {
                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }
                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) MapProperties(com.badlogic.gdx.maps.MapProperties) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) LongArray(com.badlogic.gdx.utils.LongArray) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) IntArray(com.badlogic.gdx.utils.IntArray) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 9 with StaticTiledMapTile

use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile in project libgdx by libgdx.

the class TideMapLoader method loadTileSheet.

private void loadTileSheet(TiledMap map, Element element, FileHandle tideFile, ImageResolver imageResolver) {
    if (element.getName().equals("TileSheet")) {
        String id = element.getAttribute("Id");
        String description = element.getChildByName("Description").getText();
        String imageSource = element.getChildByName("ImageSource").getText();
        Element alignment = element.getChildByName("Alignment");
        String sheetSize = alignment.getAttribute("SheetSize");
        String tileSize = alignment.getAttribute("TileSize");
        String margin = alignment.getAttribute("Margin");
        String spacing = alignment.getAttribute("Spacing");
        String[] sheetSizeParts = sheetSize.split(" x ");
        int sheetSizeX = Integer.parseInt(sheetSizeParts[0]);
        int sheetSizeY = Integer.parseInt(sheetSizeParts[1]);
        String[] tileSizeParts = tileSize.split(" x ");
        int tileSizeX = Integer.parseInt(tileSizeParts[0]);
        int tileSizeY = Integer.parseInt(tileSizeParts[1]);
        String[] marginParts = margin.split(" x ");
        int marginX = Integer.parseInt(marginParts[0]);
        int marginY = Integer.parseInt(marginParts[1]);
        String[] spacingParts = margin.split(" x ");
        int spacingX = Integer.parseInt(spacingParts[0]);
        int spacingY = Integer.parseInt(spacingParts[1]);
        FileHandle image = getRelativeFileHandle(tideFile, imageSource);
        TextureRegion texture = imageResolver.getImage(image.path());
        TiledMapTileSets tilesets = map.getTileSets();
        int firstgid = 1;
        for (TiledMapTileSet tileset : tilesets) {
            firstgid += tileset.size();
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(id);
        tileset.getProperties().put("firstgid", firstgid);
        int gid = firstgid;
        int stopWidth = texture.getRegionWidth() - tileSizeX;
        int stopHeight = texture.getRegionHeight() - tileSizeY;
        for (int y = marginY; y <= stopHeight; y += tileSizeY + spacingY) {
            for (int x = marginX; x <= stopWidth; x += tileSizeX + spacingX) {
                TiledMapTile tile = new StaticTiledMapTile(new TextureRegion(texture, x, y, tileSizeX, tileSizeY));
                tile.setId(gid);
                tileset.putTile(gid++, tile);
            }
        }
        Element properties = element.getChildByName("Properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        tilesets.addTileSet(tileset);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 10 with StaticTiledMapTile

use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile in project AmazingMaze by TheVirtualMachine.

the class Assets method loadMapResources.

/** Helper method for loading the map resources. */
private void loadMapResources() {
    manager.load(GAME_ATLAS_LOCATION, TextureAtlas.class);
    manager.finishLoadingAsset(GAME_ATLAS_LOCATION);
    // Reference used for readability.
    TextureAtlas atlas = manager.get(Assets.GAME_ATLAS_LOCATION, TextureAtlas.class);
    tiles = new TiledMapTileSet();
    StaticTiledMapTile background = new StaticTiledMapTile(atlas.findRegion(BACKGROUND));
    StaticTiledMapTile barrier = new StaticTiledMapTile(atlas.findRegion(BARRIER));
    StaticTiledMapTile placeholder = new StaticTiledMapTile(atlas.findRegion(PLACEHOLDER));
    background.setId(TileIDs.computeID(TileIDs.BACKGROUND));
    barrier.setId(TileIDs.computeID(TileIDs.BARRIER));
    placeholder.setId(TileIDs.computeID(TileIDs.PLACEHOLDER));
    tiles.putTile(background.getId(), background);
    tiles.putTile(barrier.getId(), barrier);
    tiles.putTile(placeholder.getId(), placeholder);
    StaticTiledMapTile verticalOn = new StaticTiledMapTile(atlas.findRegion(VERTICAL + ON_MODIFIER));
    StaticTiledMapTile verticalOff = new StaticTiledMapTile(atlas.findRegion(VERTICAL + OFF_MODIFIER));
    StaticTiledMapTile verticalUnknown = new StaticTiledMapTile(atlas.findRegion(VERTICAL + UNKNOWN_MODIFIER));
    verticalOn.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.ON));
    verticalOff.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.OFF));
    verticalUnknown.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.UNKNOWN));
    tiles.putTile(verticalOn.getId(), verticalOn);
    tiles.putTile(verticalOff.getId(), verticalOff);
    tiles.putTile(verticalUnknown.getId(), verticalUnknown);
    loadGates(atlas, ON_MODIFIER, UP_MODIFIER, TileIDs.ON, TileIDs.UP_GATE);
    loadGates(atlas, ON_MODIFIER, DOWN_MODIFIER, TileIDs.ON, TileIDs.DOWN_GATE);
    loadGates(atlas, OFF_MODIFIER, UP_MODIFIER, TileIDs.OFF, TileIDs.UP_GATE);
    loadGates(atlas, OFF_MODIFIER, DOWN_MODIFIER, TileIDs.OFF, TileIDs.DOWN_GATE);
    loadGates(atlas, UNKNOWN_MODIFIER, UP_MODIFIER, TileIDs.UNKNOWN, TileIDs.UP_GATE);
    loadGates(atlas, UNKNOWN_MODIFIER, DOWN_MODIFIER, TileIDs.UNKNOWN, TileIDs.DOWN_GATE);
    StaticTiledMapTile turnOnUpLeft = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + UP_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOffUpLeft = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + UP_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOnUpRight = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + UP_MODIFIER + RIGHT_MODIFIER));
    StaticTiledMapTile turnOffUpRight = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + UP_MODIFIER + RIGHT_MODIFIER));
    StaticTiledMapTile turnOnDownLeft = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + DOWN_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOffDownLeft = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + DOWN_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOnDownRight = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + DOWN_MODIFIER + RIGHT_MODIFIER));
    StaticTiledMapTile turnOffDownRight = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + DOWN_MODIFIER + RIGHT_MODIFIER));
    turnOnUpLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.UP_LEFT));
    turnOffUpLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.UP_LEFT));
    turnOnUpRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.UP_RIGHT));
    turnOffUpRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.UP_RIGHT));
    turnOnDownLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.DOWN_LEFT));
    turnOffDownLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.DOWN_LEFT));
    turnOnDownRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.DOWN_RIGHT));
    turnOffDownRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.DOWN_RIGHT));
    tiles.putTile(turnOnUpLeft.getId(), turnOnUpLeft);
    tiles.putTile(turnOffUpLeft.getId(), turnOffUpLeft);
    tiles.putTile(turnOnUpRight.getId(), turnOnUpRight);
    tiles.putTile(turnOffUpRight.getId(), turnOffUpRight);
    tiles.putTile(turnOnDownLeft.getId(), turnOnDownLeft);
    tiles.putTile(turnOffDownLeft.getId(), turnOffDownLeft);
    tiles.putTile(turnOnDownRight.getId(), turnOnDownRight);
    tiles.putTile(turnOffDownRight.getId(), turnOffDownRight);
    loadFish(atlas, BLUE_MODIFIER, TileIDs.BLUE);
    loadFish(atlas, PURPLE_MODIFIER, TileIDs.PURPLE);
    loadFish(atlas, GREEN_MODIFIER, TileIDs.GREEN);
    loadFish(atlas, RED_MODIFIER, TileIDs.RED);
    loadFish(atlas, ORANGE_MODIFIER, TileIDs.ORANGE);
    StaticTiledMapTile cheese = new StaticTiledMapTile(atlas.findRegion(CHEESE));
    cheese.setId(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.CHEESE));
    tiles.putTile(cheese.getId(), cheese);
}
Also used : TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Aggregations

StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)10 AnimatedTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)4 Element (com.badlogic.gdx.utils.XmlReader.Element)4 FileHandle (com.badlogic.gdx.files.FileHandle)3 Texture (com.badlogic.gdx.graphics.Texture)3 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)3 Cell (com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)3 Array (com.badlogic.gdx.utils.Array)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 MapLayers (com.badlogic.gdx.maps.MapLayers)2 MapProperties (com.badlogic.gdx.maps.MapProperties)2 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)2 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)2 OrthoCamController (com.badlogic.gdx.tests.utils.OrthoCamController)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 IntArray (com.badlogic.gdx.utils.IntArray)2 IOException (java.io.IOException)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1