Example usage for com.badlogic.gdx.maps MapObject getProperties

List of usage examples for com.badlogic.gdx.maps MapObject getProperties

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps MapObject getProperties.

Prototype

public MapProperties getProperties() 

Source Link

Usage

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

public static Body createStaticBodyForMapObject(World world, MapObject object) {
    final float u = Constants.UNIT_FOR_PIXEL;
    float rotation = object.getProperties().get("rotation", 0f, Float.class);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.angle = -rotation * MathUtils.degreesToRadians;

    if (object instanceof RectangleMapObject) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        /*//from   ww w . j a v  a2s  .  c  o  m
          A          D
           x--------x
           |        |
           x--------x
          B          C
         */
        float[] vertices = new float[8];
        // A
        vertices[0] = 0;
        vertices[1] = 0;
        // B
        vertices[2] = 0;
        vertices[3] = -rect.getHeight();
        // C
        vertices[4] = rect.getWidth();
        vertices[5] = -rect.getHeight();
        // D
        vertices[6] = rect.getWidth();
        vertices[7] = 0;
        scaleVertices(vertices, u);

        bodyDef.position.set(u * rect.getX(), u * (rect.getY() + rect.getHeight()));
        Body body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        body.createFixture(shape, 1);
        return body;
    } else if (object instanceof PolygonMapObject) {
        Polygon polygon = ((PolygonMapObject) object).getPolygon();
        float[] vertices = polygon.getVertices().clone();
        scaleVertices(vertices, u);

        bodyDef.position.set(polygon.getX() * u, polygon.getY() * u);
        Body body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        body.createFixture(shape, 1);
        return body;
    } else if (object instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) object).getEllipse();
        float radius = ellipse.width * u / 2;
        float x = ellipse.x * u + radius;
        float y = ellipse.y * u + radius;

        bodyDef.position.set(x, y);
        Body body = world.createBody(bodyDef);

        CircleShape shape = new CircleShape();
        shape.setRadius(radius);

        body.createFixture(shape, 1);
        return body;
    }
    throw new RuntimeException("Unsupported MapObject type: " + object);
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates the given {@link MapLayer MapLayer's} {@link MapObjects} in the given {@link World}  
 * @param world the {@link World} to create the {@link MapObjects} of the given {@link MapLayer} in
 * @param layer the {@link MapLayer} which {@link MapObjects} to create in the given {@link World}
 * @return the given {@link World} with the parsed {@link MapObjects} of the given {@link MapLayer} created in it
 *//* w ww  .  j  a  va2s .com*/
public World load(World world, MapLayer layer) {
    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = (Float) getProperty(layer.getProperties(), aliases.unitScale, unitScale, Float.class);
        if (object.getProperties().get("type", String.class).equals(aliases.object)) {
            createBody(world, object);
            createFixture(object);
        }
    }

    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = (Float) getProperty(layer.getProperties(), aliases.unitScale, unitScale, Float.class);
        if (object.getProperties().get("type", String.class).equals(aliases.body))
            createBody(world, object);
    }

    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = (Float) getProperty(layer.getProperties(), aliases.unitScale, unitScale, Float.class);
        if (object.getProperties().get("type", String.class).equals(aliases.fixture))
            createFixture(object);
    }

    for (MapObject object : layer.getObjects()) {
        if (!ignoreMapUnitScale)
            unitScale = (Float) getProperty(layer.getProperties(), aliases.unitScale, unitScale, Float.class);
        if (object.getProperties().get("type", String.class).equals(aliases.joint))
            createJoint(object);
    }

    return world;
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Body} in the given {@link World} from the given {@link MapObject}
 * @param world the {@link World} to create the {@link Body} in
 * @param mapObject the {@link MapObject} to parse the {@link Body} from
 * @return the {@link Body} created in the given {@link World} from the given {@link MapObject}
 *///  w w w  . jav a2s  .c om
