OSDN Git Service

Fix animations... i hope
authorXoppa <contact@xoppa.nl>
Sun, 22 Sep 2013 23:01:34 +0000 (01:01 +0200)
committerXoppa <contact@xoppa.nl>
Sun, 22 Sep 2013 23:01:34 +0000 (01:01 +0200)
gdx/src/com/badlogic/gdx/graphics/g3d/utils/AnimationController.java
gdx/src/com/badlogic/gdx/graphics/g3d/utils/BaseAnimationController.java

index 02fa944..9e01293 100644 (file)
@@ -109,6 +109,8 @@ public class AnimationController extends BaseAnimationController {
        public float transitionTargetTime;
        /** Whether an action is being performed. Do not alter this value. */
        public boolean inAction;
+       
+       private boolean justChangedAnimation = false;
 
        /** Construct a new AnimationController.
         * @param target The {@link ModelInstance} on which the animations will be performed. */
@@ -117,6 +119,8 @@ public class AnimationController extends BaseAnimationController {
        }
        
        private AnimationDesc obtain(final Animation anim, int loopCount, float speed, final AnimationListener listener) {
+               if (anim == null)
+                       return null;
                final AnimationDesc result = animationPool.obtain();
                result.animation = anim;
                result.listener = listener;
@@ -127,6 +131,8 @@ public class AnimationController extends BaseAnimationController {
        }
        
        private AnimationDesc obtain(final String id, int loopCount, float speed, final AnimationListener listener) {
+               if (id == null)
+                       return null;
                final Animation anim = target.getAnimation(id);
                if (anim == null)
                        throw new GdxRuntimeException("Unknown animation: "+id);
@@ -141,8 +147,19 @@ public class AnimationController extends BaseAnimationController {
        /** 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 (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;
+               justChangedAnimation = false;
                updating = true;
                final float remain = current.update(delta);
                if (remain != 0f && queued != null) {
@@ -153,10 +170,6 @@ public class AnimationController extends BaseAnimationController {
                        update(remain);
                        return;
                }
-               if (previous != null && ((transitionCurrentTime += delta) >= transitionTargetTime)) {
-                       animationPool.free(previous);
-                       previous = null;
-               }
                if (previous != null)
                        applyAnimations(previous.animation, previous.time, current.animation, current.time, transitionCurrentTime / transitionTargetTime);
                else
@@ -219,11 +232,14 @@ public class AnimationController extends BaseAnimationController {
                if (current == null)
                        current = anim;
                else {
-                       if (current.animation == anim.animation)
+                       if (anim != null && current.animation == anim.animation)
                                anim.time = current.time;
+                       else
+                               removeAnimation(current.animation);
                        animationPool.free(current);
                        current = anim;
                }
+               justChangedAnimation = true;
                return anim;
        }
        
@@ -285,7 +301,7 @@ public class AnimationController extends BaseAnimationController {
                        current = anim;
                else if (inAction)
                        queue(anim, transitionTime);
-               else if (current.animation == anim.animation) {
+               else if (anim != null && current.animation == anim.animation) {
                        anim.time = current.time;
                        animationPool.free(current);
                        current = anim;
index 055989f..e59ec49 100644 (file)
@@ -1,5 +1,6 @@
 package com.badlogic.gdx.graphics.g3d.utils;
 
+import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.g3d.ModelInstance;
 import com.badlogic.gdx.graphics.g3d.model.Animation;
 import com.badlogic.gdx.graphics.g3d.model.Node;
@@ -170,4 +171,12 @@ public class BaseAnimationController {
                        }
                }
        }
+       
+       /** Remove the specified animation, by marking the affected nodes as not animated. When switching animation, this should
+        * be call prior to applyAnimation(s). */
+       protected void removeAnimation(final Animation animation) {
+               for (final NodeAnimation nodeAnim : animation.nodeAnimations) {
+                       nodeAnim.node.isAnimated = false;
+               }
+       }
 }