Search in sources :

Example 1 with RubeImage

use of com.gushikustudios.rube.loader.serializers.utils.RubeImage in project RubeLoader by tescott.

the class ImageSerializer method read.

@SuppressWarnings("rawtypes")
@Override
public RubeImage read(Json json, JsonValue jsonData, Class type) {
    // Images reference bodies based on indexing in the .json file. -1 means no body reference
    Array<Body> bodies = scene.getBodies();
    RubeImage defaults = RubeDefaults.Image.image;
    RubeImage image = new RubeImage();
    image.angleInRads = json.readValue("angle", float.class, defaults.angleInRads, jsonData);
    int bodyIndex = json.readValue("body", int.class, jsonData);
    if (bodyIndex >= 0) {
        bodyIndex += scene.getCurrentBodyOffset();
        if ((bodies != null) && (bodyIndex < bodies.size)) {
            image.body = bodies.get(bodyIndex);
        } else {
            throw new RuntimeException("RubeImage creation error.  bodies: " + bodies + ", bodyIndex: " + bodyIndex);
        }
    }
    image.center.set(json.readValue("center", Vector2.class, defaults.center, jsonData));
    RubeVertexArray corners = json.readValue("corners", RubeVertexArray.class, jsonData);
    if (corners != null) {
        mTmp.set(corners.x[0], corners.y[0]).sub(corners.x[1], corners.y[1]);
        image.width = mTmp.len();
        mTmp.set(corners.x[1], corners.y[1]).sub(corners.x[2], corners.y[2]);
        image.height = mTmp.len();
    }
    image.file = json.readValue("file", String.class, jsonData);
    image.filter = json.readValue("filter", int.class, defaults.filter, jsonData);
    image.flip = json.readValue("flip", boolean.class, defaults.flip, jsonData);
    image.name = json.readValue("name", String.class, jsonData);
    image.opacity = json.readValue("opacity", float.class, defaults.opacity, jsonData);
    int[] colorArray = json.readValue("colorTint", int[].class, RubeDefaults.Image.colorArray, jsonData);
    image.color.set((float) colorArray[0] / 255, (float) colorArray[1] / 255, (float) colorArray[2] / 255, (float) colorArray[3] / 255);
    image.renderOrder = json.readValue("renderOrder", int.class, defaults.renderOrder, jsonData);
    image.scale = json.readValue("scale", float.class, defaults.scale, jsonData);
    scene.parseCustomProperties(json, image, jsonData);
    String name = json.readValue("name", String.class, jsonData);
    if (name != null) {
        scene.putNamed(name, image);
    }
    return image;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) RubeVertexArray(com.gushikustudios.rube.loader.serializers.utils.RubeVertexArray) Body(com.badlogic.gdx.physics.box2d.Body) RubeImage(com.gushikustudios.rube.loader.serializers.utils.RubeImage)

Example 2 with RubeImage

use of com.gushikustudios.rube.loader.serializers.utils.RubeImage in project RubeLoader by tescott.

the class WorldSerializer method read.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public World read(Json json, JsonValue jsonData, Class type) {
    World world = scene.getWorld();
    if (world == null) {
        boolean allowSleep = json.readValue("allowSleep", boolean.class, RubeDefaults.World.allowSleep, jsonData);
        boolean autoClearForces = json.readValue("autoClearForces", boolean.class, RubeDefaults.World.autoClearForces, jsonData);
        boolean continuousPhysics = json.readValue("continuousPhysics", boolean.class, RubeDefaults.World.continuousPhysics, jsonData);
        boolean warmStarting = json.readValue("warmStarting", boolean.class, RubeDefaults.World.warmStarting, jsonData);
        Vector2 gravity = json.readValue("gravity", Vector2.class, RubeDefaults.World.gravity, jsonData);
        world = new World(gravity, allowSleep);
        world.setAutoClearForces(autoClearForces);
        world.setContinuousPhysics(continuousPhysics);
        world.setWarmStarting(warmStarting);
    }
    // else ignore world settings and use the ones that were previously loaded
    scene.parseCustomProperties(json, world, jsonData);
    // Bodies
    bodySerializer.setWorld(world);
    Array<Body> bodies = json.readValue("body", Array.class, Body.class, jsonData);
    if (bodies != null) {
        if (scene.getBodies() == null) {
            scene.setBodies(bodies);
        } else {
            scene.addBodies(bodies);
        }
    }
    // Joints
    // joints are done in two passes because gear joints reference other joints
    // First joint pass
    jointSerializer.init(world, bodies, null);
    Array<Joint> joints = json.readValue("joint", Array.class, Joint.class, jsonData);
    if (joints != null) {
        if (scene.getJoints() == null) {
            scene.setJoints(joints);
        } else {
            scene.getJoints().addAll(joints);
        }
    }
    // Second joint pass
    jointSerializer.init(world, bodies, joints);
    joints = json.readValue("joint", Array.class, Joint.class, jsonData);
    // Images
    Array<RubeImage> images = json.readValue("image", Array.class, RubeImage.class, jsonData);
    if (images != null) {
        if (scene.getImages() == null) {
            scene.setImages(images);
        } else {
            scene.getImages().addAll(images);
        }
        for (int i = 0; i < images.size; i++) {
            RubeImage image = images.get(i);
            scene.setMappedImage(image.body, image);
        }
    }
    return world;
}
Also used : Array(com.badlogic.gdx.utils.Array) Vector2(com.badlogic.gdx.math.Vector2) World(com.badlogic.gdx.physics.box2d.World) Joint(com.badlogic.gdx.physics.box2d.Joint) Body(com.badlogic.gdx.physics.box2d.Body) RubeImage(com.gushikustudios.rube.loader.serializers.utils.RubeImage) Joint(com.badlogic.gdx.physics.box2d.Joint)

Example 3 with RubeImage

use of com.gushikustudios.rube.loader.serializers.utils.RubeImage in project RubeLoader by tescott.

the class RubeLoaderTest method createSpatialsFromRubeImages.

/**
    * Creates an array of SimpleSpatial objects from RubeImages.
    * 
    * @param scene2
    */
private void createSpatialsFromRubeImages(RubeScene scene) {
    Array<RubeImage> images = scene.getImages();
    if ((images != null) && (images.size > 0)) {
        spatials = new Array<SimpleSpatial>();
        for (int i = 0; i < images.size; i++) {
            RubeImage image = images.get(i);
            mTmp.set(image.width, image.height);
            String textureFileName = "data/" + image.file;
            Texture texture = textureMap.get(textureFileName);
            if (texture == null) {
                texture = new Texture(textureFileName);
                textureMap.put(textureFileName, texture);
            }
            SimpleSpatial spatial = new SimpleSpatial(texture, image.flip, image.body, image.color, mTmp, image.center, image.angleInRads * MathUtils.radiansToDegrees);
            spatials.add(spatial);
        }
    }
}
Also used : RubeImage(com.gushikustudios.rube.loader.serializers.utils.RubeImage) Texture(com.badlogic.gdx.graphics.Texture)

Aggregations

RubeImage (com.gushikustudios.rube.loader.serializers.utils.RubeImage)3 Vector2 (com.badlogic.gdx.math.Vector2)2 Body (com.badlogic.gdx.physics.box2d.Body)2 Texture (com.badlogic.gdx.graphics.Texture)1 Joint (com.badlogic.gdx.physics.box2d.Joint)1 World (com.badlogic.gdx.physics.box2d.World)1 Array (com.badlogic.gdx.utils.Array)1 RubeVertexArray (com.gushikustudios.rube.loader.serializers.utils.RubeVertexArray)1