public Body createBody(World world, MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    String type = properties.get("type", String.class);
    if (!type.equals(aliases.body) && !type.equals(aliases.object))
        throw new IllegalArgumentException("type of " + mapObject + " is  \"" + type + "\" instead of \""
                + aliases.body + "\" or \"" + aliases.object + "\"");

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = properties.get(aliases.bodyType, String.class) != null
            ? properties.get(aliases.bodyType, String.class).equals(aliases.dynamicBody) ? BodyType.DynamicBody
                    : properties.get(aliases.bodyType, String.class).equals(aliases.kinematicBody)
                            ? BodyType.KinematicBody
                            : properties.get(aliases.bodyType, String.class).equals(aliases.staticBody)
                                    ? BodyType.StaticBody
                                    : bodyDef.type
            : bodyDef.type;
    bodyDef.active = (Boolean) getProperty(properties, aliases.active, bodyDef.active, Boolean.class);
    bodyDef.allowSleep = (Boolean) getProperty(properties, aliases.allowSleep, bodyDef.allowSleep,
            Boolean.class);
    bodyDef.angle = (Float) getProperty(properties, aliases.angle, bodyDef.angle, Float.class);
    bodyDef.angularDamping = (Float) getProperty(properties, aliases.angularDamping, bodyDef.angularDamping,
            Float.class);
    bodyDef.angularVelocity = (Float) getProperty(properties, aliases.angularVelocity, bodyDef.angularVelocity,
            Float.class);
    bodyDef.awake = (Boolean) getProperty(properties, aliases.awake, bodyDef.awake, Boolean.class);
    bodyDef.bullet = (Boolean) getProperty(properties, aliases.bullet, bodyDef.bullet, Boolean.class);
    bodyDef.fixedRotation = (Boolean) getProperty(properties, aliases.fixedRotation, bodyDef.fixedRotation,
            Boolean.class);
    bodyDef.gravityScale = (Float) getProperty(properties, aliases.gravityunitScale, bodyDef.gravityScale,
            Float.class);
    bodyDef.linearDamping = (Float) getProperty(properties, aliases.linearDamping, bodyDef.linearDamping,
            Float.class);
    bodyDef.linearVelocity.set(
            (Float) getProperty(properties, aliases.linearVelocityX, bodyDef.linearVelocity.x, Float.class),
            (Float) getProperty(properties, aliases.linearVelocityY, bodyDef.linearVelocity.y, Float.class));
    bodyDef.position.set((Integer) getProperty(properties, "x", bodyDef.position.x, Integer.class) * unitScale,
            (Integer) getProperty(properties, "y", bodyDef.position.y, Integer.class) * unitScale);

    Body body = world.createBody(bodyDef);

    String name = mapObject.getName();
    if (bodies.containsKey(name)) {
        int duplicate = 1;
        while (bodies.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }

    bodies.put(name, body);

    return body;
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Fixture} from a {@link MapObject}
 * @param mapObject the {@link MapObject} which to parse
 * @return the parsed {@link Fixture}//w w w . jav a2s  . c  om
 */
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 = ((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);
        ((ChainShape) shape).createChain(polyline.getTransformedVertices());
    } 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 " + mapObject.getClass().getSimpleName()
                        + "s that are not circles are not supported");
        Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception);
        throw exception;
    } else if (mapObject instanceof TextureMapObject) {
        IllegalArgumentException exception = new IllegalArgumentException("Cannot parse " + mapObject.getName()
                + " because " + mapObject.getClass().getSimpleName() + "s are not supported");
        Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception);
        throw exception;
    } else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    fixtureDef.shape = shape;
    fixtureDef.density = (Float) getProperty(properties, aliases.density, fixtureDef.density, Float.class);
    fixtureDef.filter.categoryBits = (Short) getProperty(properties, aliases.categoryBits,
            fixtureDef.filter.categoryBits, Short.class);
    fixtureDef.filter.groupIndex = (Short) getProperty(properties, aliases.groupIndex,
            fixtureDef.filter.groupIndex, Short.class);
    fixtureDef.filter.maskBits = (Short) getProperty(properties, aliases.maskBits, fixtureDef.filter.maskBits,
            Short.class);
    fixtureDef.friction = (Float) getProperty(properties, aliases.friciton, fixtureDef.friction, Float.class);
    fixtureDef.isSensor = (Boolean) getProperty(properties, aliases.isSensor, fixtureDef.isSensor,
            Boolean.class);
    fixtureDef.restitution = (Float) getProperty(properties, aliases.restitution, fixtureDef.restitution,
            Float.class);

    Fixture fixture = body.createFixture(fixtureDef);

    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;
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Joint} from a {@link MapObject}
 * @param mapObject the {@link Joint} which to parse
 * @return the parsed {@link Joint}/*from w  w  w . ja  v  a  2  s.co  m*/
 */
