Example usage for com.badlogic.gdx.maps.tiled.tiles StaticTiledMapTile StaticTiledMapTile

List of usage examples for com.badlogic.gdx.maps.tiled.tiles StaticTiledMapTile StaticTiledMapTile

Introduction

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

Prototype

public StaticTiledMapTile(StaticTiledMapTile copy) 

Source Link

Document

Copy constructor

Usage

From source file:ca.hiphiparray.amazingmaze.Assets.java

License:Open Source License

/** Helper method for loading the map resources. */
private void loadMapResources() {
    manager.load(GAME_ATLAS_LOCATION, TextureAtlas.class);
    manager.finishLoadingAsset(GAME_ATLAS_LOCATION);
    TextureAtlas atlas = manager.get(Assets.GAME_ATLAS_LOCATION, TextureAtlas.class); // Reference used for readability.

    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//from   w  ww . j  a va  2  s  . co  m
            .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);
}

From source file:ca.hiphiparray.amazingmaze.Assets.java

License:Open Source License

/**
 * 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}.
 *//*from   w  ww. j  a  v a2 s .  c om*/
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);
}

From source file:ca.hiphiparray.amazingmaze.Assets.java

License:Open Source License

/**
 * Load all gates for the given parameters.
 *
 * @param atlas the {@link TextureAtlas} to load from.
 * @param electricStateName the name of the electrical state of the gates to load.
 * @param directionName the name of the direction of the gates to load.
 * @param electricID the ID of the electrical state of the gates to load.
 * @param directionID the directional ID of the gates to load.
 *///  w ww .j  av a2 s.c  om
private void loadGates(TextureAtlas atlas, String electricStateName, String directionName, int electricID,
        int directionID) {
    StaticTiledMapTile and = new StaticTiledMapTile(
            atlas.findRegion(AND_GATE + electricStateName + directionName));
    StaticTiledMapTile nand = new StaticTiledMapTile(
            atlas.findRegion(NAND_GATE + electricStateName + directionName));
    StaticTiledMapTile or = new StaticTiledMapTile(
            atlas.findRegion(OR_GATE + electricStateName + directionName));
    StaticTiledMapTile nor = new StaticTiledMapTile(
            atlas.findRegion(NOR_GATE + electricStateName + directionName));
    StaticTiledMapTile xor = new StaticTiledMapTile(
            atlas.findRegion(XOR_GATE + electricStateName + directionName));

    and.setId(TileIDs.computeID(TileIDs.GATE_RANGE, TileIDs.AND_GATE, directionID, electricID));
    nand.setId(TileIDs.computeID(TileIDs.GATE_RANGE, TileIDs.NAND_GATE, directionID, electricID));
    or.setId(TileIDs.computeID(TileIDs.GATE_RANGE, TileIDs.OR_GATE, directionID, electricID));
    nor.setId(TileIDs.computeID(TileIDs.GATE_RANGE, TileIDs.NOR_GATE, directionID, electricID));
    xor.setId(TileIDs.computeID(TileIDs.GATE_RANGE, TileIDs.XOR_GATE, directionID, electricID));

    tiles.putTile(and.getId(), and);
    tiles.putTile(nand.getId(), nand);
    tiles.putTile(or.getId(), or);
    tiles.putTile(nor.getId(), nor);
    tiles.putTile(xor.getId(), xor);
}

From source file:com.badlogic.gdx.tests.bench.TiledMapBench.java

License:Apache License

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 320, 320);
    camera.update();/*from w w w  . ja  va 2 s. c  o  m*/

    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);

    font = new BitmapFont();
    batch = new SpriteBatch();

    {
        tiles = new Texture(Gdx.files.internal("data/maps/tiled/tiles.png"));
        TextureRegion[][] splitTiles = TextureRegion.split(tiles, 32, 32);
        map = new TiledMap();
        MapLayers layers = map.getLayers();
        for (int l = 0; l < 20; l++) {
            TiledMapTileLayer layer = new TiledMapTileLayer(150, 100, 32, 32);
            for (int x = 0; x < 150; x++) {
                for (int y = 0; y < 100; y++) {
                    int ty = (int) (Math.random() * splitTiles.length);
                    int tx = (int) (Math.random() * splitTiles[ty].length);
                    Cell cell = new Cell();
                    cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx]));
                    layer.setCell(x, y, cell);
                }
            }
            layers.add(layer);
        }
    }

    renderer = new OrthogonalTiledMapRenderer(map);

}

From source file:com.betmansmall.game.gameLogic.mapLoader.MapLoader.java

License:Apache License

/** Loads the specified tileset data, adding it to the collection of the specified map, given the XML element, the tmxFile and
 * an {@link ImageResolver} used to retrieve the tileset Textures.
 *
 * <p>//w w  w  .j  av  a  2s.  c  o  m
 * Default tileset's property keys that are loaded by default are:
 * </p>
 *
 * <ul>
 * <li><em>firstgid</em>, (int, defaults to 1) the first valid global id used for tile numbering</li>
 * <li><em>imagesource</em>, (String, defaults to empty string) the tileset source image filename</li>
 * <li><em>imagewidth</em>, (int, defaults to 0) the tileset source image width</li>
 * <li><em>imageheight</em>, (int, defaults to 0) the tileset source image height</li>
 * <li><em>tilewidth</em>, (int, defaults to 0) the tile width</li>
 * <li><em>tileheight</em>, (int, defaults to 0) the tile height</li>
 * <li><em>margin</em>, (int, defaults to 0) the tileset margin</li>
 * <li><em>spacing</em>, (int, defaults to 0) the tileset spacing</li>
 * </ul>
 *
 * <p>
 * The values are extracted from the specified Tmx file, if a value can't be found then the default is used.
 * </p>
 * @param map the Map whose tilesets collection will be populated
 * @param element the XML element identifying the tileset to load
 * @param tmxFile the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver} */
protected void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    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);
            }
        }

        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(name);
        tileset.getProperties().put("firstgid", firstgid);
        if (image != null) {
            TextureRegion texture = imageResolver.getImage(image.path());

            MapProperties props = tileset.getProperties();
            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);

            int stopWidth = texture.getRegionWidth() - tilewidth;
            int stopHeight = texture.getRegionHeight() - tileheight;

            int id = firstgid;

            for (int y = margin; y <= stopHeight; y += tileheight + spacing) {
                for (int x = margin; x <= stopWidth; x += tilewidth + spacing) {
                    TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight);
                    TiledMapTile tile = new StaticTiledMapTile(tileRegion);
                    tile.setId(id);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(id++, tile);
                }
            }
        } else {
            Array<Element> tileElements = element.getChildrenByName("tile");
            for (Element tileElement : tileElements) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tmxFile, imageSource);
                }
                TextureRegion texture = imageResolver.getImage(image.path());
                TiledMapTile tile = new StaticTiledMapTile(texture);
                tile.setId(firstgid + tileElement.getIntAttribute("id"));
                tile.setOffsetX(offsetX);
                tile.setOffsetY(flipY ? -offsetY : offsetY);
                tileset.putTile(tile.getId(), tile);
            }
        }
        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);
    }
}

From source file:com.skettios.loader.TmxMapLoaderFixed.java

License:Apache License

