Java examples for Game:JME3
Physics Game 3 in jme3
package Physics;/*w w w. j av a 2 s. c om*/ import com.jme3.animation.AnimChannel; import com.jme3.animation.AnimControl; import com.jme3.animation.AnimEventListener; import com.jme3.app.SimpleApplication; import com.jme3.asset.plugins.ZipLocator; import com.jme3.bullet.BulletAppState; import com.jme3.bullet.control.BetterCharacterControl; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.input.ChaseCamera; import com.jme3.input.KeyInput; import com.jme3.input.controls.ActionListener; import com.jme3.input.controls.InputListener; import com.jme3.input.controls.KeyTrigger; import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.light.AmbientLight; import com.jme3.light.DirectionalLight; import com.jme3.material.MaterialList; import com.jme3.math.Vector3f; import com.jme3.scene.Node; import com.jme3.scene.plugins.ogre.OgreMeshKey; /** * test * * @author normenhansen */ public class PhysicsGame3 extends SimpleApplication implements InputListener, AnimEventListener, ActionListener { private BulletAppState bulletAppState; private BetterCharacterControl character; private AnimControl animationControl; private AnimChannel animationChannel; private AnimChannel attackChannel; private ChaseCamera chaseCam; private Node model; private boolean left = false, both = false, right = false, up = false, down = false, jump = false; private Vector3f walkDirection = new Vector3f(0, 0, 0); private float airTime = 0; private int speed = 40; private int toggleTimer; private boolean run; public static void main(String[] args) { PhysicsGame3 app = new PhysicsGame3(); app.start(); } public void simpleInitApp() { initPhysics(); initScene(); initLights(); initPlayer(); initCamera(); initAnimation(); initControlMappings(); } @Override public void onAction(String binding, boolean value, float tpf) { if (binding.equals("CharLeft")) { if (value) { left = true; } else { left = false; } } else if (binding.equals("CharRight")) { if (value) { right = true; } else { right = false; } } else if (binding.equals("CharForward")) { if (value) { up = true; } else { up = false; } } else if (binding.equals("CharForward2")) { if (value) { up = true; } else { up = false; } } else if (binding.equals("CharBackward")) { if (value) { down = true; } else { down = false; } } else if (binding.equals("CharRun")) { if (value) { run = true; } else { run = false; } } else if (binding.equals("CharJump")) { if (!"JumpStart".equals(animationChannel.getAnimationName()) && character.isOnGround()) { animationChannel.setAnim("JumpStart", 1f); } character.jump(); } if (binding.equals("CharAttack")) { attack(); } } private void attack() { // attackChannel.setAnim("Dodge", 0.1f); // attackChannel.setLoopMode(LoopMode.DontLoop); } @Override public void simpleUpdate(float tpf) { Vector3f camDir = cam.getDirection().clone().multLocal(0.25f); Vector3f camLeft = cam.getLeft().clone().multLocal(0.25f); camDir.y = 0; camLeft.y = 0; walkDirection.set(0, 0, 0); if (left && character.isOnGround()) { walkDirection.addLocal(camLeft.mult(speed)); } if (right && character.isOnGround()) { walkDirection.addLocal(camLeft.mult(speed).negate()); } if (up) { if (run) { walkDirection.addLocal(camDir.mult(speed * 2)); } else { walkDirection.addLocal(camDir.mult(speed)); } } if (down) { if (run) { walkDirection.addLocal(camDir.mult(speed * 2).negate()); } else { walkDirection.addLocal(camDir.mult(speed).negate()); } } if (!character.isOnGround()) { airTime = airTime + tpf; if (!"Jumping".equals(animationChannel.getAnimationName()) && airTime > 0.2f) { animationChannel.setAnim("Jumping", 1.0f); } } else { airTime = 0; } if (walkDirection.length() == 0) { if (!"Idle".equals(animationChannel.getAnimationName())) { animationChannel.setAnim("Idle", 0.2f); } } else { character.setViewDirection(walkDirection); if (airTime > .3f) { if (!"Idle".equals(animationChannel.getAnimationName())) { animationChannel.setAnim("Idle", 0.2f); } } else if (!"Walk".equals(animationChannel.getAnimationName())) { animationChannel.setAnim("Walk", 0.7f); } } character.setWalkDirection(walkDirection); // THIS IS WHERE THE WALKING HAPPENS } public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) { // if (channel == attackChannel) { // channel.setAnim("stand"); // } } public void onAnimChange(AnimControl control, AnimChannel channel, String animName) { // if (!"JumpStart".equals(animationChannel.getAnimationName() && )) { // animationChannel.setAnim("JumpStart", 0.7f); // } } private void initPhysics() { bulletAppState = new BulletAppState(); //bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL); stateManager.attach(bulletAppState); //bulletAppState.setDebugEnabled(true); // collision shapes visible } private void initScene() { assetManager.registerLocator("quake3level.zip", ZipLocator.class); MaterialList matList = (MaterialList) assetManager .loadAsset("Scene.material"); OgreMeshKey key = new OgreMeshKey("main.meshxml", matList); Node gameLevel = (Node) assetManager.loadAsset(key); gameLevel.setLocalTranslation(-20, -16, 20); gameLevel.setLocalScale(0.10f); gameLevel.addControl(new RigidBodyControl(0)); rootNode.attachChild(gameLevel); bulletAppState.getPhysicsSpace().addAll(gameLevel); } private void initLights() { AmbientLight ambient = new AmbientLight(); rootNode.addLight(ambient); DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1.4f, -1.4f, -1.4f)); rootNode.addLight(sun); } private void initPlayer() { character = new BetterCharacterControl(1.0f, 10, 50); character.setJumpForce(new Vector3f(0, 300, 0)); character.setGravity(new Vector3f(0, 9.8f, 0)); character.setApplyPhysicsLocal(false); //model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml"); model = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o"); model.scale(6f); model.addControl(character); model.setName("player"); bulletAppState.getPhysicsSpace().add(character); rootNode.attachChild(model); } private void initCamera() { flyCam.setEnabled(false); chaseCam = new ChaseCamera(cam, model, inputManager); } private void initAnimation() { animationControl = model.getControl(AnimControl.class); animationControl.addListener(this); animationChannel = animationControl.createChannel(); //attackChannel = animationControl.createChannel(); for (String a : animationControl.getAnimationNames()) { System.out.println(a); } // attackChannel.addBone(animationControl.getSkeleton().getBone("uparm.right")); // attackChannel.addBone(animationControl.getSkeleton().getBone("arm.right")); // attackChannel.addBone(animationControl.getSkeleton().getBone("hand.right")); } private void initControlMappings() { inputManager.addMapping("CharLeft", new KeyTrigger(KeyInput.KEY_A)); inputManager .addMapping("CharRight", new KeyTrigger(KeyInput.KEY_D)); inputManager.addMapping("CharForward", new KeyTrigger( KeyInput.KEY_W)); inputManager.addMapping("CharForward2", new MouseButtonTrigger( mouseInput.BUTTON_LEFT)); inputManager.addMapping("CharBackward", new KeyTrigger( KeyInput.KEY_S)); inputManager.addMapping("CharJump", new KeyTrigger( KeyInput.KEY_SPACE)); inputManager.addMapping("CharAttack", new KeyTrigger( KeyInput.KEY_RETURN)); inputManager.addMapping("CharRun", new KeyTrigger(KeyInput.KEY_F)); inputManager.addListener(this, "CharLeft", "CharRight"); inputManager.addListener(this, "CharForward", "CharBackward", "CharForward2"); inputManager.addListener(this, "CharJump", "CharAttack"); inputManager.addListener(this, "CharRun"); } }