public Joint createJoint(MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    JointDef jointDef = null;

    String type = properties.get("type", String.class);
    if (!type.equals(aliases.joint))
        throw new IllegalArgumentException(
                "type of " + mapObject + " is  \"" + type + "\" instead of \"" + aliases.joint + "\"");

    String jointType = properties.get(aliases.jointType, String.class);

    // get all possible values
    if (jointType.equals(aliases.distanceJoint)) {
        DistanceJointDef distanceJointDef = new DistanceJointDef();
        distanceJointDef.dampingRatio = (Float) getProperty(properties, aliases.dampingRatio,
                distanceJointDef.dampingRatio, Float.class);
        distanceJointDef.frequencyHz = (Float) getProperty(properties, aliases.frequencyHz,
                distanceJointDef.frequencyHz, Float.class);
        distanceJointDef.length = (Float) getProperty(properties, aliases.length, distanceJointDef.length,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        distanceJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, distanceJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, distanceJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        distanceJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, distanceJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, distanceJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);

        jointDef = distanceJointDef;
    } else if (jointType.equals(aliases.frictionJoint)) {
        FrictionJointDef frictionJointDef = new FrictionJointDef();
        frictionJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, frictionJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, frictionJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        frictionJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, frictionJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, frictionJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        frictionJointDef.maxForce = (Float) getProperty(properties, aliases.maxForce, frictionJointDef.maxForce,
                Float.class);
        frictionJointDef.maxTorque = (Float) getProperty(properties, aliases.maxTorque,
                frictionJointDef.maxTorque, Float.class);

        jointDef = frictionJointDef;
    } else if (jointType.equals(aliases.gearJoint)) {
        GearJointDef gearJointDef = new GearJointDef();
        gearJointDef.joint1 = joints.get(properties.get(aliases.joint1, String.class));
        gearJointDef.joint2 = joints.get(properties.get(aliases.joint2, String.class));
        gearJointDef.ratio = (Float) getProperty(properties, aliases.ratio, gearJointDef.ratio, Float.class);

        jointDef = gearJointDef;
    } else if (jointType.equals(aliases.mouseJoint)) {
        MouseJointDef mouseJointDef = new MouseJointDef();
        mouseJointDef.dampingRatio = (Float) getProperty(properties, aliases.dampingRatio,
                mouseJointDef.dampingRatio, Float.class);
        mouseJointDef.frequencyHz = (Float) getProperty(properties, aliases.frequencyHz,
                mouseJointDef.frequencyHz, Float.class);
        mouseJointDef.maxForce = (Float) getProperty(properties, aliases.maxForce, mouseJointDef.maxForce,
                Float.class);
        mouseJointDef.target.set(
                (Float) getProperty(properties, aliases.targetX, mouseJointDef.target.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.targetY, mouseJointDef.target.y, Float.class)
                        * tileHeight * unitScale);

        jointDef = mouseJointDef;
    } else if (jointType.equals(aliases.prismaticJoint)) {
        PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
        prismaticJointDef.enableLimit = (Boolean) getProperty(properties, aliases.enableLimit,
                prismaticJointDef.enableLimit, Boolean.class);
        prismaticJointDef.enableMotor = (Boolean) getProperty(properties, aliases.enableMotor,
                prismaticJointDef.enableMotor, Boolean.class);
        prismaticJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, prismaticJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, prismaticJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        prismaticJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, prismaticJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, prismaticJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        prismaticJointDef.localAxisA.set(
                (Float) getProperty(properties, aliases.localAxisAX, prismaticJointDef.localAxisA.x,
                        Float.class),
                (Float) getProperty(properties, aliases.localAxisAY, prismaticJointDef.localAxisA.y,
                        Float.class));
        prismaticJointDef.lowerTranslation = (Float) getProperty(properties, aliases.lowerTranslation,
                prismaticJointDef.lowerTranslation, Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        prismaticJointDef.maxMotorForce = (Float) getProperty(properties, aliases.maxMotorForce,
                prismaticJointDef.maxMotorForce, Float.class);
        prismaticJointDef.motorSpeed = (Float) getProperty(properties, aliases.motorSpeed,
                prismaticJointDef.motorSpeed, Float.class);
        prismaticJointDef.referenceAngle = (Float) getProperty(properties, aliases.referenceAngle,
                prismaticJointDef.referenceAngle, Float.class);
        prismaticJointDef.upperTranslation = (Float) getProperty(properties, aliases.upperTranslation,
                prismaticJointDef.upperTranslation, Float.class) * (tileWidth + tileHeight) / 2 * unitScale;

        jointDef = prismaticJointDef;
    } else if (jointType.equals(aliases.pulleyJoint)) {
        PulleyJointDef pulleyJointDef = new PulleyJointDef();
        pulleyJointDef.groundAnchorA.set(
                (Float) getProperty(properties, aliases.groundAnchorAX, pulleyJointDef.groundAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.groundAnchorAY, pulleyJointDef.groundAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.groundAnchorB.set(
                (Float) getProperty(properties, aliases.groundAnchorBX, pulleyJointDef.groundAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.groundAnchorBY, pulleyJointDef.groundAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.lengthA = (Float) getProperty(properties, aliases.lengthA, pulleyJointDef.lengthA,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        pulleyJointDef.lengthB = (Float) getProperty(properties, aliases.lengthB, pulleyJointDef.lengthB,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;
        pulleyJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, pulleyJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, pulleyJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, pulleyJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, pulleyJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        pulleyJointDef.ratio = (Float) getProperty(properties, aliases.ratio, pulleyJointDef.ratio,
                Float.class);

        jointDef = pulleyJointDef;
    } else if (jointType.equals(aliases.revoluteJoint)) {
        RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
        revoluteJointDef.enableLimit = (Boolean) getProperty(properties, aliases.enableLimit,
                revoluteJointDef.enableLimit, Boolean.class);
        revoluteJointDef.enableMotor = (Boolean) getProperty(properties, aliases.enableMotor,
                revoluteJointDef.enableMotor, Boolean.class);
        revoluteJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, revoluteJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, revoluteJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        revoluteJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, revoluteJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, revoluteJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        revoluteJointDef.lowerAngle = (Float) getProperty(properties, aliases.lowerAngle,
                revoluteJointDef.lowerAngle, Float.class);
        revoluteJointDef.maxMotorTorque = (Float) getProperty(properties, aliases.maxMotorTorque,
                revoluteJointDef.maxMotorTorque, Float.class);
        revoluteJointDef.motorSpeed = (Float) getProperty(properties, aliases.motorSpeed,
                revoluteJointDef.motorSpeed, Float.class);
        revoluteJointDef.referenceAngle = (Float) getProperty(properties, aliases.referenceAngle,
                revoluteJointDef.referenceAngle, Float.class);
        revoluteJointDef.upperAngle = (Float) getProperty(properties, aliases.upperAngle,
                revoluteJointDef.upperAngle, Float.class);

        jointDef = revoluteJointDef;
    } else if (jointType.equals(aliases.ropeJoint)) {
        RopeJointDef ropeJointDef = new RopeJointDef();
        ropeJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, ropeJointDef.localAnchorA.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, ropeJointDef.localAnchorA.y, Float.class)
                        * tileHeight * unitScale);
        ropeJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, ropeJointDef.localAnchorB.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, ropeJointDef.localAnchorB.y, Float.class)
                        * tileHeight * unitScale);
        ropeJointDef.maxLength = (Float) getProperty(properties, aliases.maxLength, ropeJointDef.maxLength,
                Float.class) * (tileWidth + tileHeight) / 2 * unitScale;

        jointDef = ropeJointDef;
    } else if (jointType.equals(aliases.weldJoint)) {
        WeldJointDef weldJointDef = new WeldJointDef();
        weldJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, weldJointDef.localAnchorA.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, weldJointDef.localAnchorA.y, Float.class)
                        * tileHeight * unitScale);
        weldJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, weldJointDef.localAnchorB.x, Float.class)
                        * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, weldJointDef.localAnchorB.y, Float.class)
                        * tileHeight * unitScale);
        weldJointDef.referenceAngle = (Float) getProperty(properties, aliases.referenceAngle,
                weldJointDef.referenceAngle, Float.class);

        jointDef = weldJointDef;
    } else if (jointType.equals(aliases.wheelJoint)) {
        WheelJointDef wheelJointDef = new WheelJointDef();
        wheelJointDef.dampingRatio = (Float) getProperty(properties, aliases.dampingRatio,
                wheelJointDef.dampingRatio, Float.class);
        wheelJointDef.enableMotor = (Boolean) getProperty(properties, aliases.enableMotor,
                wheelJointDef.enableMotor, Boolean.class);
        wheelJointDef.frequencyHz = (Float) getProperty(properties, aliases.frequencyHz,
                wheelJointDef.frequencyHz, Float.class);
        wheelJointDef.localAnchorA.set(
                (Float) getProperty(properties, aliases.localAnchorAX, wheelJointDef.localAnchorA.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorAY, wheelJointDef.localAnchorA.y,
                        Float.class) * tileHeight * unitScale);
        wheelJointDef.localAnchorB.set(
                (Float) getProperty(properties, aliases.localAnchorBX, wheelJointDef.localAnchorB.x,
                        Float.class) * tileWidth * unitScale,
                (Float) getProperty(properties, aliases.localAnchorBY, wheelJointDef.localAnchorB.y,
                        Float.class) * tileHeight * unitScale);
        wheelJointDef.localAxisA.set(
                (Float) getProperty(properties, aliases.localAxisAX, wheelJointDef.localAxisA.x, Float.class),
                (Float) getProperty(properties, aliases.localAxisAY, wheelJointDef.localAxisA.y, Float.class));
        wheelJointDef.maxMotorTorque = (Float) getProperty(properties, aliases.maxMotorTorque,
                wheelJointDef.maxMotorTorque, Float.class);
        wheelJointDef.motorSpeed = (Float) getProperty(properties, aliases.motorSpeed, wheelJointDef.motorSpeed,
                Float.class);

        jointDef = wheelJointDef;
    }

    jointDef.bodyA = bodies.get(properties.get(aliases.bodyA, String.class));
    jointDef.bodyB = bodies.get(properties.get(aliases.bodyB, String.class));
    jointDef.collideConnected = (Boolean) getProperty(properties, aliases.collideConnected,
            jointDef.collideConnected, Boolean.class);

    Joint joint = jointDef.bodyA.getWorld().createJoint(jointDef);

    String name = mapObject.getName();
    if (joints.containsKey(name)) {
        int duplicate = 1;
        while (joints.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }

    joints.put(name, joint);

    return joint;
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * @param layer the {@link MapLayer} which hierarchy to print
 * @return a human readable {@link String} containing the hierarchy of the {@link MapObjects} of the given {@link MapLayer}
 *///from w ww  . java  2 s.c  om
public String getHierarchy(MapLayer layer) {
    String hierarchy = "";

    for (MapObject object : layer.getObjects()) {
        hierarchy += object.getName() + " (" + object.getClass().getSimpleName() + "):\n";
        Iterator<String> keys = object.getProperties().getKeys();
        while (keys.hasNext()) {
            String key = keys.next();
            hierarchy += "\t" + key + ": " + object.getProperties().get(key) + "\n";
        }
    }

    return hierarchy;
}

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

/**
 * creates the given {@link com.badlogic.gdx.maps.MapLayer MapLayer's} {@link com.badlogic.gdx.maps.MapObjects} in the given {@link com.badlogic.gdx.physics.box2d.World}
 *
 * @param world the {@link com.badlogic.gdx.physics.box2d.World} to create the {@link com.badlogic.gdx.maps.MapObjects} of the given {@link com.badlogic.gdx.maps.MapLayer} in
 * @param layer the {@link com.badlogic.gdx.maps.MapLayer} which {@link com.badlogic.gdx.maps.MapObjects} to create in the given {@link com.badlogic.gdx.physics.box2d.World}
 * @return the given {@link com.badlogic.gdx.physics.box2d.World} with the parsed {@link com.badlogic.gdx.maps.MapObjects} of the given {@link com.badlogic.gdx.maps.MapLayer} created in it
 *//*  ww  w  .  j av  a  2  s  . co  m*/
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;
}

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

