Search in sources :

Example 1 with Box2DPhysicsObject

use of com.indignado.games.smariano.model.entities.base.Box2DPhysicsObject in project Entitas-Java by Rubentxu.

the class Box2DMapObjectParser method createBody.

/**
 * 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}
 */
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;
}
Also used : MapProperties(com.badlogic.gdx.maps.MapProperties) Box2DPhysicsObject(com.indignado.games.smariano.model.entities.base.Box2DPhysicsObject)

Example 2 with Box2DPhysicsObject

use of com.indignado.games.smariano.model.entities.base.Box2DPhysicsObject in project Entitas-Java by Rubentxu.

the class Box2DMapObjectParser method createBody.

/**
 * 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}
 */
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;
}
Also used : Box2DPhysicsObject(com.indignado.games.smariano.model.entities.base.Box2DPhysicsObject)

Aggregations

Box2DPhysicsObject (com.indignado.games.smariano.model.entities.base.Box2DPhysicsObject)2 MapProperties (com.badlogic.gdx.maps.MapProperties)1