Example usage for com.badlogic.gdx.math Polyline Polyline

List of usage examples for com.badlogic.gdx.math Polyline Polyline

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Polyline Polyline.

Prototype

public Polyline(float[] vertices) 

Source Link

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;// ww w . ja  va 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);
    }
}

From source file:headmade.arttag.utils.MapUtils.java

License:Apache License

public static void loadMap(ArtTagScreen artTagScreen, String mapName) {
    if (artTagScreen.currentRoom != null) {
        unloadMap(artTagScreen);//w  ww  . ja v a 2 s. com
    }

    if (null != Player.instance.warpDirection) {
        Gdx.app.log(TAG, "Loading map " + mapName);
        if (DIRECTION_LEFT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == 0) {
                artTagScreen.currentRoomIndexX = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexX--;
            }
        } else if (DIRECTION_RIGHT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexX = 0;
            } else {
                artTagScreen.currentRoomIndexX++;
            }
        } else if (DIRECTION_TOP.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexY = 0;
            } else {
                artTagScreen.currentRoomIndexY++;
            }
        } else if (DIRECTION_BOTTOM.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == 0) {
                artTagScreen.currentRoomIndexY = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexY--;
            }
        }

    }

    boolean isNewRoom = true;
    if (artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] == null) {
        artTagScreen.currentRoom = new Room(mapName);
        artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] = artTagScreen.currentRoom;
    } else {
        artTagScreen.currentRoom = artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY];
        isNewRoom = false;
    }

    final Box2DMapObjectParser parser = new Box2DMapObjectParser(ArtTag.UNIT_SCALE);
    // final Box2DMapObjectParser.Listener.Adapter listener = new Box2DMapObjectParser.Listener.Adapter() {
    //
    // @Override
    // public void created(Fixture fixture, MapObject mapObject) {
    //
    // Gdx.app.log(TAG, "mapObject.getProperties()" + fixture.getFilterData().maskBits);
    // super.created(fixture, mapObject);
    // }
    //
    // };
    // parser.setListener(listener);

    final TiledMap map = artTagScreen.currentRoom.getMap();

    final String onGameOver = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    final String hideJobDesc = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    artTagScreen.onGameOver = onGameOver;
    artTagScreen.isHideJobDesc = hideJobDesc == null ? false : true;

    parser.load(artTagScreen.world, map);
    if (null == artTagScreen.mapRenderer) {
        // artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(artTagScreen.map, artTagScreen.getGame().getBatch());
        artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(map, parser.getUnitScale(),
                artTagScreen.getGame().getBatch());
    } else {
        artTagScreen.mapRenderer.setMap(map);
    }

    MapLayer layer = map.getLayers().get("objects");
    for (final MapObject mapObject : layer.getObjects()) {
        if (OBJ_ART.equals(mapObject.getName())) {
            if (isNewRoom) {
                // add Art only if this a new room. If this is an old room the art was created before.
                if (mapObject instanceof RectangleMapObject) {
                    createNewArt(artTagScreen, mapObject, ((RectangleMapObject) mapObject).getRectangle(),
                            parser.getUnitScale());
                } else {
                    Gdx.app.error(TAG, OBJ_ART + " has to be a Rectangle");
                }
            }
        } else if (OBJ_WARP.equals(mapObject.getName())) {
            final Body warp = createWarp(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String direction = mapObject.getProperties().get(PROP_DIRECTION, String.class);
            final String room = mapObject.getProperties().get(PROP_ROOM, String.class);
            warp.setUserData(new WarpVo(direction, room));
        } else if (OBJ_EXIT.equals(mapObject.getName())) {
            createExit(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(), parser.getUnitScale());
        } else if (OBJ_HINT.equals(mapObject.getName())) {
            final Body hintBody = createHint(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String hint = mapObject.getProperties().get(PROP_TEXT, String.class);
            hintBody.setUserData(hint);
        } else if (OBJ_PLAYER.equals(mapObject.getName())) {
            final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
            if (null == Player.instance.body) {
                final String direction = mapObject.getProperties().get("direction", String.class);
                if (null != Player.instance.warpDirection) {
                    if (Player.instance.warpDirection.equalsIgnoreCase(direction)) {
                        Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                                (e.y + e.height / 2f) * parser.getUnitScale());
                        Player.instance.warpDirection = null;
                    }
                } else if (direction == null) {
                    Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                            (e.y + e.height / 2f) * parser.getUnitScale());
                }
            }
        }
    }

    if (!isNewRoom) {
        // create ArtSensors for old Art
        for (final Art art : artTagScreen.currentRoom.getArtList()) {
            createArtSensor(artTagScreen, art);
        }
    }

    { // guards
        final HashMap<String, Guard> guards = new HashMap<String, Guard>();
        final Array<MapObject> paths = new Array<MapObject>();
        layer = map.getLayers().get("guards");
        for (final MapObject mapObject : layer.getObjects()) {
            if (mapObject.getName() != null && mapObject.getName().contains(OBJ_GUARD)) {
                final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
                final Guard g = new Guard();
                g.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                        (e.y + e.height / 2f) * parser.getUnitScale());
                guards.put(mapObject.getName().trim(), g);
            } else if (mapObject.getName() != null && mapObject.getName().contains(OBJ_PATH)) {
                paths.add(mapObject);
            } else {
                Gdx.app.log(TAG, "WTF");
            }
        }
        for (final MapObject mapObject : paths) {
            final PolylineMapObject pl = (PolylineMapObject) mapObject;
            final String ownerName = pl.getProperties().get("owner", String.class);
            final Guard g = guards.get(ownerName.trim());
            Gdx.app.log(TAG, "guards " + guards.keySet());
            if (g != null) {
                final Polyline orgPolyline = pl.getPolyline();
                final Polyline p = new Polyline(orgPolyline.getVertices());
                p.setScale(parser.getUnitScale(), parser.getUnitScale());
                p.setPosition(orgPolyline.getX() * parser.getUnitScale(),
                        orgPolyline.getY() * parser.getUnitScale());
                final float[] vertices = p.getTransformedVertices();
                for (int i = 0; i < vertices.length; i += 2) {
                    g.path.add(new Vector2(vertices[i], vertices[i + 1]));
                }
                artTagScreen.guards.add(g);
            } else {
                Gdx.app.log(TAG, "No guard for path " + ownerName);
            }
        }

    }

    layer = map.getLayers().get("lights");
    for (final MapObject mapObject : layer.getObjects()) {
        if (mapObject.getProperties().get("type", String.class).contains(LIGTH_POINT)) {
            if (mapObject instanceof EllipseMapObject) {
                createPointLight(artTagScreen, (EllipseMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_POINT + " light has to be a Circle not " + mapObject);
            }
        } else if (mapObject.getProperties().get("type", String.class).contains(LIGTH_CONE)) {
            if (mapObject instanceof PolygonMapObject) {
                createConeLight(artTagScreen, (PolygonMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_CONE + " light has to be a Polygon");
            }
        }
    }

}

From source file:im.ligas.worms.worm.impl.SplitterWormImpl.java

License:Open Source License

@Override
public Array<Shape2D> getObstacles() {
    final Array<Shape2D> obsticles = super.getObstacles();
    for (Branch branch : branches) {
        obsticles.add(new Polyline(Utils.convertToPrimitive(branch.bBody)));
    }//  w  ww. java  2  s . c o m
    return obsticles;
}

From source file:im.ligas.worms.worm.impl.WormImpl.java

License:Open Source License

@Override
public Array<Shape2D> getObstacles() {
    Array<Shape2D> shape2Ds = new Array<Shape2D>();
    shape2Ds.add(new Polyline(Utils.convertToPrimitive(body)));
    return shape2Ds;
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link Fixture} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return the parsed {@link Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    MapProperties properties = mapObject.getProperties();

    Shape shape = null;/*  ww  w. jav  a2 s .co  m*/
    if (mapObject instanceof RectangleMapObject) {
        shape = new PolygonShape();
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        float x = rectangle.x * unitScale, y = rectangle.y * unitScale, width = rectangle.width * unitScale,
                height = rectangle.height * unitScale;
        ((PolygonShape) shape).setAsBox(width / 2, height / 2,
                vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject) {
        shape = new PolygonShape();
        Polygon polygon = new Polygon(((PolygonMapObject) mapObject).getPolygon().getTransformedVertices());
        polygon.setScale(unitScale, unitScale);
        polygon.setPosition(-body.getPosition().x, -body.getPosition().y);
        ((PolygonShape) shape).set(polygon.getTransformedVertices());
    } else if (mapObject instanceof PolylineMapObject) {
        shape = new ChainShape();
        Polyline polyline = new Polyline(
                ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x,
                polyline.getY() * unitScale - body.getPosition().y);
        polyline.setScale(unitScale, unitScale);
        ((ChainShape) shape).createChain(polyline.getTransformedVertices());
    } else if (mapObject instanceof CircleMapObject) {
        shape = new CircleShape();
        Circle mapObjectCircle = ((CircleMapObject) mapObject).getCircle();
        Circle circle = new Circle(mapObjectCircle.x, mapObjectCircle.y, mapObjectCircle.radius);
        circle.setPosition(circle.x * unitScale - body.getPosition().x,
                circle.y * unitScale - body.getPosition().y);
        circle.radius *= unitScale;
        CircleShape circleShape = (CircleShape) shape;
        circleShape.setPosition(vec2.set(circle.x, circle.y));
        circleShape.setRadius(circle.radius);
    } else if (mapObject instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();

        if (ellipse.width == ellipse.height) {
            CircleMapObject circleMapObject = new CircleMapObject(ellipse.x + ellipse.width / 2,
                    ellipse.y + ellipse.height / 2, ellipse.width / 2);
            circleMapObject.setName(mapObject.getName());
            circleMapObject.getProperties().putAll(mapObject.getProperties());
            circleMapObject.setColor(mapObject.getColor());
            circleMapObject.setVisible(mapObject.isVisible());
            circleMapObject.setOpacity(mapObject.getOpacity());
            return createFixture(circleMapObject, body);
        }

        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + mapObject.getClass().getSimpleName() + "s that are not circles are not supported");
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + mapObject.getClass().getSimpleName() + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    assignProperties(fixtureDef, heritage);
    assignProperties(fixtureDef, mapProperties);
    assignProperties(fixtureDef, layerProperties);
    assignProperties(fixtureDef, properties);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(getProperty(layerProperties, aliases.userData, fixture.getUserData()));
    fixture.setUserData(getProperty(properties, aliases.userData, fixture.getUserData()));

    shape.dispose();

    fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture);

    return fixture;
}

From source file:non.plugins.math.java

public Polyline polyline(float[] vertices) {
    return new Polyline(vertices);
}

From source file:releasethekraken.path.SeaCreaturePath.java

/**
 * Constructs a new SeaCreaturePath//from   ww w  .  j a  v a 2 s  .  c o m
 * @param id The path's ID, from the level editor
 * @param parent The path that leads to this path, or null if this is the first path
 * @param nextPaths A list of paths that can be gone on after this path.  The list is empty if there are none.
 * @param polyline The Polyline object that holds the path data
 */
public SeaCreaturePath(int id, SeaCreaturePath parent, Array<SeaCreaturePath> nextPaths, Polyline polyline) {
    this.id = id;
    this.parent = parent;
    this.nextPaths = nextPaths;

    final float scale = 1 / 16F; //Scale it down to match the world units

    //Scale the polyline to match the world units.
    //This has to be done manually because Polyline.setScale() doesn't seem to work.
    float[] vertices = polyline.getTransformedVertices();
    for (int i = 0; i < vertices.length; i++)
        vertices[i] *= scale;
    polyline = new Polyline(vertices);

    this.polyline = polyline;

    vertices = polyline.getVertices(); //Get an array of vertices, in x1, y1, x2, y2,... format
    Vector2[] points = new Vector2[vertices.length / 2]; //Make an array of Vector2 objects

    //Convert the vertices to Vector2 objects
    for (int i = 0; i < points.length; i++) {
        float x = vertices[i * 2];
        float y = vertices[(i * 2) + 1];
        points[i] = new Vector2(x, y);
    }

    //TODO: Fix this!
    //Add additional starting and ending points so that all of the original points are used, due to how catmull rom spline works
    Array<Vector2> pointsList = new Array<Vector2>(points);
    Vector2 firstPoint = points[0].cpy().add(points[0].cpy().sub(points[1]));
    pointsList.insert(0, firstPoint);
    Vector2 lastPoint = points[points.length - 1].cpy()
            .add(points[points.length - 1].cpy().sub(points[points.length - 2]));
    pointsList.insert(pointsList.size, lastPoint);

    //Make the smooth path with the points
    this.path = new CatmullRomSpline<Vector2>(pointsList.toArray(), false);

    Gdx.app.log("SCPath " + this.toString(true), "Points: " + Arrays.toString(points));
}