private void createModelObject(World world, MapObject object) {
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.hero))
        box2dObjectFactory.createEntity(GRUPO.HERO, object);
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.movingPlatform))
        box2dObjectFactory.createEntity(GRUPO.MOVING_PLATFORM, object);
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.water))
        box2dObjectFactory.createEntity(GRUPO.FLUID, object);
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.enemy))
        box2dObjectFactory.createEntity(GRUPO.ENEMY, object);
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.item))
        box2dObjectFactory.createEntity(GRUPO.ITEMS, object);
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.millObject))
        box2dObjectFactory.createEntity(GRUPO.MILL, object);
    if (object.getProperties().get(aliases.typeModelObject).equals(aliases.checkPointObject))
        box2dObjectFactory.createEntity(GRUPO.CHECKPOINT, object);

}

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link com.badlogic.gdx.physics.box2d.Body} in the given {@link com.badlogic.gdx.physics.box2d.World} from the given {@link com.badlogic.gdx.maps.MapObject}
 *
 * @param world     the {@link com.badlogic.gdx.physics.box2d.World} to create the {@link com.badlogic.gdx.physics.box2d.Body} in
 * @param mapObject the {@link com.badlogic.gdx.maps.MapObject} to parse the {@link com.badlogic.gdx.physics.box2d.Body} from
 * @return the {@link com.badlogic.gdx.physics.box2d.Body} created in the given {@link com.badlogic.gdx.physics.box2d.World} from the given {@link com.badlogic.gdx.maps.MapObject}
 *//*from  ww  w .  j  ava 2s . c  om*/
