use of com.badlogic.gdx.maps.MapProperties in project libgdx by libgdx.
the class TmxMapLoader method loadTilemap.
/** Loads the map data, given the XML root element and an {@link ImageResolver} used to return the tileset Textures
* @param root the XML root element
* @param tmxFile the Filehandle of the tmx file
* @param imageResolver the {@link ImageResolver}
* @return the {@link TiledMap} */
protected TiledMap loadTilemap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
TiledMap map = new TiledMap();
String mapOrientation = root.getAttribute("orientation", null);
int mapWidth = root.getIntAttribute("width", 0);
int mapHeight = root.getIntAttribute("height", 0);
int tileWidth = root.getIntAttribute("tilewidth", 0);
int tileHeight = root.getIntAttribute("tileheight", 0);
int hexSideLength = root.getIntAttribute("hexsidelength", 0);
String staggerAxis = root.getAttribute("staggeraxis", null);
String staggerIndex = root.getAttribute("staggerindex", null);
String mapBackgroundColor = root.getAttribute("backgroundcolor", null);
MapProperties mapProperties = map.getProperties();
if (mapOrientation != null) {
mapProperties.put("orientation", mapOrientation);
}
mapProperties.put("width", mapWidth);
mapProperties.put("height", mapHeight);
mapProperties.put("tilewidth", tileWidth);
mapProperties.put("tileheight", tileHeight);
mapProperties.put("hexsidelength", hexSideLength);
if (staggerAxis != null) {
mapProperties.put("staggeraxis", staggerAxis);
}
if (staggerIndex != null) {
mapProperties.put("staggerindex", staggerIndex);
}
if (mapBackgroundColor != null) {
mapProperties.put("backgroundcolor", mapBackgroundColor);
}
mapTileWidth = tileWidth;
mapTileHeight = tileHeight;
mapWidthInPixels = mapWidth * tileWidth;
mapHeightInPixels = mapHeight * tileHeight;
if (mapOrientation != null) {
if ("staggered".equals(mapOrientation)) {
if (mapHeight > 1) {
mapWidthInPixels += tileWidth / 2;
mapHeightInPixels = mapHeightInPixels / 2 + tileHeight / 2;
}
}
}
Element properties = root.getChildByName("properties");
if (properties != null) {
loadProperties(map.getProperties(), properties);
}
Array<Element> tilesets = root.getChildrenByName("tileset");
for (Element element : tilesets) {
loadTileSet(map, element, tmxFile, imageResolver);
root.removeChild(element);
}
for (int i = 0, j = root.getChildCount(); i < j; i++) {
Element element = root.getChild(i);
String name = element.getName();
if (name.equals("layer")) {
loadTileLayer(map, element);
} else if (name.equals("objectgroup")) {
loadObjectGroup(map, element);
} else if (name.equals("imagelayer")) {
loadImageLayer(map, element, tmxFile, imageResolver);
}
}
return map;
}
use of com.badlogic.gdx.maps.MapProperties 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.MapProperties in project ultimate-java by pantinor.
the class DungeonRoomTiledMapLoader method load.
public TiledMap load() {
TiledMap map = new TiledMap();
MapProperties mapProperties = map.getProperties();
mapProperties.put("dungeonRoom", room);
mapProperties.put("orientation", "orthogonal");
mapProperties.put("width", mapWidth);
mapProperties.put("height", mapHeight);
mapProperties.put("tilewidth", tileWidth);
mapProperties.put("tileheight", tileHeight);
mapProperties.put("backgroundcolor", "#000000");
TiledMapTileLayer layer = new TiledMapTileLayer(mapWidth, mapHeight, tileWidth, tileHeight);
layer.setName("Map Layer");
layer.setVisible(true);
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
Tile ct = room.getTile(x, y);
// if (room.getTriggerAt(x, y) != null) ct = GameScreen.baseTileSet.getTileByIndex(3); //temp debugging triggers
Cell cell = new Cell();
TextureRegion tileRegion = atlas.findRegion(ct.getName());
TiledMapTile tmt = new StaticTiledMapTile(tileRegion);
tmt.setId(y * mapWidth + x);
cell.setTile(tmt);
layer.setCell(x, mapHeight - 1 - y, cell);
}
}
map.getLayers().add(layer);
try {
loadCombatPositions(map);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
use of com.badlogic.gdx.maps.MapProperties in project ultimate-java by pantinor.
the class UltimaTiledMapLoader method load.
// for intro map
public TiledMap load(byte[] bytes, int width, int height, TileSet ts, int tileDim) {
this.mapWidth = width;
this.mapHeight = height;
Tile[] tiles = new Tile[width * height];
int pos = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// convert a byte to an unsigned int value
int index = bytes[pos] & 0xff;
pos++;
Tile tile = ts.getTileByIndex(index);
if (tile == null) {
System.out.println("Tile index cannot be found: " + index + " using index 129 for black space.");
tile = ts.getTileByIndex(129);
}
tiles[x + y * width] = tile;
}
}
TiledMap map = new TiledMap();
MapProperties mapProperties = map.getProperties();
mapProperties.put("name", "none");
mapProperties.put("id", "none");
mapProperties.put("orientation", "orthogonal");
mapProperties.put("width", mapWidth);
mapProperties.put("height", mapHeight);
mapProperties.put("tilewidth", tileDim);
mapProperties.put("tileheight", tileDim);
mapProperties.put("backgroundcolor", "#000000");
TiledMapTileLayer layer = new TiledMapTileLayer(mapWidth, mapHeight, tileWidth, tileHeight);
layer.setName("Map Layer");
layer.setVisible(true);
int dx = 0, dy = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Tile ct = tiles[x + y * width];
Cell cell = new Cell();
Array<TextureAtlas.AtlasRegion> tileRegions = atlas.findRegions(ct.getName());
Array<StaticTiledMapTile> ar = new Array<>();
for (TextureAtlas.AtlasRegion r : tileRegions) {
ar.add(new StaticTiledMapTile(r));
}
if (ar.size == 0) {
System.out.println(ct.getName());
}
TiledMapTile tmt = null;
if (tileRegions.size > 1) {
tmt = new AnimatedTiledMapTile(.7f, ar);
} else {
tmt = ar.first();
}
tmt.setId(dy * mapWidth + dx);
cell.setTile(tmt);
layer.setCell(dx, mapHeight - 1 - dy, cell);
dx++;
}
dx = 0;
dy++;
}
map.getLayers().add(layer);
return map;
}
use of com.badlogic.gdx.maps.MapProperties in project Entitas-Java by Rubentxu.
the class Box2DMapObjectParser method createBody.
/**
* creates a {@link Body} in the given {@link World} from the given {@link MapObject}
*
* @param world the {@link World} to create the {@link Body} in
* @param mapObject the {@link MapObject} to parse the {@link Body} from
* @return the {@link Body} created in the given {@link World} from the given {@link MapObject}
*/
public Body createBody(World world, MapObject mapObject) {
MapProperties properties = mapObject.getProperties();
String type = properties.get("type", String.class);
if (!type.equals(aliases.body) && !type.equals(aliases.object))
throw new IllegalArgumentException("type of " + mapObject + " is \"" + type + "\" instead of \"" + aliases.body + "\" or \"" + aliases.object + "\"");
BodyDef bodyDef = new BodyDef();
bodyDef.type = properties.get(aliases.bodyType, String.class) != null ? properties.get(aliases.bodyType, String.class).equals(aliases.dynamicBody) ? BodyType.DynamicBody : properties.get(aliases.bodyType, String.class).equals(aliases.kinematicBody) ? BodyType.KinematicBody : properties.get(aliases.bodyType, String.class).equals(aliases.staticBody) ? BodyType.StaticBody : bodyDef.type : bodyDef.type;
bodyDef.active = getProperty(properties, aliases.active, bodyDef.active);
bodyDef.allowSleep = getProperty(properties, aliases.allowSleep, bodyDef.allowSleep);
bodyDef.angle = getProperty(properties, aliases.angle, bodyDef.angle);
bodyDef.angularDamping = getProperty(properties, aliases.angularDamping, bodyDef.angularDamping);
bodyDef.angularVelocity = getProperty(properties, aliases.angularVelocity, bodyDef.angularVelocity);
bodyDef.awake = getProperty(properties, aliases.awake, bodyDef.awake);
bodyDef.bullet = getProperty(properties, aliases.bullet, bodyDef.bullet);
bodyDef.fixedRotation = getProperty(properties, aliases.fixedRotation, bodyDef.fixedRotation);
bodyDef.gravityScale = getProperty(properties, aliases.gravityunitScale, bodyDef.gravityScale);
bodyDef.linearDamping = getProperty(properties, aliases.linearDamping, bodyDef.linearDamping);
bodyDef.linearVelocity.set(getProperty(properties, aliases.linearVelocityX, bodyDef.linearVelocity.x), getProperty(properties, aliases.linearVelocityY, bodyDef.linearVelocity.y));
bodyDef.position.set(getProperty(properties, "x", bodyDef.position.x) * unitScale, getProperty(properties, "y", bodyDef.position.y) * unitScale);
Body body = world.createBody(bodyDef);
String name = mapObject.getName();
if (bodies.containsKey(name)) {
int duplicate = 1;
while (bodies.containsKey(name + duplicate)) duplicate++;
name += duplicate;
}
Box2DPhysicsObject box2DPhysicsObject = new Box2DPhysicsObject(name, GRUPO.STATIC, body);
body.setUserData(box2DPhysicsObject);
bodies.put(name, body);
return body;
}
Aggregations