/**
 * Loads the specified tileset data, adding it to the collection of the specified map, given the XML element, the tmxFile and
 * an {@link ImageResolver} used to retrieve the tileset Textures.
 * <p/>/* w w w .j  av  a 2s  . c o m*/
 * <p>
 * Default tileset's property keys that are loaded by default are:
 * </p>
 * <p/>
 * <ul>
 * <li><em>firstgid</em>, (int, defaults to 1) the first valid global id used for tile numbering</li>
 * <li><em>imagesource</em>, (String, defaults to empty string) the tileset source image filename</li>
 * <li><em>imagewidth</em>, (int, defaults to 0) the tileset source image width</li>
 * <li><em>imageheight</em>, (int, defaults to 0) the tileset source image height</li>
 * <li><em>tilewidth</em>, (int, defaults to 0) the tile width</li>
 * <li><em>tileheight</em>, (int, defaults to 0) the tile height</li>
 * <li><em>margin</em>, (int, defaults to 0) the tileset margin</li>
 * <li><em>spacing</em>, (int, defaults to 0) the tileset spacing</li>
 * </ul>
 * <p/>
 * <p>
 * The values are extracted from the specified Tmx file, if a value can't be found then the default is used.
 * </p>
 *
 * @param map           the Map whose tilesets collection will be populated
 * @param element       the XML element identifying the tileset to load
 * @param tmxFile       the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver}
 */
protected void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    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 = getRelativeFileHandleFixed(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 = getRelativeFileHandleFixed(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 = getRelativeFileHandleFixed(tmxFile, imageSource);
            }
        }

        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(name);
        tileset.getProperties().put("firstgid", firstgid);
        if (image != null) {
            TextureRegion texture = imageResolver.getImage(image.path());

            MapProperties props = tileset.getProperties();
            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);

            int stopWidth = texture.getRegionWidth() - tilewidth;
            int stopHeight = texture.getRegionHeight() - tileheight;

            int id = firstgid;

            for (int y = margin; y <= stopHeight; y += tileheight + spacing) {
                for (int x = margin; x <= stopWidth; x += tilewidth + spacing) {
                    TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight);
                    TiledMapTile tile = new StaticTiledMapTile(tileRegion);
                    tile.setId(id);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(-offsetY);
                    tileset.putTile(id++, tile);
                }
            }
        } else {
            Array<Element> tileElements = element.getChildrenByName("tile");
            for (Element tileElement : tileElements) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandleFixed(tmxFile, imageSource);
                }
                TextureRegion texture = imageResolver.getImage(image.path());
                TiledMapTile tile = new StaticTiledMapTile(texture);
                tile.setId(firstgid + tileElement.getIntAttribute("id"));
                tile.setOffsetX(offsetX);
                tile.setOffsetY(-offsetY);
                tileset.putTile(tile.getId(), tile);
            }
        }
        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);
    }
}

From source file:de.bitowl.advent.game2.MyAtlasTmxMapLoader.java

License:Apache License

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver,
        AtlasTiledMapLoaderParameters parameter) {
    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);

        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;

        FileHandle image = null;//  www. j ava 2 s  .co m
        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);
                imageSource = element.getChildByName("image").getAttribute("source");
                imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
                imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            imageSource = element.getChildByName("image").getAttribute("source");
            imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
            imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
        }

        if (!map.getProperties().containsKey("atlas")) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }

        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, map.getProperties().get("atlas", String.class));
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        //String regionsName = atlasHandle.nameWithoutExtension();
        String regionsName = name;

        if (parameter != null && parameter.forceTextureFilters) {
            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);

        Array<AtlasRegion> regions = atlas.findRegions(regionsName);
        System.out.println(regions.size);
        for (AtlasRegion region : regions) {
            // handle unused tile ids
            if (region != null) {
                StaticTiledMapTile tile = new StaticTiledMapTile(region);

                if (!yUp) {
                    region.flip(false, true);
                }

                int tileid = firstgid + region.index;
                tile.setId(tileid);
                tileset.putTile(tileid, tile);
                System.out.println("put tile " + tileid);
            }
        }

        Array<Element> tileElements = element.getChildrenByName("tile");

        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            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);
                }
            }
        }

        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        System.out.println("add tileset to map: " + tileset.getName());
        map.getTileSets().addTileSet(tileset);
    }
}

From source file:gui.entity.bomb.Barrel.java

@Override
public void render() {
    //To make sure no bomb gets placed into wall
    if (!map.isCellBlocked(new MapCellCoordinates(pos.getX(), pos.getY())) && !isExploded) {
        //Execute fuse sound
        if (soundId == -1) {
            placeSound = AudioManager.getBarrelPlace();
            soundId = placeSound.play();
        }//from w  ww.  j  a  v a 2 s  .  c  o m

        Player player = entityManager.getPlayerManager().getCurrentPlayerObject();
        if (player != null && placeSound != null) {
            player.playSoundInDistance(placeSound, soundId, pos, Constants.BARRELFUSEMOD);
        }

        //Check if bomb has been hit by deadly tile
        if (map.isCellDeadly(new MapCellCoordinates(pos.getX(), pos.getY()))
                && hasBombTouchedDeadlyTile == false && timerTillExplosion < explosionTime) {
            //To delay the explosion after beeing hit from another bomb
            timerTillExplosion = explosionTime - delayExplodeAfterHitByBomb;

            hasBombTouchedDeadlyTile = true;
        }

        //If time to explode or deadly tile has been touched
        if (timerTillExplosion >= explosionTime) {
            placeSound.stop();
            explode(AudioManager.getBigBombExplosion());

            //Delete explosion effect after a while
            if (timerTillExplosionDelete >= explosionDuration) {
                deleteCubicExplosionEffect();

                //Object gets delete only set if everything is done.
                this.isExploded = true;
            } else {
                //Add passed time to timer
                timerTillExplosionDelete += Constants.DELTATIME;
            }

        } else if (!hasBombTouchedDeadlyTile)//Creates bomb animation
        {
            //Create new cell and set its animation texture
            TiledMapTileLayer.Cell cell = new TiledMapTileLayer.Cell();
            cell.setTile(new StaticTiledMapTile(animEffects.getFrame(bombAnim, true)));
            cell.getTile().getProperties().put("bomb", null);

            //Set bomb into bomb layer
            map.getBombLayer().setCell(super.cellPos.getX(), super.cellPos.getY(), cell);
        }

        //Add passed time to timer
        if (hasBombTouchedDeadlyTile == true) {
            timerTillExplosion += Constants.DELTATIME;
        }

    } else //If bomb placed in wall
    {
        //Delete bomb object.
        this.isExploded = true;
    }
}

From source file:gui.entity.bomb.BlackHole.java

private void animateBomb() {
    //Create new cell and set its animation texture
    TiledMapTileLayer.Cell cell = new TiledMapTileLayer.Cell();
    cell.setTile(new StaticTiledMapTile(animEffects.getFrame(bombAnim, true)));
    cell.getTile().getProperties().put("bomb", null);

    //Set bomb into bomb layer
    map.getBombLayer().setCell(super.cellPos.getX(), super.cellPos.getY(), cell);
}

From source file:gui.entity.bomb.Bomb.java

/**
 * Deltes block on given cell position. Spawns also a coin randomly on destroyed blocks.
 * If it is undestructable nothing happens and false gets returned.
 * @param x: Cell position on x axis/*from   w w w . j a va 2  s . co m*/
 * @param y: Cell position on y axis
 * @return boolean
 */
protected boolean deleteBlock(MapCellCoordinates localCellPos) {
    Cell currentCell = blockLayer.getCell(localCellPos.getX(), localCellPos.getY());

    if (currentCell != null) {
        //If block is undestructable
        if (currentCell.getTile().getProperties().containsKey("undestructable")) {
            return false;

        } else {
            //Delete block with empty texture
            Cell cell = new Cell();
            cell.setTile(new StaticTiledMapTile(emptyBlock));
            map.getBlockLayer().setCell(localCellPos.getX(), localCellPos.getY(), cell);

            /**---------------------RANDOM COIN---------------------**/
            //Check for a bug and if main player placed that bomb
            if (currentCell.getTile().getId() != cell.getTile().getId() && playerId == Constants.PLAYERID) {
                dropFromBlock(localCellPos);
            }
        }
    }

    // If there is no block 
    return true;
}