Search in sources :

Example 6 with Animation

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

the class AnimationMonitorTest method shouldNotReturnTriggerTwice.

@Test
public void shouldNotReturnTriggerTwice() {
    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();
    assertFalse(animationMonitor.isTriggered());
}
Also used : Animation(com.gemserk.animation4j.gdx.Animation) AnimationFrameMonitor(com.gemserk.animation4j.gdx.AnimationFrameMonitor) Test(org.junit.Test)

Example 7 with Animation

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

the class AnimationMonitorTest method shouldNotTriggerIfNotFrameOnSecondIteration.

@Test
public void shouldNotTriggerIfNotFrameOnSecondIteration() {
    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 + 25f);
    animationMonitor.update();
    assertFalse(animationMonitor.isTriggered());
}
Also used : Animation(com.gemserk.animation4j.gdx.Animation) AnimationFrameMonitor(com.gemserk.animation4j.gdx.AnimationFrameMonitor) Test(org.junit.Test)

Example 8 with Animation

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

the class AnimationMonitorTest method testWasShowWhenItShouldBe.

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

Example 9 with Animation

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

the class AnimationMonitorTest method shouldHadBeenShownIfGreaterFrame.

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

Example 10 with Animation

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

the class LibgdxResourceBuilder method animation.

public void animation(final String id, final String textureAtlasId, final String prefix, final int sf, final int ef, final boolean loop, final boolean removeAlias, final float time, final float... times) {
    resourceManager.addVolatile(id, new DataLoader<Animation>() {

        class DuplicatedSpritesRemover {

            float[] times;

            Sprite[] frames;

            public void removeDuplicates(Sprite[] frames, float[] times) {
                ArrayList<Sprite> newSprites = new ArrayList<Sprite>();
                ArrayList<FloatValue> newTimes = new ArrayList<FloatValue>();
                Sprite lastSprite = null;
                int i = 0;
                FloatValue frameTime = null;
                do {
                    Sprite sprite = frames[i];
                    if (SpriteUtils.isAliasSprite(lastSprite, sprite)) {
                        frameTime.value += times[i];
                    } else {
                        newSprites.add(sprite);
                        lastSprite = sprite;
                        frameTime = new FloatValue(times[i]);
                        newTimes.add(frameTime);
                    }
                    i++;
                } while (i < frames.length);
                this.frames = new Sprite[newSprites.size()];
                this.times = new float[newTimes.size()];
                newSprites.toArray(this.frames);
                for (i = 0; i < newTimes.size(); i++) this.times[i] = newTimes.get(i).value;
            }
        }

        FrameAnimationImpl cachedFrameAnimation = null;

        Animation cachedAnimation = null;

        @Override
        public Animation load() {
            if (cachedAnimation == null) {
                TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
                Array<Sprite> sprites = null;
                try {
                    sprites = textureAtlas.createSprites(prefix);
                } catch (GdxRuntimeException e) {
                    throw new RuntimeException("Failed to create animation " + id + " from texture atlas " + textureAtlasId, e);
                }
                if (sprites.size == 0)
                    throw new IllegalArgumentException("Failed to create animation " + id + ", no regions found for prefix " + prefix);
                int endFrame = ef;
                int startFrame = sf;
                if (endFrame == -1)
                    endFrame = sprites.size - 1;
                if (startFrame == -1)
                    startFrame = 0;
                Sprite[] frames = new Sprite[endFrame - startFrame + 1];
                int frameNumber = startFrame;
                if (endFrame >= sprites.size) {
                    throw new IllegalArgumentException("Failed to create animation " + id + ", end frame " + endFrame + " couldn't be greater than sprites quantity " + sprites.size);
                }
                int framesCount = frames.length;
                float[] newTimes = new float[framesCount];
                // newTimes[0] = 0.001f * (float) time;
                newTimes[0] = time;
                float lastTime = newTimes[0];
                for (int i = 1; i < framesCount; i++) {
                    if (i < times.length) {
                        // newTimes[i] = ((float) times[i]) * 0.001f;
                        newTimes[i] = times[i];
                        lastTime = newTimes[i];
                    } else
                        newTimes[i] = lastTime;
                }
                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);
                    frameNumber++;
                }
                if (removeAlias) {
                    int framesBeforeRemoval = frames.length;
                    DuplicatedSpritesRemover duplicatedSpritesRemover = new DuplicatedSpritesRemover();
                    duplicatedSpritesRemover.removeDuplicates(frames, newTimes);
                    frames = duplicatedSpritesRemover.frames;
                    newTimes = duplicatedSpritesRemover.times;
                    Gdx.app.log("commons-gdx", "[" + id + "] frames removed: " + (framesBeforeRemoval - frames.length));
                }
                cachedFrameAnimation = new FrameAnimationImpl(newTimes);
                cachedFrameAnimation.setLoop(loop);
                cachedAnimation = new Animation(frames, cachedFrameAnimation);
            }
            Sprite[] frames = new Sprite[cachedAnimation.getFramesCount()];
            for (int i = 0; i < frames.length; i++) {
                Sprite sprite = cachedAnimation.getFrame(i);
                if (sprite instanceof AtlasSprite)
                    frames[i] = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
                else
                    frames[i] = new Sprite(sprite);
            }
            return new Animation(frames, new FrameAnimationImpl(cachedFrameAnimation));
        }
    });
}
Also used : AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) ArrayList(java.util.ArrayList) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Array(com.badlogic.gdx.utils.Array) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FrameAnimationImpl(com.gemserk.animation4j.FrameAnimationImpl) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Animation(com.gemserk.animation4j.gdx.Animation) FloatValue(com.gemserk.commons.values.FloatValue)

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