Example usage for com.badlogic.gdx.graphics.g2d Animation getKeyFrame

List of usage examples for com.badlogic.gdx.graphics.g2d Animation getKeyFrame

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d Animation getKeyFrame.

Prototype

public TextureRegion getKeyFrame(float stateTime, boolean looping) 

Source Link

Document

Returns a TextureRegion based on the so called state time.

Usage

From source file:com.badlydrawngames.veryangryrobots.WorldView.java

License:Apache License

private void drawRobot(Robot robot) {
    Animation robotAnimation = null;
    switch (robot.state) {
    case Robot.SCANNING:
        robotAnimation = Assets.robotScanningAnimation;
        break;/*  www. j a  v a 2s .c  o m*/
    case Robot.WALKING_DOWN:
        robotAnimation = Assets.robotWalkingLeftAnimation;
        break;
    case Robot.WALKING_LEFT:
        robotAnimation = Assets.robotWalkingLeftAnimation;
        break;
    case Robot.WALKING_LEFT_DIAGONAL:
        robotAnimation = Assets.robotWalkingLeftAnimation;
        break;
    case Robot.WALKING_RIGHT:
        robotAnimation = Assets.robotWalkingRightAnimation;
        break;
    case Robot.WALKING_RIGHT_DIAGONAL:
        robotAnimation = Assets.robotWalkingRightAnimation;
        break;
    case Robot.WALKING_UP:
        robotAnimation = Assets.robotWalkingRightAnimation;
        break;
    default:
        robotAnimation = Assets.robotScanningAnimation;
    }
    draw(robot, robotAnimation.getKeyFrame(robot.stateTime, true));
}

From source file:com.dongbat.invasion.system.SpriteAnimationRenderSystem.java

@Override
protected void process(Entity entity) {
    SpriteAnimation spriteAnimation = sa.get(entity);
    DisplayPosition displayPosition = dp.get(entity);
    String current = spriteAnimation.getCurrent();
    if (current == null) {
        return;/*from   w w w  . j  a  va 2 s.  c  om*/
    }

    Animation animation = spriteAnimation.getAnimation(current);
    if (animation == null) {
        return;
    }

    long startTime = spriteAnimation.getStartTime();
    if (startTime == -1) {
        return;
    }

    float stateTime = TimeUtil.millisToSeconds(TimeUtil.getGameTimeSince(startTime));

    if (animation.isAnimationFinished(stateTime) && !spriteAnimation.isLooping()) {
        SpriteAnimation.AnimationFinishedCallback callback = spriteAnimation.getCallback();
        if (callback != null) {
            callback.finished(entity);
        }
        return;
    }

    TextureRegion frame = animation.getKeyFrame(stateTime, spriteAnimation.isLooping());
    // TODO: camera
    batch.draw(frame, displayPosition.getX() - frame.getRegionWidth() / 2f,
            displayPosition.getY() - frame.getRegionHeight() / 2f);
    batch.setProjectionMatrix(RenderCameraUtil.getCamera().combined);
}

From source file:com.laex.cg2d.entityeditor.pages.ExternalAnimationPreview.java

License:Open Source License

@Override
public void create() {
    super.create();

    this.shapeRenderer = new ShapeRenderer();

    Texture.setEnforcePotImages(false);/*w w  w  . j a  v a2 s  .  c o m*/

    batch = new SpriteBatch();
    w = Gdx.graphics.getWidth();
    h = Gdx.graphics.getHeight();

    cam = new OrthographicCamera(w, h);
    cam.position.set(0, 0, 0);

    CGEntityAnimation anim = null;

    FileInputStream fis;
    try {
        fis = new FileInputStream(cgeFile);
        CGEntity entity = CGEntity.parseFrom(fis);
        for (CGEntityAnimation ea : entity.getAnimationsList()) {
            if (ea.getAnimationName().equals(animationName)) {
                anim = ea;
            }
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }

    if (anim == null) {
        return;
    }

    FileHandle handle = Gdx.files.absolute(anim.getSpritesheetFile().getResourceFileAbsolute());
    if (!handle.exists()) {
        return;
    }

    Texture tex = new Texture(handle);
    tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    Array<TextureRegion> indexedFrames = new Array<TextureRegion>();
    for (CGEntitySpritesheetItem esi : anim.getSpritesheetItemsList()) {
        CGBounds b = esi.getExtractBounds();
        TextureRegion tr = new TextureRegion(tex, (int) b.getX(), (int) b.getY(), (int) b.getWidth(),
                (int) b.getHeight());
        indexedFrames.add(tr);
    }

    Animation sa = new Animation(anim.getAnimationDuration(), indexedFrames);
    spriteAnimation = Optional.fromNullable(sa);

    spr = new Sprite(sa.getKeyFrame(stateTime, true));
}

From source file:com.laex.cg2d.render.impl.EntityManager.java

License:Open Source License

/**
 * Creates the entity./*  w  ww . j  a v  a 2s  .  c  o m*/
 * 
 * @param shape
 *          the shape
 * @param animationName
 *          the animation name
 * @return the body
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public Body createEntity(CGShape shape, String animationName) throws IOException {
    CGEditorShapeType eType = shape.getEditorShapeType();

    if (eType == CGEditorShapeType.BACKGROUND_SHAPE) {
        return null;
    }

    /* We alo create shapes */
    if (eType == CGEditorShapeType.SIMPLE_SHAPE_BOX || eType == CGEditorShapeType.SIMPLE_SHAPE_CIRCLE
            || eType == CGEditorShapeType.SIMPLE_SHAPE_HEDGE || eType == CGEditorShapeType.SIMPLE_SHAPE_VEDGE) {
        Body b = manipulator.createBody(shape, null, null);
        b.setUserData(shape);
        return b;
    }

    /* For Entities */
    String entPath = shape.getEntityRefFile().getResourceFileAbsolute();
    final CGEntity entity = CGEntity.parseFrom(Gdx.files.absolute(entPath).read());

    shapeToEntityMap.put(shape, entity);

    /* Entity Animation */
    CGEntityAnimation ea = null;

    /* If name is not provided, get default animation */
    if (animationName == null) {
        ea = RunnerUtil.getDefaultAnimation(entity);
    } else {
        ea = RunnerUtil.getAnimationFrom(entity, animationName);
    }

    Body b = manipulator.createBody(shape, entity, ea);
    b.setUserData(shape);

    // Resource file empty indicates this entity might not have
    // image & collision shape defined.
    if (ea.getSpritesheetFile().getResourceFileAbsolute().trim().isEmpty()) {
        manipulator.world().destroyBody(b);
        return null;
    }

    FileHandle handle = Gdx.files.absolute(ea.getSpritesheetFile().getResourceFileAbsolute());

    Texture tex = new Texture(handle);
    tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    Array<TextureRegion> walkFrames = new Array<TextureRegion>();

    for (CGEntitySpritesheetItem cgEi : ea.getSpritesheetItemsList()) {
        CGBounds bnds = cgEi.getExtractBounds();
        TextureRegion tr = new TextureRegion(tex, (int) bnds.getX(), (int) bnds.getY(), (int) bnds.getWidth(),
                (int) bnds.getHeight());
        walkFrames.add(tr);
    }

    Animation spriteAnimation = new Animation(ea.getAnimationDuration(), walkFrames);
    shapeToAnimationMap.put(shape, spriteAnimation);

    Sprite spr = new Sprite(spriteAnimation.getKeyFrame(stateTime, true));

    // Set the position & size
    float x = shape.getBounds().getX();
    float y = shape.getBounds().getY();
    float width = shape.getBounds().getWidth();
    float height = shape.getBounds().getHeight();

    Vector2 scrPos = new Vector2(x, y);
    Vector2 worldPos = ScreenToWorld.inst(manipulator.model()).screenToWorldFlipped(scrPos, height);
    spr.setPosition(worldPos.x, worldPos.y);

    // the position circle (collision shape) will vary with that of
    // a box. so
    // we need to check and set position for each types
    if (ea.getCollisionType() == CGEntityCollisionType.CIRCLE) {
        Vector3 origin = shapeToAnimationOriginMap.get(shape);

        float radius = origin.z;

        float x1 = (worldPos.x - radius);
        float y1 = (worldPos.y - radius);
        spr.setPosition(x1, y1);
    }

    float w = ((float) width / manipulator.ptmRatio());
    float h = ((float) height / manipulator.ptmRatio());

    spr.setSize(w, h);

    shapeToSpriteMap.put(shape, spr);

    return b;
}

From source file:com.laex.cg2d.render.impl.EntityManager.java

License:Open Source License

@Override
public void render() {
    if (!drawEntities) {
        return;/*from w  w w  . j  a  va 2s . c o m*/
    }

    batch.begin();

    manipulator.acceptBodyVisitor(new BodyVisitor() {

        @Override
        public void visit(Body b, CGShape shape) {
            if (!(shape.getEditorShapeType() == CGEditorShapeType.ENTITY_SHAPE)) {
                return;
            }

            Vector2 pos = b.getPosition();

            // position for circle collision shape & box collision shape differ.
            CGEntity e = shapeToEntityMap.get(shape);
            CGEntityAnimation ea = RunnerUtil.getDefaultAnimation(e);

            // Position of circle should be adjusted of radius.
            if (ea.getCollisionType() == CGEntityCollisionType.CIRCLE) {
                float radius = shapeToAnimationOriginMap.get(shape).z;
                pos.x = pos.x - radius;
                pos.y = pos.y - radius;
            }

            Sprite spr = shapeToSpriteMap.get(shape);
            if (spr == null) {
                return;
            }

            spr.setPosition(pos.x, pos.y);
            // setting origin important for rotations to work properly
            Vector3 origin = shapeToAnimationOriginMap.get(shape);
            spr.setOrigin(origin.x, origin.y);
            spr.setRotation(b.getAngle() * MathUtils.radiansToDegrees);

            Animation anim = shapeToAnimationMap.get(shape);

            TextureRegion tr = anim.getKeyFrame(stateTime, true);
            spr.setRegion(tr);
            spr.draw(batch);
        }
    });

    batch.end();
}

From source file:com.sturdyhelmetgames.dodgethecars.entity.Car.java

License:Apache License

@Override
public void render(SpriteBatch spriteBatch, float delta) {
    super.render(spriteBatch, delta);
    final float scale = getScale();

    // render animation based on direction
    if (direction == Direction.LEFT) {
        Animation animation = null;
        if (color == CAR_COLOR_RED) {
            animation = Art.carRedDriveLeftAnimation;
        } else if (color == CAR_COLOR_CYAN) {
            animation = Art.carCyanDriveLeftAnimation;
        } else if (color == CAR_COLOR_GREEN) {
            animation = Art.carGreenDriveLeftAnimation;
        }//from  w w  w.jav  a  2  s .  c o m
        spriteBatch.draw(animation.getKeyFrame(animationState, true), x, y, 0f, 0f, width, height, scale, scale,
                0f);
    } else if (direction == Direction.RIGHT) {
        // x offset is calculated because of car's shadow behaves slightly differently
        final float x = this.x - RENDER_X_OFFSET;
        Animation animation = null;
        if (color == CAR_COLOR_RED) {
            animation = Art.carRedDriveRightAnimation;
        } else if (color == CAR_COLOR_CYAN) {
            animation = Art.carCyanDriveRightAnimation;
        } else if (color == CAR_COLOR_GREEN) {
            animation = Art.carGreenDriveRightAnimation;
        }
        spriteBatch.draw(animation.getKeyFrame(animationState, true), x, y, 0f, 0f, width, height, scale, scale,
                0f);
    }
}

From source file:com.sturdyhelmetgames.roomforchange.entity.KingSnake.java

License:Apache License

@Override
public void render(float delta, SpriteBatch batch) {
    if (blinkTick < BLINK_TICK_MAX) {
        super.render(delta, batch);
        Animation animation = null;

        if (direction == Direction.UP) {
            animation = Assets.kingSnakeWalkBack;
        } else if (direction == Direction.DOWN) {
            animation = Assets.kingSnakeWalkFront;
        } else if (direction == Direction.RIGHT) {
            animation = Assets.kingSnakeWalkRight;
        } else if (direction == Direction.LEFT) {
            animation = Assets.kingSnakeWalkLeft;
        }/*from  w ww.  j av  a2s  .c  om*/

        batch.draw(animation.getKeyFrame(stateTime, true), bounds.x - 0.1f, bounds.y - 0.1f, width, height);

    }
}

From source file:com.sturdyhelmetgames.roomforchange.entity.Mummy.java

License:Apache License

@Override
public void render(float delta, SpriteBatch batch) {
    if (blinkTick < BLINK_TICK_MAX) {
        super.render(delta, batch);
        Animation animation = null;

        if (direction == Direction.UP) {
            animation = Assets.mummyWalkBack;
        } else if (direction == Direction.DOWN) {
            animation = Assets.mummyWalkFront;
        } else if (direction == Direction.RIGHT) {
            animation = Assets.mummyWalkRight;
        } else if (direction == Direction.LEFT) {
            animation = Assets.mummyWalkLeft;
        }/*from   w w w.  java2  s.co  m*/

        batch.draw(animation.getKeyFrame(stateTime, true), bounds.x - 0.1f, bounds.y - 0.1f, width,
                height + 0.4f);
    }
}

From source file:com.sturdyhelmetgames.roomforchange.entity.Snake.java

License:Apache License

@Override
public void render(float delta, SpriteBatch batch) {
    if (blinkTick < BLINK_TICK_MAX) {
        super.render(delta, batch);
        Animation animation = null;

        if (direction == Direction.UP) {
            animation = Assets.snakeWalkBack;
        } else if (direction == Direction.DOWN) {
            animation = Assets.snakeWalkFront;
        } else if (direction == Direction.RIGHT) {
            animation = Assets.snakeWalkRight;
        } else if (direction == Direction.LEFT) {
            animation = Assets.snakeWalkLeft;
        }/*from  w w  w  . jav a  2  s  . c  om*/

        batch.draw(animation.getKeyFrame(stateTime, true), bounds.x - 0.1f, bounds.y - 0.1f, width, height);
    }
}

From source file:de.brainstormsoftworks.taloonerrl.render.RenderUtil.java

License:Open Source License

/**
 * retrieves the key frame for the given animation
 *
 * @param animation//w w  w  .j av a  2  s  .c  om
 *            to get the frame for
 * @return texture region
 */
public static TextureRegion getKeyFrame(final Animation animation) {
    return animation.getKeyFrame(GameEngine.getInstance().getStateTime(), true);
}