Browse Source

Migrate TestOgreAnim to new anim system (#1333)

* Migrate TestOgreAnim to new anim system

* Removing tabs

* Update TestOgreAnim.java
Jérôme 5 years ago
parent
commit
4c8e400a83
1 changed files with 62 additions and 43 deletions
  1. 62 43
      jme3-examples/src/main/java/jme3test/model/anim/TestOgreAnim.java

+ 62 - 43
jme3-examples/src/main/java/jme3test/model/anim/TestOgreAnim.java

@@ -32,23 +32,30 @@
 
 package jme3test.model.anim;
 
-import com.jme3.animation.*;
+import com.jme3.anim.AnimClip;
+import com.jme3.anim.AnimComposer;
+import com.jme3.anim.SkinningControl;
+import com.jme3.anim.tween.Tween;
+import com.jme3.anim.tween.action.Action;
+import com.jme3.anim.tween.action.BaseAction;
+import com.jme3.anim.tween.action.LinearBlendSpace;
 import com.jme3.app.SimpleApplication;
 import com.jme3.input.KeyInput;
 import com.jme3.input.controls.ActionListener;
 import com.jme3.input.controls.KeyTrigger;
 import com.jme3.light.DirectionalLight;
-import com.jme3.math.*;
-import com.jme3.scene.*;
+import com.jme3.math.ColorRGBA;
+import com.jme3.math.Quaternion;
+import com.jme3.math.Vector3f;
+import com.jme3.scene.Geometry;
+import com.jme3.scene.Node;
+import com.jme3.scene.Spatial;
 import com.jme3.scene.shape.Box;
 
-//TODO rework this Test when the new animation system is done.
-public class TestOgreAnim extends SimpleApplication
-        implements AnimEventListener, ActionListener {
+public class TestOgreAnim extends SimpleApplication implements ActionListener {
 
-    private AnimChannel channel;
-    private AnimControl control;
-    private Geometry geom;
+    private AnimComposer animComposer;
+    private static LoopAction currentAction;
 
     public static void main(String[] args) {
         TestOgreAnim app = new TestOgreAnim();
@@ -66,25 +73,25 @@ public class TestOgreAnim extends SimpleApplication
         dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
         rootNode.addLight(dl);
 
-        Spatial model = assetManager.loadModel("Models/Oto/OtoOldAnim.j3o");
+        Spatial model = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
         model.center();
 
-        control = model.getControl(AnimControl.class);
-        control.addListener(this);
-        channel = control.createChannel();
-
-        for (String anim : control.getAnimationNames())
-            System.out.println(anim);
+        animComposer = model.getControl(AnimComposer.class);
+        animComposer.actionBlended("Attack", new LinearBlendSpace(0f, 0.5f), "Dodge");
+        for (AnimClip animClip : animComposer.getAnimClips()) {
+            Action action = animComposer.action(animClip.getName());
+            animComposer.addAction(animClip.getName(), new LoopAction(action, animComposer));
+        }
+        currentAction = (LoopAction) animComposer.setCurrentAction("stand"); // Walk, pull, Dodge, stand, push
 
-        channel.setAnim("stand");
-        geom = (Geometry)((Node)model).getChild(0);
-        SkeletonControl skeletonControl = model.getControl(SkeletonControl.class);
+        SkinningControl skinningControl = model.getControl(SkinningControl.class);
+        skinningControl.setHardwareSkinningPreferred(false);
 
-        Box b = new Box(.25f,3f,.25f);
+        Box b = new Box(.25f, 3f, .25f);
         Geometry item = new Geometry("Item", b);
         item.move(0, 1.5f, 0);
         item.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
-        Node n = skeletonControl.getAttachmentsNode("hand.right");
+        Node n = skinningControl.getAttachmentsNode("hand.right");
         n.attachChild(item);
 
         rootNode.attachChild(model);
@@ -94,35 +101,47 @@ public class TestOgreAnim extends SimpleApplication
     }
 
     @Override
-    public void simpleUpdate(float tpf) {
-        super.simpleUpdate(tpf);
-//                        geom.getMesh().createCollisionData();
-
+    public void onAction(String binding, boolean value, float tpf) {
+        if (binding.equals("Attack") && value) {
+            if (currentAction != null && !currentAction.equals(animComposer.getAction("Dodge"))) {
+                currentAction = (LoopAction) animComposer.setCurrentAction("Dodge");
+                currentAction.setNextAnim("stand");
+                currentAction.setSpeed(0.1f);
+            }
+        }
     }
 
+    static class LoopAction extends BaseAction {
 
-    @Override
-    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
-        if (animName.equals("Dodge")){
-            channel.setAnim("stand", 0.50f);
-            channel.setLoopMode(LoopMode.DontLoop);
-            channel.setSpeed(1f);
+        final AnimComposer ac;
+        private boolean loop = false;
+        private String nextAnim = null;
+
+        public LoopAction(Tween delegate, AnimComposer ac) {
+            super(delegate);
+            this.ac = ac;
         }
-    }
 
-    @Override
-    public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
-    }
+        public void setLoop(boolean loop) {
+            this.loop = loop;
+        }
 
-    @Override
-    public void onAction(String binding, boolean value, float tpf) {
-        if (binding.equals("Attack") && value){
-            if (!channel.getAnimationName().equals("Dodge")){
-                channel.setAnim("Dodge", 0.50f);
-                channel.setLoopMode(LoopMode.Cycle);
-                channel.setSpeed(0.10f);
+        public void setNextAnim(String nextAction) {
+            this.nextAnim = nextAction;
+        }
+
+        @Override
+        public boolean interpolate(double t) {
+            boolean running = super.interpolate(t);
+            if (!loop && !running) {
+                setSpeed(0);
+                ac.removeCurrentAction(AnimComposer.DEFAULT_LAYER);
+                if (nextAnim != null && ac.hasAction(nextAnim)) {
+                    currentAction = (LoopAction) ac.setCurrentAction(nextAnim);
+                }
             }
+            return running;
         }
-    }
 
+    }
 }