use of com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject in project libgdx by libgdx.
the class TiledMapObjectLoadingTest method render.
@Override
public void render() {
Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);
batch.setProjectionMatrix(camera.combined);
shapeRenderer.setColor(Color.BLUE);
Gdx.gl20.glLineWidth(2);
MapLayer layer = map.getLayers().get("Objects");
AnimatedTiledMapTile.updateAnimationBaseTime();
for (MapObject mapObject : layer.getObjects()) {
if (mapObject instanceof TiledMapTileMapObject) {
batch.begin();
TiledMapTileMapObject tmtObject = (TiledMapTileMapObject) mapObject;
TextureRegion textureRegion = tmtObject.getTile().getTextureRegion();
// TilEd rotation is clockwise, we need counter-clockwise.
float rotation = -tmtObject.getRotation();
float scaleX = tmtObject.getScaleX();
float scaleY = tmtObject.getScaleY();
float xPos = tmtObject.getX();
float yPos = tmtObject.getY();
textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
batch.draw(textureRegion, xPos, yPos, tmtObject.getOriginX() * scaleX, tmtObject.getOriginY() * scaleY, textureRegion.getRegionWidth() * scaleX, textureRegion.getRegionHeight() * scaleY, 1f, 1f, rotation);
// We flip back to the original state.
textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
batch.end();
} else if (mapObject instanceof EllipseMapObject) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
shapeRenderer.ellipse(ellipse.x, ellipse.y, ellipse.width, ellipse.height);
shapeRenderer.end();
} else if (mapObject instanceof RectangleMapObject) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
shapeRenderer.end();
} else if (mapObject instanceof PolygonMapObject) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
shapeRenderer.polygon(polygon.getTransformedVertices());
shapeRenderer.end();
}
}
batch.begin();
font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
batch.end();
}
use of com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject in project libgdx by libgdx.
the class BaseTmxMapLoader method loadObject.
protected void loadObject(TiledMap map, MapLayer layer, Element element) {
if (element.getName().equals("object")) {
MapObject object = null;
float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;
float x = element.getFloatAttribute("x", 0) * scaleX;
float y = (flipY ? (mapHeightInPixels - element.getFloatAttribute("y", 0)) : element.getFloatAttribute("y", 0)) * scaleY;
float width = element.getFloatAttribute("width", 0) * scaleX;
float height = element.getFloatAttribute("height", 0) * scaleY;
if (element.getChildCount() > 0) {
Element child = null;
if ((child = element.getChildByName("polygon")) != null) {
String[] points = child.getAttribute("points").split(" ");
float[] vertices = new float[points.length * 2];
for (int i = 0; i < points.length; i++) {
String[] point = points[i].split(",");
vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
}
Polygon polygon = new Polygon(vertices);
polygon.setPosition(x, y);
object = new PolygonMapObject(polygon);
} else if ((child = element.getChildByName("polyline")) != null) {
String[] points = child.getAttribute("points").split(" ");
float[] vertices = new float[points.length * 2];
for (int i = 0; i < points.length; i++) {
String[] point = points[i].split(",");
vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
}
Polyline polyline = new Polyline(vertices);
polyline.setPosition(x, y);
object = new PolylineMapObject(polyline);
} else if ((child = element.getChildByName("ellipse")) != null) {
object = new EllipseMapObject(x, flipY ? y - height : y, width, height);
}
}
if (object == null) {
String gid = null;
if ((gid = element.getAttribute("gid", null)) != null) {
int id = (int) Long.parseLong(gid);
boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
TiledMapTile tile = map.getTileSets().getTile(id & ~MASK_CLEAR);
TiledMapTileMapObject tiledMapTileMapObject = new TiledMapTileMapObject(tile, flipHorizontally, flipVertically);
TextureRegion textureRegion = tiledMapTileMapObject.getTextureRegion();
tiledMapTileMapObject.getProperties().put("gid", id);
tiledMapTileMapObject.setX(x);
tiledMapTileMapObject.setY(flipY ? y : y - height);
float objectWidth = element.getFloatAttribute("width", textureRegion.getRegionWidth());
float objectHeight = element.getFloatAttribute("height", textureRegion.getRegionHeight());
tiledMapTileMapObject.setScaleX(scaleX * (objectWidth / textureRegion.getRegionWidth()));
tiledMapTileMapObject.setScaleY(scaleY * (objectHeight / textureRegion.getRegionHeight()));
tiledMapTileMapObject.setRotation(element.getFloatAttribute("rotation", 0));
object = tiledMapTileMapObject;
} else {
object = new RectangleMapObject(x, flipY ? y - height : y, width, height);
}
}
object.setName(element.getAttribute("name", null));
String rotation = element.getAttribute("rotation", null);
if (rotation != null) {
object.getProperties().put("rotation", Float.parseFloat(rotation));
}
String type = element.getAttribute("type", null);
if (type != null) {
object.getProperties().put("type", type);
}
int id = element.getIntAttribute("id", 0);
if (id != 0) {
object.getProperties().put("id", id);
}
object.getProperties().put("x", x);
if (object instanceof TiledMapTileMapObject) {
object.getProperties().put("y", y);
} else {
object.getProperties().put("y", (flipY ? y - height : y));
}
object.getProperties().put("width", width);
object.getProperties().put("height", height);
object.setVisible(element.getIntAttribute("visible", 1) == 1);
Element properties = element.getChildByName("properties");
if (properties != null) {
loadProperties(object.getProperties(), properties);
}
layer.getObjects().add(object);
}
}
Aggregations