Example usage for com.badlogic.gdx.maps MapProperties get

List of usage examples for com.badlogic.gdx.maps MapProperties get

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps MapProperties get.

Prototype

public <T> T get(String key, Class<T> clazz) 

Source Link

Document

Returns the object for the given key, casting it to clazz.

Usage

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  .j ava  2s .  c o m*/
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}/*from  w w  w  .  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 = ((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}//  w  w  w  .ja v a2  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

/**
 * internal method for easier access of {@link MapProperty MapProperties}
 * @param properties the {@link MapProperties} from which to get a property
 * @param property the key of the desired property
 * @param defaultValue the default return value in case the value of the given key cannot be returned
 * @param clazz The {@link Class} of the desired property. You still need to cast because it was not possible to make this a generic method.
 * @return the property value associated with the given property key
 *//*w  w w  .  ja  v  a2s .  c om*/
private Object getProperty(MapProperties properties, String property, Object defaultValue, Class<?> clazz) {
    if (clazz == Float.class)
        return properties.get(property, String.class) != null
                ? Float.parseFloat(properties.get(property, String.class))
                : defaultValue;
    else if (clazz == Integer.class)
        return properties.get(property, String.class) != null ? properties.get(property, Integer.class)
                : defaultValue;
    else if (clazz == Short.class)
        return properties.get(property, String.class) != null ? properties.get(property, Short.class)
                : defaultValue;
    else if (clazz == Boolean.class)
        return properties.get(property, String.class) != null
                ? Boolean.parseBoolean(properties.get(property, String.class))
                : defaultValue;
    return defaultValue;
}

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}
 *///www  . ja  v a  2  s  .  co m
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  . ja  v  a  2  s .  c  o  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;
}

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

License:Apache License

/**
 * creates a {@link com.badlogic.gdx.physics.box2d.Joint} from a {@link com.badlogic.gdx.maps.MapObject}
 *
 * @param mapObject the {@link com.badlogic.gdx.physics.box2d.Joint} to parse
 * @return the parsed {@link com.badlogic.gdx.physics.box2d.Joint}
 *//*w  ww  .ja  va2 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 = getProperty(properties, aliases.dampingRatio,
                distanceJointDef.dampingRatio);
        distanceJointDef.frequencyHz = getProperty(properties, aliases.frequencyHz,
                distanceJointDef.frequencyHz);
        distanceJointDef.length = getProperty(properties, aliases.length, distanceJointDef.length)
                * (tileWidth + tileHeight) / 2 * unitScale;
        distanceJointDef.localAnchorA.set(
                getProperty(properties, aliases.localAnchorAX, distanceJointDef.localAnchorA.x) * tileWidth
                        * unitScale,
                getProperty(properties, aliases.localAnchorAY, distanceJointDef.localAnchorA.y) * tileHeight
                        * unitScale);
        distanceJointDef.localAnchorB.set(
                getProperty(properties, aliases.localAnchorBX, distanceJointDef.localAnchorB.x) * tileWidth
                        * unitScale,
                getProperty(properties, aliases.localAnchorBY, distanceJointDef.localAnchorB.y) * tileHeight
                        * unitScale);

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

        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 = getProperty(properties, aliases.ratio, gearJointDef.ratio);

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

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

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

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

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

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

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

        jointDef = wheelJointDef;
    }

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

    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.nuliy.example.GameScreen.java

public GameScreen(Game game) {
    this.game = game;
    map = new TmxMapLoader().load("GTA MAP.tmx");
    mapRender = new OrthogonalTiledMapRenderer(map);
    miniMap = new TmxMapLoader().load("GTA MAP.tmx");
    miniMapRender = new OrthogonalTiledMapRenderer(map);
    playermarker = new Texture(Gdx.files.internal("playermarkercircle.png"));
    healthbar = new Texture(Gdx.files.internal("healthbar.png"));
    HUDfist = new Texture(Gdx.files.internal("fist.png"));
    HUDm4 = new Texture(Gdx.files.internal("m4.png"));
    playerScore = new Scoreboard();
    MapProperties prop = map.getProperties();

    mapWidth = prop.get("width", Integer.class);
    mapHeight = prop.get("height", Integer.class);
    tilePixelWidth = prop.get("tilewidth", Integer.class);
    tilePixelHeight = prop.get("tileheight", Integer.class);

    mapPixelWidth = (mapWidth - 2) * tilePixelWidth - 16;
    mapPixelHeight = (mapHeight - 2) * tilePixelHeight;

    font.scale(0.4f);/*from ww  w .j a  v  a  2 s .  com*/

    Peds = new Pedestrian[200];
    deadPeds = new DeadPed[numDeadPeds];
    police = new Police[10];
}

From source file:com.redtoorange.game.gameobject.GameMap.java

License:Open Source License

/**
 * Build a GameMap that will encapsulate a Tiled TMX Map.  Scaling is handled automatically.  Two Arrays will be
 * pulled from the TMX Map for object layers name: "walls", "playerspawn".
 *
 * @param parent   The gameObject this is placed under, usually the scene root.
 * @param mapPath  Rhe complete path from the Asset folder for the TMX file.
 * @param batch    The SpriteBatch to embed into the MapRenderer.
 * @param camera   The camera that is following the player, this is used for tile occlusion.
 * @param mapScale The amount to resize the entire map by.  1/16f if you want 16 map pixels to equal 1 game unit.
 */// w  ww.j a  v a 2 s  .  c o  m
public GameMap(GameObject parent, String mapPath, SpriteBatch batch, OrthographicCamera camera,
        float mapScale) {
    super(parent, new Vector2(0, 0));
    this.mapScale = mapScale;

    TmxMapLoader mapLoader = new TmxMapLoader(new InternalFileHandleResolver());
    map = mapLoader.load(mapPath);

    //Pull the properties from the TMX file.
    MapProperties properties = map.getProperties();
    enemyCount = properties.get("enemy", Integer.class);
    healthCount = properties.get("health", Integer.class);
    ammoCount = properties.get("ammo", Integer.class);

    maxWidth = properties.get("width", Integer.class);
    maxHeight = properties.get("height", Integer.class);

    mapRenderer = new MapRenderComponent(map, mapScale, batch, camera);

    buildWalls();
    buildPlayerSpawns();
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.TileAnimator.java

License:Apache License

/** animates the {@link TiledMapTileLayer target layer} using the given animations
 *  @param animations the animations to use
 *  @param layer the {@link TiledMapTileLayer} which tiles to animate
 *  @param animationKey the key used to tell if a tile is a frame
 *  @param intervalKey the key used to get the animation interval (duration each frame is displayed)
 *  @param defaultInterval the interval used if no value is found for the intervalKey */
public static void animateLayer(ObjectMap<String, Array<StaticTiledMapTile>> animations,
        TiledMapTileLayer layer, String animationKey, String intervalKey, float defaultInterval) {
    for (int x = 0; x < layer.getWidth(); x++)
        for (int y = 0; y < layer.getHeight(); y++) {
            Cell cell;// www.  java 2 s.com
            TiledMapTile tile;
            MapProperties tileProperties;
            if ((cell = layer.getCell(x, y)) != null && (tile = cell.getTile()) != null
                    && (tileProperties = tile.getProperties()).containsKey(animationKey)) {
                AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(
                        getProperty(tileProperties, intervalKey, defaultInterval),
                        animations.get(tileProperties.get(animationKey, String.class)));
                animatedTile.getProperties().putAll(tile.getProperties());
                cell.setTile(animatedTile);
            }
        }
}