Example usage for com.badlogic.gdx.maps.objects RectangleMapObject RectangleMapObject

List of usage examples for com.badlogic.gdx.maps.objects RectangleMapObject RectangleMapObject

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps.objects RectangleMapObject RectangleMapObject.

Prototype

public RectangleMapObject(float x, float y, float width, float height) 

Source Link

Document

Creates a Rectangle object with the given X and Y coordinates along with a given width and height.

Usage

From source file:de.bitowl.advent.game2.MyAtlasTmxMapLoader.java

License:Apache License

protected void loadObject(MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
        MapObject object = null;//from ww  w . jav  a 2 s.c o m

        float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
        float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;

        float x = element.getIntAttribute("x", 0) * scaleX;
        float y = (yUp ? mapHeightInPixels - element.getIntAttribute("y", 0) : element.getIntAttribute("y", 0))
                * scaleY;

        float width = element.getIntAttribute("width", 0) * scaleX;
        float height = element.getIntAttribute("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] = Integer.parseInt(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Integer.parseInt(point[1]) * scaleY;
                    if (yUp) {
                        vertices[i * 2 + 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] = Integer.parseInt(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Integer.parseInt(point[1]) * scaleY;
                    if (yUp) {
                        vertices[i * 2 + 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, yUp ? y - height : y, width, height);
            }
        }
        if (object == null) {
            object = new RectangleMapObject(x, yUp ? y - height : y, width, height);
        }
        object.setName(element.getAttribute("name", null));
        String type = element.getAttribute("type", null);
        if (type != null) {
            object.getProperties().put("type", type);
        }
        int gid = element.getIntAttribute("gid", -1);
        if (gid != -1) {
            object.getProperties().put("gid", gid);
        }
        object.getProperties().put("x", x * scaleX);
        object.getProperties().put("y", (yUp ? y - height : y) * scaleY);
        object.setVisible(element.getIntAttribute("visible", 1) == 1);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(object.getProperties(), properties);
        }
        layer.getObjects().add(object);
    }
}