Search in sources :

Example 1 with Animation

use of com.gemserk.animation4j.gdx.Animation in project commons-gdx by gemserk.

the class AnimationFromTextureAtlasResourceBuilder method build.

@Override
public Animation build() {
    TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
    if (sprites == null) {
        try {
            sprites = textureAtlas.createSprites(prefix);
        } catch (GdxRuntimeException e) {
            throw new RuntimeException("Failed to create animation from texture atlas " + textureAtlasId, e);
        }
    }
    if (endFrame == -1)
        endFrame = sprites.size - 1;
    if (startFrame == -1)
        startFrame = 0;
    Sprite[] frames = new Sprite[endFrame - startFrame + 1];
    int frameNumber = startFrame;
    for (int i = 0; i < frames.length; i++) {
        Sprite sprite = sprites.get(frameNumber);
        if (sprite instanceof AtlasSprite)
            frames[i] = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
        else
            frames[i] = new Sprite(sprite);
        frameTransformation.transform(frames[i]);
        frameNumber++;
    }
    int framesCount = frames.length;
    float[] newTimes = new float[framesCount - 1];
    int lastTime = time;
    for (int i = 0; i < framesCount - 1; i++) {
        if (i < times.length) {
            newTimes[i] = ((float) times[i]) * 0.001f;
            lastTime = times[i];
        } else
            newTimes[i] = ((float) lastTime) * 0.001f;
    }
    FrameAnimationImpl frameAnimation = new FrameAnimationImpl(0.001f * (float) time, newTimes);
    frameAnimation.setLoop(loop);
    return new Animation(frames, frameAnimation);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FrameAnimationImpl(com.gemserk.animation4j.FrameAnimationImpl) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Animation(com.gemserk.animation4j.gdx.Animation)

Example 2 with Animation

use of com.gemserk.animation4j.gdx.Animation in project commons-gdx by gemserk.

the class AnimationMonitorTest method shouldNotBeShownWhenStarted.

@Test
public void shouldNotBeShownWhenStarted() {
    Animation animation = new Animation(new Sprite[] {}, new FrameAnimationImpl(true, 50f, 25f, 30f));
    AnimationFrameMonitor animationMonitor = new AnimationFrameMonitor(animation, 50f);
    animation.update(1f);
    animationMonitor.update();
    assertFalse(animationMonitor.isTriggered());
}
Also used : Animation(com.gemserk.animation4j.gdx.Animation) AnimationFrameMonitor(com.gemserk.animation4j.gdx.AnimationFrameMonitor) Test(org.junit.Test)

Example 3 with Animation

use of com.gemserk.animation4j.gdx.Animation in project commons-gdx by gemserk.

the class AnimationMonitorTest method shouldNotTriggerAgainOnDifferentAnimationIterationButUpdatedTwice.

@Test
public void shouldNotTriggerAgainOnDifferentAnimationIterationButUpdatedTwice() {
    Animation animation = new Animation(new Sprite[] {}, new FrameAnimationImpl(true, 50f, 25f, 30f));
    AnimationFrameMonitor animationMonitor = new AnimationFrameMonitor(animation, 50f);
    animation.update(51f);
    animationMonitor.update();
    animationMonitor.update();
    animation.update(25f + 30f + 50f);
    animationMonitor.update();
    animationMonitor.update();
    assertFalse(animationMonitor.isTriggered());
}
Also used : Animation(com.gemserk.animation4j.gdx.Animation) AnimationFrameMonitor(com.gemserk.animation4j.gdx.AnimationFrameMonitor) Test(org.junit.Test)

Example 4 with Animation

use of com.gemserk.animation4j.gdx.Animation in project commons-gdx by gemserk.

the class AnimationMonitorTest method shouldTriggerAgainOnDifferentAnimationIteration.

@Test
public void shouldTriggerAgainOnDifferentAnimationIteration() {
    Animation animation = new Animation(new Sprite[] {}, new FrameAnimationImpl(true, 50f, 25f, 30f));
    AnimationFrameMonitor animationMonitor = new AnimationFrameMonitor(animation, 50f);
    animation.update(51f);
    animationMonitor.update();
    animationMonitor.update();
    animation.update(25f + 30f + 50f);
    animationMonitor.update();
    assertTrue(animationMonitor.isTriggered());
}
Also used : Animation(com.gemserk.animation4j.gdx.Animation) AnimationFrameMonitor(com.gemserk.animation4j.gdx.AnimationFrameMonitor) Test(org.junit.Test)

Example 5 with Animation

use of com.gemserk.animation4j.gdx.Animation in project commons-gdx by gemserk.

the class AnimationMonitorTest method shouldTriggerIfDirecltyChangedIteration.

@Test
public void shouldTriggerIfDirecltyChangedIteration() {
    Animation animation = new Animation(new Sprite[] {}, new FrameAnimationImpl(true, 50f, 25f, 30f));
    AnimationFrameMonitor animationMonitor = new AnimationFrameMonitor(animation, 50f);
    animation.update(50f + 25f + 31f);
    animationMonitor.update();
    assertTrue(animationMonitor.isTriggered());
}
Also used : Animation(com.gemserk.animation4j.gdx.Animation) AnimationFrameMonitor(com.gemserk.animation4j.gdx.AnimationFrameMonitor) Test(org.junit.Test)

Aggregations

Animation (com.gemserk.animation4j.gdx.Animation)11 AnimationFrameMonitor (com.gemserk.animation4j.gdx.AnimationFrameMonitor)8 Test (org.junit.Test)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)3 FrameAnimationImpl (com.gemserk.animation4j.FrameAnimationImpl)3 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 Texture (com.badlogic.gdx.graphics.Texture)1 Array (com.badlogic.gdx.utils.Array)1 FloatValue (com.gemserk.commons.values.FloatValue)1 ArrayList (java.util.ArrayList)1