Search in sources :

Example 1 with TiledMapTileMapObject

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();
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) Ellipse(com.badlogic.gdx.math.Ellipse) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) MapLayer(com.badlogic.gdx.maps.MapLayer) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) Rectangle(com.badlogic.gdx.math.Rectangle) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) TextureMapObject(com.badlogic.gdx.maps.objects.TextureMapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject)

Example 2 with TiledMapTileMapObject

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);
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) Polyline(com.badlogic.gdx.math.Polyline) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject)

Aggregations

TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 MapObject (com.badlogic.gdx.maps.MapObject)2 EllipseMapObject (com.badlogic.gdx.maps.objects.EllipseMapObject)2 PolygonMapObject (com.badlogic.gdx.maps.objects.PolygonMapObject)2 RectangleMapObject (com.badlogic.gdx.maps.objects.RectangleMapObject)2 TiledMapTileMapObject (com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject)2 Polygon (com.badlogic.gdx.math.Polygon)2 MapLayer (com.badlogic.gdx.maps.MapLayer)1 PolylineMapObject (com.badlogic.gdx.maps.objects.PolylineMapObject)1 TextureMapObject (com.badlogic.gdx.maps.objects.TextureMapObject)1 Ellipse (com.badlogic.gdx.math.Ellipse)1 Polyline (com.badlogic.gdx.math.Polyline)1 Rectangle (com.badlogic.gdx.math.Rectangle)1 Element (com.badlogic.gdx.utils.XmlReader.Element)1