Search in sources :

Example 11 with MapObject

use of com.badlogic.gdx.maps.MapObject in project Entitas-Java by Rubentxu.

the class Box2DMapObjectParser method createFixture.

/**
 * creates a {@link Fixture} from a {@link MapObject}
 *
 * @param mapObject the {@link MapObject} to parse
 * @return the parsed {@link Fixture}
 */
public Fixture createFixture(MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();
    String type = properties.get("type", String.class);
    Body body = bodies.get(type.equals(aliases.object) ? mapObject.getName() : properties.get(aliases.body, String.class));
    if (!type.equals(aliases.fixture) && !type.equals(aliases.object))
        throw new IllegalArgumentException("type of " + mapObject + " is  \"" + type + "\" instead of \"" + aliases.fixture + "\" or \"" + aliases.object + "\"");
    FixtureDef fixtureDef = new FixtureDef();
    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        shape = new PolygonShape();
        Rectangle rectangle = new Rectangle(((RectangleMapObject) mapObject).getRectangle());
        rectangle.x *= unitScale;
        rectangle.y *= unitScale;
        rectangle.width *= unitScale;
        rectangle.height *= unitScale;
        ((PolygonShape) shape).setAsBox(rectangle.width / 2, rectangle.height / 2, new Vector2(rectangle.x - body.getPosition().x + rectangle.width / 2, rectangle.y - body.getPosition().y + rectangle.height / 2), body.getAngle());
    } else if (mapObject instanceof PolygonMapObject) {
        shape = new PolygonShape();
        Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
        polygon.setPosition(polygon.getX() * unitScale - body.getPosition().x, polygon.getY() * unitScale - body.getPosition().y);
        polygon.setScale(unitScale, unitScale);
        ((PolygonShape) shape).set(polygon.getTransformedVertices());
    } else if (mapObject instanceof PolylineMapObject) {
        shape = new ChainShape();
        Polyline polyline = ((PolylineMapObject) mapObject).getPolyline();
        polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x, polyline.getY() * unitScale - body.getPosition().y);
        polyline.setScale(unitScale, unitScale);
        float[] vertices = polyline.getTransformedVertices();
        Vector2[] vectores = new Vector2[vertices.length / 2];
        for (int i = 0, j = 0; i < vertices.length; i += 2, j++) {
            vectores[j].x = vertices[i];
            vectores[j].y = vertices[i + 1];
        }
        ((ChainShape) shape).createChain(vectores);
    } else if (mapObject instanceof CircleMapObject) {
        shape = new CircleShape();
        Circle circle = ((CircleMapObject) mapObject).getCircle();
        circle.setPosition(circle.x * unitScale - body.getPosition().x, circle.y * unitScale - body.getPosition().y);
        circle.radius *= unitScale;
        ((CircleShape) shape).setPosition(new Vector2(circle.x, circle.y));
        ((CircleShape) shape).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.y, 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);
        }
        IllegalArgumentException exception = new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because  that are not circles are not supported");
        Gdx.app.error(getClass().getName(), exception.getMessage(), exception);
        throw exception;
    } else if (mapObject instanceof TextureMapObject) {
        IllegalArgumentException exception = new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because s are not supported");
        Gdx.app.error(getClass().getName(), exception.getMessage(), exception);
        throw exception;
    } else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();
    fixtureDef.shape = shape;
    fixtureDef.density = getProperty(properties, aliases.density, fixtureDef.density);
    fixtureDef.filter.categoryBits = getProperty(properties, aliases.categoryBits, GRUPO.STATIC.getCategory());
    fixtureDef.filter.groupIndex = getProperty(properties, aliases.groupIndex, fixtureDef.filter.groupIndex);
    fixtureDef.filter.maskBits = getProperty(properties, aliases.maskBits, Box2DPhysicsObject.MASK_STATIC);
    fixtureDef.friction = getProperty(properties, aliases.friciton, fixtureDef.friction);
    fixtureDef.isSensor = getProperty(properties, aliases.isSensor, fixtureDef.isSensor);
    fixtureDef.restitution = getProperty(properties, aliases.restitution, fixtureDef.restitution);
    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(body.getUserData());
    shape.dispose();
    String name = mapObject.getName();
    if (fixtures.containsKey(name)) {
        int duplicate = 1;
        while (fixtures.containsKey(name + duplicate)) duplicate++;
        name += duplicate;
    }
    fixtures.put(name, fixture);
    return fixture;
}
Also used : MapObject(com.badlogic.gdx.maps.MapObject) MapProperties(com.badlogic.gdx.maps.MapProperties)

Example 12 with MapObject

use of com.badlogic.gdx.maps.MapObject in project Entitas-Java by Rubentxu.

the class Box2DMapObjectParser method load.

/**
 * creates the given {@link MapLayer MapLayer's} {@link com.badlogic.gdx.maps.MapObjects} in the given {@link World}
 *
 * @param world the {@link World} to create the {@link com.badlogic.gdx.maps.MapObjects} of the given {@link MapLayer} in
 * @param layer the {@link MapLayer} which {@link com.badlogic.gdx.maps.MapObjects} to create in the given {@link World}
 * @return the given {@link World} with the parsed {@link com.badlogic.gdx.maps.MapObjects} of the given {@link MapLayer} created in it
 */
public World load(World world, MapLayer layer) {
    System.out.println("UNIT SCALE...........:" + unitScale);
    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = getProperty(layer.getProperties(), aliases.unitScale, unitScale);
        if (object.getProperties().get("type", "", String.class).equals(aliases.modelObject)) {
            createModelObject(world, object);
        }
    }
    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = getProperty(layer.getProperties(), aliases.unitScale, unitScale);
        if (object.getProperties().get("type", "", String.class).equals(aliases.object)) {
            createBody(world, object);
            createFixtures(object);
        }
    }
    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = getProperty(layer.getProperties(), aliases.unitScale, unitScale);
        if (object.getProperties().get("type", "", String.class).equals(aliases.body))
            createBody(world, object);
    }
    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = getProperty(layer.getProperties(), aliases.unitScale, unitScale);
        if (object.getProperties().get("type", "", String.class).equals(aliases.fixture))
            createFixtures(object);
    }
    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = getProperty(layer.getProperties(), aliases.unitScale, unitScale);
        if (object.getProperties().get("type", "", String.class).equals(aliases.joint))
            createJoint(object);
    }
    return world;
}
Also used : MapObject(com.badlogic.gdx.maps.MapObject)

Aggregations

MapObject (com.badlogic.gdx.maps.MapObject)12 MapLayer (com.badlogic.gdx.maps.MapLayer)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)3 MapObjects (com.badlogic.gdx.maps.MapObjects)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 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)2 TiledMapTileMapObject (com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject)2 Polygon (com.badlogic.gdx.math.Polygon)2 Creature (objects.Creature)2 Tile (objects.Tile)2 FileHandle (com.badlogic.gdx.files.FileHandle)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)1 Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)1 MapProperties (com.badlogic.gdx.maps.MapProperties)1 PolylineMapObject (com.badlogic.gdx.maps.objects.PolylineMapObject)1 TextureMapObject (com.badlogic.gdx.maps.objects.TextureMapObject)1