use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile 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.StaticTiledMapTile 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.StaticTiledMapTile in project libgdx by libgdx.
the class TiledMapBench method create.
@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();
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);
}
use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile in project libgdx by libgdx.
the class HexagonalTiledMapTest method create.
@Override
public void create() {
super.create();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, (w / h) * 480, 480);
camera.update();
cameraController = new OrthoCamController(camera);
Gdx.input.setInputProcessor(cameraController);
hexture = new Texture(Gdx.files.internal("data/maps/tiled/hex/hexes.png"));
TextureRegion[][] hexes = TextureRegion.split(hexture, 112, 97);
map = new TiledMap();
MapLayers layers = map.getLayers();
TiledMapTile[] tiles = new TiledMapTile[3];
tiles[0] = new StaticTiledMapTile(new TextureRegion(hexes[0][0]));
tiles[1] = new StaticTiledMapTile(new TextureRegion(hexes[0][1]));
tiles[2] = new StaticTiledMapTile(new TextureRegion(hexes[1][0]));
for (int l = 0; l < 1; l++) {
TiledMapTileLayer layer = new TiledMapTileLayer(45, 30, 112, 97);
for (int y = 0; y < 30; y++) {
for (int x = 0; x < 45; x++) {
int id = (int) (Math.random() * 3);
Cell cell = new Cell();
cell.setTile(tiles[id]);
layer.setCell(x, y, cell);
}
}
layers.add(layer);
}
renderer = new HexagonalTiledMapRenderer(map);
}
use of com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile in project AmazingMaze by TheVirtualMachine.
the class Assets method loadGates.
/**
* 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.
*/
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);
}
Aggregations