Skip to content

Instantly share code, notes, and snippets.

@cessationoftime
Created April 7, 2012 20:37
Show Gist options
  • Save cessationoftime/2331988 to your computer and use it in GitHub Desktop.
Save cessationoftime/2331988 to your computer and use it in GitHub Desktop.
JME3 and Scala
package com.random
import com.jme3.animation.AnimChannel
import com.jme3.animation.AnimControl
import com.jme3.animation.AnimEventListener
import com.jme3.animation.LoopMode
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.ColorRGBA
import com.jme3.math.Vector3f
import com.jme3.scene.Node
import scala.collection.JavaConversions
import com.jme3.scene.debug.SkeletonDebugger
import com.jme3.material.Material
import com.jme3.input.controls.MouseButtonTrigger
import com.jme3.input.MouseInput
/**
* Sample 7 - how to load an OgreXML model and play an animation,
* using channels, a controller, and an AnimEventListener.
*/
object AnimationDemo extends SimpleApplication with App with JmeScala with AnimEventListener {
import JavaConversions._
this.start;
lazy val player = {
val p = assetManager.loadModel("Models/Oto/Oto.mesh.xml").asInstanceOf[Node];
p.setLocalScale(0.5f);
p
}
lazy val control = {
val c = player.getControl(classOf[AnimControl]);
c.addListener(this);
c
}
lazy val channel = {
val ch = control.createChannel();
ch.setAnim("stand");
ch
}
def simpleInitApp = {
viewPort.setBackgroundColor(ColorRGBA.LightGray);
initKeys;
val dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
rootNode.addLight(dl);
rootNode.attachChild(player);
for (anim <- control.getAnimationNames()) { System.out.println(anim); }
val skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
val mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Green);
mat.getAdditionalRenderState().setDepthTest(false);
skeletonDebug.setMaterial(mat);
player.attachChild(skeletonDebug);
}
def onAnimCycleDone(control: AnimControl, channel: AnimChannel, animName: String) = {
animName match {
case "Stand" =>
case _ =>
println(animName + " Done")
channel.setAnim("stand", 0.50f);
channel.setLoopMode(LoopMode.DontLoop);
channel.setSpeed(1f);
}
}
def onAnimChange(control: AnimControl, channel: AnimChannel, animName: String) = {
// unused
}
/** Custom Keybinding: Map named actions to inputs. */
def initKeys {
import KeyInput._, MouseInput._
inputManager.addMapping("Walk", new KeyTrigger(KEY_SPACE));
inputManager.addMapping("pull", new MouseButtonTrigger(BUTTON_LEFT));
inputManager.addMapping("push", new MouseButtonTrigger(BUTTON_RIGHT));
inputManager.addMapping("Dodge", new KeyTrigger(KEY_R));
inputManager.addListener(actionListener, "Walk", "pull", "push", "Dodge");
}
val actionListener = InputListener.action { (name, keyPressed, tpf) =>
(name, keyPressed) match {
case ("Walk", false) =>
if (channel.getAnimationName != "Walk") {
channel.setAnim("Walk", 0.50f);
channel.setLoopMode(LoopMode.Loop);
}
case ("pull", false) =>
if (channel.getAnimationName != "pull") {
channel.setAnim("pull", 0.50f);
channel.setLoopMode(LoopMode.DontLoop);
}
case ("push", false) =>
if (channel.getAnimationName != "push") {
channel.setAnim("push", 0.50f);
channel.setLoopMode(LoopMode.DontLoop);
//channel.setSpeed(0.5f)
}
case ("Dodge", false) =>
if (channel.getAnimationName != "Dodge") {
channel.setAnim("Dodge", 0.30f);
channel.setLoopMode(LoopMode.DontLoop);
channel.setSpeed(0.4f)
}
case _ =>
}
}
}
package com.random
import com.jme3.input.controls.AnalogListener
import com.jme3.input.controls.ActionListener
trait JmeScala {
object InputListener {
def action(action: (String, Boolean, Float) => Unit) = {
new ActionListener() {
def onAction(name: String, keyPressed: Boolean, tpf: Float) = {
action(name, keyPressed, tpf)
}
}
}
def analog(action: (String, Float, Float) => Unit) = {
new AnalogListener() {
def onAnalog(name: String, value: Float, tpf: Float) = {
action(name, value, tpf)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment