1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package jme3test.model.anim; 34 35 import com.jme3.animation.*; 36 import com.jme3.app.SimpleApplication; 37 import com.jme3.input.KeyInput; 38 import com.jme3.input.controls.ActionListener; 39 import com.jme3.input.controls.KeyTrigger; 40 import com.jme3.light.DirectionalLight; 41 import com.jme3.math.ColorRGBA; 42 import com.jme3.math.Quaternion; 43 import com.jme3.math.Vector3f; 44 import com.jme3.scene.Geometry; 45 import com.jme3.scene.Node; 46 import com.jme3.scene.Spatial; 47 import com.jme3.scene.shape.Box; 48 49 public class TestOgreAnim extends SimpleApplication 50 implements AnimEventListener, ActionListener { 51 52 private AnimChannel channel; 53 private AnimControl control; 54 main(String[] args)55 public static void main(String[] args) { 56 TestOgreAnim app = new TestOgreAnim(); 57 app.start(); 58 } 59 60 @Override simpleInitApp()61 public void simpleInitApp() { 62 flyCam.setMoveSpeed(10f); 63 cam.setLocation(new Vector3f(6.4013605f, 7.488437f, 12.843031f)); 64 cam.setRotation(new Quaternion(-0.060740203f, 0.93925786f, -0.2398315f, -0.2378785f)); 65 66 DirectionalLight dl = new DirectionalLight(); 67 dl.setDirection(new Vector3f(-0.1f, -0.7f, -1).normalizeLocal()); 68 dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f)); 69 rootNode.addLight(dl); 70 71 Spatial model = (Spatial) assetManager.loadModel("Models/Oto/Oto.mesh.xml"); 72 model.center(); 73 74 control = model.getControl(AnimControl.class); 75 control.addListener(this); 76 channel = control.createChannel(); 77 78 for (String anim : control.getAnimationNames()) 79 System.out.println(anim); 80 81 channel.setAnim("stand"); 82 83 SkeletonControl skeletonControl = model.getControl(SkeletonControl.class); 84 85 Box b = new Box(.25f,3f,.25f); 86 Geometry item = new Geometry("Item", b); 87 item.move(0, 1.5f, 0); 88 item.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m")); 89 Node n = skeletonControl.getAttachmentsNode("hand.right"); 90 n.attachChild(item); 91 92 rootNode.attachChild(model); 93 94 inputManager.addListener(this, "Attack"); 95 inputManager.addMapping("Attack", new KeyTrigger(KeyInput.KEY_SPACE)); 96 } 97 onAnimCycleDone(AnimControl control, AnimChannel channel, String animName)98 public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) { 99 if (animName.equals("Dodge")){ 100 channel.setAnim("stand", 0.50f); 101 channel.setLoopMode(LoopMode.DontLoop); 102 channel.setSpeed(1f); 103 } 104 } 105 onAnimChange(AnimControl control, AnimChannel channel, String animName)106 public void onAnimChange(AnimControl control, AnimChannel channel, String animName) { 107 } 108 onAction(String binding, boolean value, float tpf)109 public void onAction(String binding, boolean value, float tpf) { 110 if (binding.equals("Attack") && value){ 111 if (!channel.getAnimationName().equals("Dodge")){ 112 channel.setAnim("Dodge", 0.50f); 113 channel.setLoopMode(LoopMode.Cycle); 114 channel.setSpeed(0.10f); 115 } 116 } 117 } 118 119 } 120