/** Update any animations currently being played. * @param delta The time elapsed since last update, change this to alter the overall speed (can be negative). */ public void update (float delta) { if (paused) return; if (previous != null && ((transitionCurrentTime += delta) >= transitionTargetTime)) { removeAnimation(previous.animation); justChangedAnimation = true; animationPool.free(previous); previous = null; } if (justChangedAnimation) { target.calculateTransforms(); justChangedAnimation = false; } if (current == null || current.loopCount == 0 || current.animation == null) return; final float remain = current.update(delta); if (remain != 0f && queued != null) { inAction = false; animate(queued, queuedTransitionTime); queued = null; update(remain); return; } if (previous != null) applyAnimations(previous.animation, previous.offset + previous.time, current.animation, current.offset + current.time, transitionCurrentTime / transitionTargetTime); else applyAnimation(current.animation, current.offset + current.time); }
/** Set the active animation, replacing any current animation. * @param id The ID of the {@link Animation} within the {@link ModelInstance}. * @param loopCount The number of times to loop the animation, zero to play the animation only once, negative to continuously * loop the animation. * @return The {@link AnimationDesc} which can be read to get the progress of the animation. Will be invalid when the animation * is completed. */ public AnimationDesc setAnimation (final String id, int loopCount) { return setAnimation(id, loopCount, 1.0f, null); }
/** Apply an action animation on top of the current animation. */ protected AnimationDesc action (final Animation anim, float offset, float duration, int loopCount, float speed, final AnimationListener listener, float transitionTime) { return action(obtain(anim, offset, duration, loopCount, speed, listener), transitionTime); }
/** Queue an animation to be applied when the current is finished. If current is continuous it will be synced on next loop. */ protected AnimationDesc queue (final Animation anim, float offset, float duration, int loopCount, float speed, final AnimationListener listener, float transitionTime) { return queue(obtain(anim, offset, duration, loopCount, speed, listener), transitionTime); }
/** Changes the current animation by blending the new on top of the old during the transition time. */ protected AnimationDesc animate (final Animation anim, float offset, float duration, int loopCount, float speed, final AnimationListener listener, float transitionTime) { return animate(obtain(anim, offset, duration, loopCount, speed, listener), transitionTime); }
/** Changes the current animation by blending the new on top of the old during the transition time. * @param id The ID of the {@link Animation} within the {@link ModelInstance}. * @param loopCount The number of times to loop the animation, zero to play the animation only once, negative to continuously * loop the animation. * @param listener The {@link AnimationListener} which will be informed when the animation is looped or completed. * @param transitionTime The time to transition the new animation on top of the currently playing animation (if any). * @return The {@link AnimationDesc} which can be read to get the progress of the animation. Will be invalid when the animation * is completed. */ public AnimationDesc animate (final String id, int loopCount, final AnimationListener listener, float transitionTime) { return animate(id, loopCount, 1.0f, listener, transitionTime); }
@Override public void render () { if (character != null) { animation.update(Gdx.graphics.getDeltaTime()); if (Gdx.input.isKeyPressed(Keys.UP)) { if (!animation.inAction) { animation.animate("Walk", -1, 1f, null, 0.2f); status = walk; animation.animate("Walk", -1, -1f, null, 0.2f); status = back; animation.animate("Idle", -1, 1f, null, 0.2f); status = idle; animation.action("Attack", 1, 1f, null, 0.2f);
/** Set the active animation, replacing any current animation. */ protected AnimationDesc setAnimation (final Animation anim, float offset, float duration, int loopCount, float speed, final AnimationListener listener) { return setAnimation(obtain(anim, offset, duration, loopCount, speed, listener)); }
/** Apply an action animation on top of the current animation. */ protected AnimationDesc action (final AnimationDesc anim, float transitionTime) { if (anim.loopCount < 0) throw new GdxRuntimeException("An action cannot be continuous"); if (current == null || current.loopCount == 0) animate(anim, transitionTime); else { AnimationDesc toQueue = inAction ? null : obtain(current); inAction = false; animate(anim, transitionTime); inAction = true; if (toQueue != null) queue(toQueue, transitionTime); } return anim; } }
@Override protected void render (ModelBatch batch, Array<ModelInstance> instances) { for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries()) e.value.update(Gdx.graphics.getDeltaTime()); batch.render(instances, environment); }
character.transform.setToRotation(Vector3.Y, 180).trn(0, -bbox.min.y, 0); instances.add(character); animation = new AnimationController(character); animation.animate("Idle", -1, 1f, null, 0.2f); status = idle; for (Animation anim : character.animations)
@Override protected void onLoaded () { if (currentlyLoading == null || currentlyLoading.isEmpty()) return; instances.clear(); animationControllers.clear(); final ModelInstance instance = new ModelInstance(assets.get(currentlyLoading, Model.class)); for (Material m : instance.materials) m.set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, 0.8f)); instances.add(instance); if (instance.animations.size > 0) animationControllers.put(instance, new AnimationController(instance)); currentlyLoading = null; }
/** Changes the current animation by blending the new on top of the old during the transition time. */ protected AnimationDesc animate (final AnimationDesc anim, float transitionTime) { if (current == null) current = anim; else if (inAction) queue(anim, transitionTime); else if (!allowSameAnimation && anim != null && current.animation == anim.animation) { anim.time = current.time; animationPool.free(current); current = anim; } else { if (previous != null) { removeAnimation(previous.animation); animationPool.free(previous); } previous = current; current = anim; transitionCurrentTime = 0f; transitionTargetTime = transitionTime; } return anim; }
/** Apply an action animation on top of the current animation. * @param id The ID of the {@link Animation} within the {@link ModelInstance}. * @param loopCount The number of times to loop the animation, zero to play the animation only once, negative to continuously * loop the animation. * @param speed The speed at which the animation should be played. Default is 1.0f. A value of 2.0f will play the animation at * twice the normal speed, a value of 0.5f will play the animation at half the normal speed, etc. This value can be * negative, causing the animation to played in reverse. This value cannot be zero. * @param listener The {@link AnimationListener} which will be informed when the animation is looped or completed. * @param transitionTime The time to transition the new animation on top of the currently playing animation (if any). * @return The {@link AnimationDesc} which can be read to get the progress of the animation. Will be invalid when the animation * is completed. */ public AnimationDesc action (final String id, int loopCount, float speed, final AnimationListener listener, float transitionTime) { return action(id, 0, -1f, loopCount, speed, listener, transitionTime); }
private AnimationDesc obtain (final AnimationDesc anim) { return obtain(anim.animation, anim.offset, anim.duration, anim.loopCount, anim.speed, anim.listener); }
/** Queue an animation to be applied when the {@link #current} animation is finished. If the current animation is continuously * looping it will be synchronized on next loop. * @param id The ID of the {@link Animation} within the {@link ModelInstance}. * @param loopCount The number of times to loop the animation, zero to play the animation only once, negative to continuously * loop the animation. * @param speed The speed at which the animation should be played. Default is 1.0f. A value of 2.0f will play the animation at * twice the normal speed, a value of 0.5f will play the animation at half the normal speed, etc. This value can be * negative, causing the animation to played in reverse. This value cannot be zero. * @param listener The {@link AnimationListener} which will be informed when the animation is looped or completed. * @param transitionTime The time to transition the new animation on top of the currently playing animation (if any). * @return The {@link AnimationDesc} which can be read to get the progress of the animation. Will be invalid when the animation * is completed. */ public AnimationDesc queue (final String id, int loopCount, float speed, final AnimationListener listener, float transitionTime) { return queue(id, 0f, -1f, loopCount, speed, listener, transitionTime); }
/** Set the active animation, replacing any current animation. */ protected AnimationDesc setAnimation (final AnimationDesc anim) { if (current == null) current = anim; else { if (!allowSameAnimation && anim != null && current.animation == anim.animation) anim.time = current.time; else removeAnimation(current.animation); animationPool.free(current); current = anim; } justChangedAnimation = true; return anim; }
/** Changes the current animation by blending the new on top of the old during the transition time. * @param id The ID of the {@link Animation} within the {@link ModelInstance}. * @param transitionTime The time to transition the new animation on top of the currently playing animation (if any). * @return The {@link AnimationDesc} which can be read to get the progress of the animation. Will be invalid when the animation * is completed. */ public AnimationDesc animate (final String id, float transitionTime) { return animate(id, 1, 1.0f, null, transitionTime); }
/** Changes the current animation by blending the new on top of the old during the transition time. */ protected AnimationDesc animate (final Animation anim, float offset, float duration, int loopCount, float speed, final AnimationListener listener, float transitionTime) { return animate(obtain(anim, offset, duration, loopCount, speed, listener), transitionTime); }
/** Set the active animation, replacing any current animation. */ protected AnimationDesc setAnimation (final Animation anim, float offset, float duration, int loopCount, float speed, final AnimationListener listener) { return setAnimation(obtain(anim, offset, duration, loopCount, speed, listener)); }