public Body createBody(World world, MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    String type = properties.get("type", String.class);
    if (!type.equals(aliases.body) && !type.equals(aliases.object))
        throw new IllegalArgumentException("type of " + mapObject + " is  \"" + type + "\" instead of \""
                + aliases.body + "\" or \"" + aliases.object + "\"");

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = properties.get(aliases.bodyType, String.class) != null
            ? properties.get(aliases.bodyType, String.class).equals(aliases.dynamicBody) ? BodyType.DynamicBody
                    : properties.get(aliases.bodyType, String.class).equals(aliases.kinematicBody)
                            ? BodyType.KinematicBody
                            : properties.get(aliases.bodyType, String.class).equals(aliases.staticBody)
                                    ? BodyType.StaticBody
                                    : bodyDef.type
            : bodyDef.type;
    bodyDef.active = getProperty(properties, aliases.active, bodyDef.active);
    bodyDef.allowSleep = getProperty(properties, aliases.allowSleep, bodyDef.allowSleep);
    bodyDef.angle = getProperty(properties, aliases.angle, bodyDef.angle);
    bodyDef.angularDamping = getProperty(properties, aliases.angularDamping, bodyDef.angularDamping);
    bodyDef.angularVelocity = getProperty(properties, aliases.angularVelocity, bodyDef.angularVelocity);
    bodyDef.awake = getProperty(properties, aliases.awake, bodyDef.awake);
    bodyDef.bullet = getProperty(properties, aliases.bullet, bodyDef.bullet);
    bodyDef.fixedRotation = getProperty(properties, aliases.fixedRotation, bodyDef.fixedRotation);
    bodyDef.gravityScale = getProperty(properties, aliases.gravityunitScale, bodyDef.gravityScale);
    bodyDef.linearDamping = getProperty(properties, aliases.linearDamping, bodyDef.linearDamping);
    bodyDef.linearVelocity.set(getProperty(properties, aliases.linearVelocityX, bodyDef.linearVelocity.x),
            getProperty(properties, aliases.linearVelocityY, bodyDef.linearVelocity.y));
    bodyDef.position.set(getProperty(properties, "x", bodyDef.position.x) * unitScale,
            getProperty(properties, "y", bodyDef.position.y) * unitScale);

    Body body = world.createBody(bodyDef);

    String name = mapObject.getName();
    if (bodies.containsKey(name)) {
        int duplicate = 1;
        while (bodies.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }
    Box2DPhysicsObject box2DPhysicsObject = new Box2DPhysicsObject(name, GRUPO.STATIC, body);
    body.setUserData(box2DPhysicsObject);
    bodies.put(name, body);

    return body;
}

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link com.badlogic.gdx.physics.box2d.Fixture} from a {@link com.badlogic.gdx.maps.MapObject}
 *
 * @param mapObject the {@link com.badlogic.gdx.maps.MapObject} to parse
 * @return the parsed {@link com.badlogic.gdx.physics.box2d.Fixture}
 *//*w ww .j  a v  a 2s. co m*/
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();

        /*
        b2ChainShape* chain = (b2ChainShape*)addr;
        b2Vec2* verticesOut = new b2Vec2[numVertices];
        for( int i = 0; i < numVertices; i++ )
        verticesOut[i] = b2Vec2(verts[i<<1], verts[(i<<1)+1]);
        chain->CreateChain( verticesOut, numVertices );
        delete verticesOut;
        */

        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;
}