Skip to content

Commit 1296eb2

Browse files
authored
convert TestAttachmentsNode to the new animation system (#1503)
* convert TestAttachmentsNode to the new animation system * TestAttachmentsNode: update copyright year * TestAttachmentsNode: combine nested "if" statements
1 parent 3677591 commit 1296eb2

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

jme3-examples/src/main/java/jme3test/model/anim/TestAttachmentsNode.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
*/
3232
package jme3test.model.anim;
3333

34-
import com.jme3.animation.AnimChannel;
35-
import com.jme3.animation.AnimControl;
36-
import com.jme3.animation.AnimEventListener;
37-
import com.jme3.animation.LoopMode;
38-
import com.jme3.animation.SkeletonControl;
34+
import com.jme3.anim.AnimComposer;
35+
import com.jme3.anim.SkinningControl;
36+
import com.jme3.anim.tween.Tween;
37+
import com.jme3.anim.tween.Tweens;
38+
import com.jme3.anim.tween.action.Action;
39+
import com.jme3.anim.util.AnimMigrationUtils;
3940
import com.jme3.app.SimpleApplication;
4041
import com.jme3.input.KeyInput;
4142
import com.jme3.input.controls.ActionListener;
@@ -56,15 +57,16 @@
5657
* Derived from {@link jme3test.model.anim.TestOgreAnim}.
5758
*/
5859
public class TestAttachmentsNode extends SimpleApplication
59-
implements AnimEventListener, ActionListener {
60+
implements ActionListener {
61+
62+
private Action punchesOnce;
63+
private AnimComposer control;
6064

6165
public static void main(String[] args) {
6266
TestAttachmentsNode app = new TestAttachmentsNode();
6367
app.start();
6468
}
6569

66-
private AnimChannel channel;
67-
6870
@Override
6971
public void simpleInitApp() {
7072
flyCam.setMoveSpeed(10f);
@@ -75,23 +77,40 @@ public void simpleInitApp() {
7577
dl.setDirection(new Vector3f(-0.1f, -0.7f, -1).normalizeLocal());
7678
dl.setColor(ColorRGBA.White);
7779
rootNode.addLight(dl);
78-
80+
/*
81+
* Load the Jaime model and convert it
82+
* from the old animation system to the new one.
83+
*/
7984
Spatial model = assetManager.loadModel("Models/Jaime/Jaime.j3o");
80-
AnimControl control = model.getControl(AnimControl.class);
81-
SkeletonControl skeletonControl = model.getControl(SkeletonControl.class);
85+
model = AnimMigrationUtils.migrate(model);
86+
/*
87+
* Play the "Idle" animation at half speed.
88+
*/
89+
control = model.getControl(AnimComposer.class);
90+
control.setCurrentAction("Idle");
91+
control.setGlobalSpeed(0.5f);
92+
/*
93+
* Define a "PunchesOnce" action sequence to play the "Punches"
94+
* animation for one cycle before returning to idle.
95+
*/
96+
Action punches = control.action("Punches");
97+
Tween doneTween
98+
= Tweens.callMethod(control, "setCurrentAction", "Idle");
99+
punchesOnce = control.actionSequence("PunchesOnce", punches, doneTween);
82100

83101
model.center();
84102
model.setLocalScale(5f);
85103

86-
control.addListener(this);
87-
channel = control.createChannel();
88-
channel.setAnim("Idle");
89-
90104
Box box = new Box(0.3f, 0.02f, 0.02f);
91105
Geometry saber = new Geometry("saber", box);
92106
saber.move(0.4f, 0.05f, 0.01f);
93107
Material red = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
94108
saber.setMaterial(red);
109+
/*
110+
* Create an attachments node for Jaime's right hand,
111+
* and attach the saber to that Node.
112+
*/
113+
SkinningControl skeletonControl = model.getControl(SkinningControl.class);
95114
Node n = skeletonControl.getAttachmentsNode("hand.R");
96115
n.attachChild(saber);
97116
rootNode.attachChild(model);
@@ -100,27 +119,11 @@ public void simpleInitApp() {
100119
inputManager.addMapping("Attack", new KeyTrigger(KeyInput.KEY_SPACE));
101120
}
102121

103-
@Override
104-
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
105-
if (animName.equals("Punches")) {
106-
channel.setAnim("Idle", 0.5f);
107-
channel.setLoopMode(LoopMode.DontLoop);
108-
channel.setSpeed(1f);
109-
}
110-
}
111-
112-
@Override
113-
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
114-
}
115-
116122
@Override
117123
public void onAction(String binding, boolean value, float tpf) {
118-
if (binding.equals("Attack") && value) {
119-
if (!channel.getAnimationName().equals("Punches")) {
120-
channel.setAnim("Punches", 0.5f);
121-
channel.setLoopMode(LoopMode.Cycle);
122-
channel.setSpeed(0.5f);
123-
}
124+
if (value && binding.equals("Attack")
125+
&& control.getCurrentAction() != punchesOnce) {
126+
control.setCurrentAction("PunchesOnce");
124127
}
125128
}
126129
}

0 commit comments

Comments
 (0)