Advertisement
Guest User

Untitled

a guest
Mar 29th, 2011
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.79 KB | None | 0 0
  1. package main;
  2.  
  3. import com.jme3.animation.AnimChannel;
  4. import com.jme3.animation.AnimControl;
  5. import com.jme3.animation.AnimEventListener;
  6. import com.jme3.animation.LoopMode;
  7. import com.jme3.app.SimpleApplication;
  8. import com.jme3.bullet.BulletAppState;
  9. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  10. import com.jme3.bullet.collision.shapes.CollisionShape;
  11. import com.jme3.bullet.control.CharacterControl;
  12. import com.jme3.bullet.control.RigidBodyControl;
  13. import com.jme3.bullet.util.CollisionShapeFactory;
  14. import com.jme3.input.ChaseCamera;
  15. import com.jme3.input.KeyInput;
  16. import com.jme3.input.MouseInput;
  17. import com.jme3.input.controls.ActionListener;
  18. import com.jme3.input.controls.KeyTrigger;
  19. import com.jme3.input.controls.MouseButtonTrigger;
  20. import com.jme3.light.AmbientLight;
  21. import com.jme3.light.DirectionalLight;
  22. import com.jme3.math.ColorRGBA;
  23. import com.jme3.math.Vector3f;
  24. import com.jme3.scene.Node;
  25. import com.jme3.scene.Spatial;
  26. import com.jme3.system.AppSettings;
  27. import org.lwjgl.input.Mouse;
  28.  
  29. /*
  30.  * JME Test 07: Collisions & Third Person Camera
  31.  * Detecting 3d collisions + animating model in 3rd person view
  32.  */
  33. public class Game extends SimpleApplication
  34.     implements AnimEventListener, ActionListener {
  35.     // We are implementing the ActionListener instead of creating an
  36.     // instance of it so that we can override default camera controls
  37.  
  38.     public static void main(String[] args) {
  39.         Game g = new Game();
  40.         AppSettings settings = new AppSettings(true);
  41.         settings.setTitle("JME Animation + Collision Test - by Franc[e]sco");
  42.         settings.setSettingsDialogImage("/Textures/neko_nagato.jpg");
  43.         g.setSettings(settings);
  44.         g.start();
  45.     }
  46.    
  47.     private AnimChannel ach;
  48.     private AnimControl act;
  49.     private Node player_model;
  50.     private Spatial scene_model;
  51.     private BulletAppState bulletAppState; // Nedeed to access collision functions
  52.     private RigidBodyControl landscape; // The scene is a rigid landscape
  53.     private CharacterControl player; // The player entity
  54.     private Node player_node; // Used to fix the model Y offset
  55.     private ChaseCamera chase_cam; // Third person camera
  56.     private Vector3f walk_direction = new Vector3f();
  57.     private float air_time = 0.0f;
  58.     private boolean left = false, right = false, up = false, down = false,
  59.     /* --------- */ attacking = false, capture_mouse = true, running = false,
  60.     /* --------- */ jumping = false, jump_pressed = false, attack_pressed = false,
  61.     /* --------- */ lock_movement = false;        
  62.  
  63.     @Override
  64.     public void simpleInitApp() {
  65.         init_models();
  66.         init_physics();
  67.         init_camera();
  68.         init_keys();
  69.         init_light();
  70.         init_anim();
  71.     }
  72.    
  73.     // Light ---------------------------------------------------------
  74.     private void init_light() {
  75.         AmbientLight al = new AmbientLight();
  76.         al.setColor(ColorRGBA.White.mult(1.3f)); // mult makes it brighter
  77.         rootNode.addLight(al);
  78.  
  79.         DirectionalLight sun = new DirectionalLight();
  80.         sun.setColor(ColorRGBA.White);
  81.         sun.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
  82.         rootNode.addLight(sun);        
  83.     }
  84.    
  85.     // Models --------------------------------------------------------
  86.     private void init_models() {
  87.         player_node = new Node("Player Node");
  88.         player_model = (Node)assetManager.loadModel("Models/Ninja/Ninja.mesh.j3o");
  89.         player_model.setLocalScale(0.035f);
  90.         player_model.getLocalTranslation().addLocal(0, -3.6f, 0); // model offset fix
  91.         player_node.attachChild(player_model);
  92.        
  93.         scene_model = assetManager.loadModel("Scenes/town/main.j3o");
  94.         scene_model.setLocalScale(2f);
  95.     }
  96.    
  97.     // Physics -------------------------------------------------------
  98.     private void init_physics() {
  99.        // Initialize physics engine
  100.         bulletAppState = new BulletAppState();
  101.         stateManager.attach(bulletAppState);
  102.        
  103.         // Our rigid scene
  104.         CollisionShape scene_shape =
  105.             CollisionShapeFactory.createMeshShape((Node)scene_model);
  106.         landscape = new RigidBodyControl(scene_shape, 0); // Static physics node with mass 0
  107.         scene_model.addControl(landscape);
  108.        
  109.         // Third parameter of CapsuleCollisionShape is the axis, 1 = Y
  110.         CapsuleCollisionShape capsule_shape =
  111.             new CapsuleCollisionShape(2.3f, 2.6f, 1);
  112.         player = new CharacterControl(capsule_shape, 0.05f);
  113.             // 0.05f is the highest step you can climb without jumping
  114.         player.setJumpSpeed(30);
  115.         player.setFallSpeed(60);
  116.         player.setGravity(70);
  117.         player_node.addControl(player);
  118.         player.setPhysicsLocation(new Vector3f(0, 10, 0));
  119.         player_node.setLocalTranslation(new Vector3f(0, 10, 0));
  120.        
  121.         rootNode.attachChild(player_node);
  122.         rootNode.attachChild(scene_model);
  123.        
  124.         // Physic nodes are attached to the physics space
  125.         // instead of the root node
  126.         bulletAppState.getPhysicsSpace().add(landscape);
  127.         bulletAppState.getPhysicsSpace().add(player);
  128.     }
  129.    
  130.     // Camera --------------------------------------------------------
  131.     void init_camera() {
  132.         viewPort.setBackgroundColor(new ColorRGBA(0.7f,0.8f,1f,1));
  133.         flyCam.setEnabled(false);
  134.         chase_cam = new ChaseCamera(cam, player_model, inputManager);
  135.         chase_cam.setDragToRotate(false);
  136.         chase_cam.setInvertVerticalAxis(true);
  137.         chase_cam.setLookAtOffset(new Vector3f(0, 2f, 0));
  138.     }
  139.    
  140.     // Controls ------------------------------------------------------
  141.     private void init_keys() {
  142.         inputManager.addMapping("Left",  new KeyTrigger(KeyInput.KEY_A));
  143.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
  144.         inputManager.addMapping("Up",    new KeyTrigger(KeyInput.KEY_W));
  145.         inputManager.addMapping("Down",  new KeyTrigger(KeyInput.KEY_S));
  146.         inputManager.addMapping("Jump",  new KeyTrigger(KeyInput.KEY_SPACE));
  147.         inputManager.addMapping("CatchM", new KeyTrigger(KeyInput.KEY_Q));
  148.         inputManager.addMapping("Run",    new KeyTrigger(KeyInput.KEY_LSHIFT));
  149.         inputManager.addMapping("Attack", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  150.         inputManager.addListener(this, "Left");
  151.         inputManager.addListener(this, "Right");
  152.         inputManager.addListener(this, "Up");
  153.         inputManager.addListener(this, "Down");
  154.         inputManager.addListener(this, "Jump");
  155.         inputManager.addListener(this, "CatchM");
  156.         inputManager.addListener(this, "Run");
  157.         inputManager.addListener(this, "Attack");
  158.     }
  159.    
  160.     public void onAction(String name, boolean pressed, float k) {
  161.         if (name.equals("Left"))
  162.             left = pressed;
  163.         else if (name.equals("Right"))
  164.             right = pressed;
  165.         else if (name.equals("Up"))
  166.             up = pressed;
  167.         else if (name.equals("Down"))
  168.             down = pressed;
  169.         else if (name.equals("Jump"))
  170.             jump_pressed = true;
  171.         else if (name.equals("CatchM") && !pressed) {
  172.             capture_mouse ^= true;
  173.             Mouse.setGrabbed(capture_mouse);
  174.             chase_cam.setDragToRotate(!capture_mouse);
  175.         }
  176.         else if (name.equals("Run"))
  177.             running = pressed;
  178.         else if (name.equals("Attack")){
  179.             if(capture_mouse && !jumping) {
  180.                 attack_pressed = pressed;
  181.                 if(pressed && !attacking)
  182.                     attacking = true;
  183.             }
  184.         }
  185.     }
  186.    
  187.     // Animations ----------------------------------------------------
  188.     private void init_anim() {
  189.         act = player_model.getControl(AnimControl.class);
  190.         act.addListener(this);
  191.         ach = act.createChannel();
  192.         ach.setAnim("Idle2");
  193.     }
  194.    
  195.     public void onAnimCycleDone(AnimControl ctrl, AnimChannel ch, String name) {
  196.         if(name.equals("Attack2") && attacking && !attack_pressed) {
  197.             if (!ch.getAnimationName().equals("Idle2")) {
  198.                 ch.setAnim("Idle2", 0f);
  199.                 ch.setLoopMode(LoopMode.Loop);
  200.                 ch.setSpeed(1f);
  201.                 attacking = false;
  202.                 lock_movement = false;
  203.             }
  204.         }
  205.         else if(name.equals("JumpNoHeight"))
  206.             jump_pressed = false;
  207.     }
  208.    
  209.     public void onAnimChange(AnimControl ctrl, AnimChannel ch, String name) {
  210.         // Blah, unused but I still need to implement it D:
  211.     }
  212.    
  213.     // Update Loop ---------------------------------------------------
  214.     @Override
  215.     public void simpleUpdate(float k) {
  216.         float movement_amount = 0.3f;
  217.         if(running) movement_amount *= 1.75;
  218.        
  219.         // Gets forward direction and moves it forward
  220.         Vector3f direction = cam.getDirection().clone().multLocal(movement_amount);
  221.         // Gets left direction and moves it to the left
  222.         Vector3f left_direction = cam.getLeft().clone().multLocal(movement_amount * 0.75f);
  223.        
  224.         // We don't want to fly or go underground
  225.         direction.y = 0;
  226.         left_direction.y = 0;
  227.        
  228.         walk_direction.set(0, 0, 0); // The walk direction is initially null
  229.  
  230.         if(left) walk_direction.addLocal(left_direction);
  231.         if(right) walk_direction.addLocal(left_direction.negate());
  232.         if(up) walk_direction.addLocal(direction);
  233.         if(down) walk_direction.addLocal(direction.negate());
  234.        
  235.         if(!player.onGround()) air_time += k;
  236.         else {
  237.             air_time = 0;
  238.             jumping = false;
  239.         }
  240.        
  241.         if ((air_time > 0.1f || jump_pressed) && !attacking) {
  242.             jumping = true;
  243.             // Stop movement if jumping while walking
  244.             if(jump_pressed && ach.getAnimationName().equals("Walk"))
  245.                 lock_movement = true;
  246.             if (!ach.getAnimationName().equals("JumpNoHeight")) {
  247.                 ach.setAnim("JumpNoHeight");
  248.                 ach.setSpeed(1f);
  249.                 ach.setLoopMode(LoopMode.DontLoop);
  250.             }
  251.             if(ach.getTime() >= 0.32f) { // Delay jump to make the animation look decent
  252.                 player.jump();
  253.                 lock_movement = false;
  254.             }
  255.         } else if(attacking) {
  256.             lock_movement = true;
  257.             if (!ach.getAnimationName().equals("Attack2")) {
  258.                 ach.setAnim("Attack2");
  259.                 ach.setSpeed(1f);
  260.                 ach.setLoopMode(LoopMode.Loop);
  261.             }
  262.         } else {
  263.             // If we're not walking, set standing animation if not jumping
  264.             if (walk_direction.length() == 0) {
  265.                  ach.setLoopMode(LoopMode.Loop);
  266.                  if (!ach.getAnimationName().equals("Idle2")) {
  267.                     ach.setAnim("Idle2", 0f);
  268.                     ach.setSpeed(1f);
  269.                  }
  270.             } else {
  271.                 // ... otherwise, set the walking animation
  272.                 ach.setLoopMode(LoopMode.Loop);
  273.                 if(!ach.getAnimationName().equals("Walk"))
  274.                     ach.setAnim("Walk", 0.5f);
  275.                 if(running) ach.setSpeed(1.75f);
  276.                 else ach.setSpeed(1f);
  277.             }
  278.         }
  279.         if(!lock_movement)
  280.             player.setWalkDirection(walk_direction);
  281.         else
  282.             player.setWalkDirection(Vector3f.ZERO);
  283.        
  284.         // Rotate model to point walk direction if moving
  285.         if(walk_direction.length() != 0)
  286.             player.setViewDirection(walk_direction.negate());
  287.             // negating cause the model is flipped
  288.        
  289.         // Rotate model to point camera direction if attacking
  290.         if(attacking)
  291.             player.setViewDirection(direction.negate());
  292.     }
  293.    
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement