use of com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile in project libgdx by libgdx.
the class TideMapLoader method loadLayer.
private void loadLayer(TiledMap map, Element element) {
if (element.getName().equals("Layer")) {
String id = element.getAttribute("Id");
String visible = element.getAttribute("Visible");
Element dimensions = element.getChildByName("Dimensions");
String layerSize = dimensions.getAttribute("LayerSize");
String tileSize = dimensions.getAttribute("TileSize");
String[] layerSizeParts = layerSize.split(" x ");
int layerSizeX = Integer.parseInt(layerSizeParts[0]);
int layerSizeY = Integer.parseInt(layerSizeParts[1]);
String[] tileSizeParts = tileSize.split(" x ");
int tileSizeX = Integer.parseInt(tileSizeParts[0]);
int tileSizeY = Integer.parseInt(tileSizeParts[1]);
TiledMapTileLayer layer = new TiledMapTileLayer(layerSizeX, layerSizeY, tileSizeX, tileSizeY);
layer.setName(id);
layer.setVisible(visible.equalsIgnoreCase("True"));
Element tileArray = element.getChildByName("TileArray");
Array<Element> rows = tileArray.getChildrenByName("Row");
TiledMapTileSets tilesets = map.getTileSets();
TiledMapTileSet currentTileSet = null;
int firstgid = 0;
int x, y;
for (int row = 0, rowCount = rows.size; row < rowCount; row++) {
Element currentRow = rows.get(row);
y = rowCount - 1 - row;
x = 0;
for (int child = 0, childCount = currentRow.getChildCount(); child < childCount; child++) {
Element currentChild = currentRow.getChild(child);
String name = currentChild.getName();
if (name.equals("TileSheet")) {
currentTileSet = tilesets.getTileSet(currentChild.getAttribute("Ref"));
firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
} else if (name.equals("Null")) {
x += currentChild.getIntAttribute("Count");
} else if (name.equals("Static")) {
Cell cell = new Cell();
cell.setTile(currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
layer.setCell(x++, y, cell);
} else if (name.equals("Animated")) {
// Create an AnimatedTile
int interval = currentChild.getInt("Interval");
Element frames = currentChild.getChildByName("Frames");
Array<StaticTiledMapTile> frameTiles = new Array<StaticTiledMapTile>();
for (int frameChild = 0, frameChildCount = frames.getChildCount(); frameChild < frameChildCount; frameChild++) {
Element frame = frames.getChild(frameChild);
String frameName = frame.getName();
if (frameName.equals("TileSheet")) {
currentTileSet = tilesets.getTileSet(frame.getAttribute("Ref"));
firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
} else if (frameName.equals("Static")) {
frameTiles.add((StaticTiledMapTile) currentTileSet.getTile(firstgid + frame.getIntAttribute("Index")));
}
}
Cell cell = new Cell();
cell.setTile(new AnimatedTiledMapTile(interval / 1000f, frameTiles));
// TODO: Reuse existing animated tiles
layer.setCell(x++, y, cell);
}
}
}
Element properties = element.getChildByName("Properties");
if (properties != null) {
loadProperties(layer.getProperties(), properties);
}
map.getLayers().add(layer);
}
}
use of com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile in project libgdx by libgdx.
the class TmxMapLoader method loadTileSet.
/** 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>
* 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);
if (source != null) {
image = getRelativeFileHandle(getRelativeFileHandle(tmxFile, source), imageSource);
} else {
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);
}
}
use of com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile 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);
}
}
}
}
}
}
use of com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile 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);
}
}
use of com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile in project libgdx by libgdx.
the class HexagonalTiledMapRenderer method renderCell.
/** render a single cell */
private void renderCell(final TiledMapTileLayer.Cell cell, final float x, final float y, final float color) {
if (cell != null) {
final TiledMapTile tile = cell.getTile();
if (tile != null) {
if (tile instanceof AnimatedTiledMapTile)
return;
final boolean flipX = cell.getFlipHorizontally();
final boolean flipY = cell.getFlipVertically();
final int rotations = cell.getRotation();
TextureRegion region = tile.getTextureRegion();
float x1 = x + tile.getOffsetX() * unitScale;
float y1 = y + tile.getOffsetY() * unitScale;
float x2 = x1 + region.getRegionWidth() * unitScale;
float y2 = y1 + region.getRegionHeight() * unitScale;
float u1 = region.getU();
float v1 = region.getV2();
float u2 = region.getU2();
float v2 = region.getV();
vertices[X1] = x1;
vertices[Y1] = y1;
vertices[C1] = color;
vertices[U1] = u1;
vertices[V1] = v1;
vertices[X2] = x1;
vertices[Y2] = y2;
vertices[C2] = color;
vertices[U2] = u1;
vertices[V2] = v2;
vertices[X3] = x2;
vertices[Y3] = y2;
vertices[C3] = color;
vertices[U3] = u2;
vertices[V3] = v2;
vertices[X4] = x2;
vertices[Y4] = y1;
vertices[C4] = color;
vertices[U4] = u2;
vertices[V4] = v1;
if (flipX) {
float temp = vertices[U1];
vertices[U1] = vertices[U3];
vertices[U3] = temp;
temp = vertices[U2];
vertices[U2] = vertices[U4];
vertices[U4] = temp;
}
if (flipY) {
float temp = vertices[V1];
vertices[V1] = vertices[V3];
vertices[V3] = temp;
temp = vertices[V2];
vertices[V2] = vertices[V4];
vertices[V4] = temp;
}
if (rotations == 2) {
float tempU = vertices[U1];
vertices[U1] = vertices[U3];
vertices[U3] = tempU;
tempU = vertices[U2];
vertices[U2] = vertices[U4];
vertices[U4] = tempU;
float tempV = vertices[V1];
vertices[V1] = vertices[V3];
vertices[V3] = tempV;
tempV = vertices[V2];
vertices[V2] = vertices[V4];
vertices[V4] = tempV;
}
batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
}
}
}
Aggregations