Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- 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.bullet.BulletAppState;
- import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
- import com.jme3.bullet.collision.shapes.CollisionShape;
- import com.jme3.bullet.control.CharacterControl;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.bullet.util.CollisionShapeFactory;
- import com.jme3.input.ChaseCamera;
- import com.jme3.input.KeyInput;
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.ActionListener;
- 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.math.ColorRGBA;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.system.AppSettings;
- import org.lwjgl.input.Mouse;
- /*
- * JME Test 07: Collisions & Third Person Camera
- * Detecting 3d collisions + animating model in 3rd person view
- */
- public class Game extends SimpleApplication
- implements AnimEventListener, ActionListener {
- // We are implementing the ActionListener instead of creating an
- // instance of it so that we can override default camera controls
- public static void main(String[] args) {
- Game g = new Game();
- AppSettings settings = new AppSettings(true);
- settings.setTitle("JME Animation + Collision Test - by Franc[e]sco");
- settings.setSettingsDialogImage("/Textures/neko_nagato.jpg");
- g.setSettings(settings);
- g.start();
- }
- private AnimChannel ach;
- private AnimControl act;
- private Node player_model;
- private Spatial scene_model;
- private BulletAppState bulletAppState; // Nedeed to access collision functions
- private RigidBodyControl landscape; // The scene is a rigid landscape
- private CharacterControl player; // The player entity
- private Node player_node; // Used to fix the model Y offset
- private ChaseCamera chase_cam; // Third person camera
- private Vector3f walk_direction = new Vector3f();
- private float air_time = 0.0f;
- private boolean left = false, right = false, up = false, down = false,
- /* --------- */ attacking = false, capture_mouse = true, running = false,
- /* --------- */ jumping = false, jump_pressed = false, attack_pressed = false,
- /* --------- */ lock_movement = false;
- @Override
- public void simpleInitApp() {
- init_models();
- init_physics();
- init_camera();
- init_keys();
- init_light();
- init_anim();
- }
- // Light ---------------------------------------------------------
- private void init_light() {
- AmbientLight al = new AmbientLight();
- al.setColor(ColorRGBA.White.mult(1.3f)); // mult makes it brighter
- rootNode.addLight(al);
- DirectionalLight sun = new DirectionalLight();
- sun.setColor(ColorRGBA.White);
- sun.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
- rootNode.addLight(sun);
- }
- // Models --------------------------------------------------------
- private void init_models() {
- player_node = new Node("Player Node");
- player_model = (Node)assetManager.loadModel("Models/Ninja/Ninja.mesh.j3o");
- player_model.setLocalScale(0.035f);
- player_model.getLocalTranslation().addLocal(0, -3.6f, 0); // model offset fix
- player_node.attachChild(player_model);
- scene_model = assetManager.loadModel("Scenes/town/main.j3o");
- scene_model.setLocalScale(2f);
- }
- // Physics -------------------------------------------------------
- private void init_physics() {
- // Initialize physics engine
- bulletAppState = new BulletAppState();
- stateManager.attach(bulletAppState);
- // Our rigid scene
- CollisionShape scene_shape =
- CollisionShapeFactory.createMeshShape((Node)scene_model);
- landscape = new RigidBodyControl(scene_shape, 0); // Static physics node with mass 0
- scene_model.addControl(landscape);
- // Third parameter of CapsuleCollisionShape is the axis, 1 = Y
- CapsuleCollisionShape capsule_shape =
- new CapsuleCollisionShape(2.3f, 2.6f, 1);
- player = new CharacterControl(capsule_shape, 0.05f);
- // 0.05f is the highest step you can climb without jumping
- player.setJumpSpeed(30);
- player.setFallSpeed(60);
- player.setGravity(70);
- player_node.addControl(player);
- player.setPhysicsLocation(new Vector3f(0, 10, 0));
- player_node.setLocalTranslation(new Vector3f(0, 10, 0));
- rootNode.attachChild(player_node);
- rootNode.attachChild(scene_model);
- // Physic nodes are attached to the physics space
- // instead of the root node
- bulletAppState.getPhysicsSpace().add(landscape);
- bulletAppState.getPhysicsSpace().add(player);
- }
- // Camera --------------------------------------------------------
- void init_camera() {
- viewPort.setBackgroundColor(new ColorRGBA(0.7f,0.8f,1f,1));
- flyCam.setEnabled(false);
- chase_cam = new ChaseCamera(cam, player_model, inputManager);
- chase_cam.setDragToRotate(false);
- chase_cam.setInvertVerticalAxis(true);
- chase_cam.setLookAtOffset(new Vector3f(0, 2f, 0));
- }
- // Controls ------------------------------------------------------
- private void init_keys() {
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
- inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
- inputManager.addMapping("CatchM", new KeyTrigger(KeyInput.KEY_Q));
- inputManager.addMapping("Run", new KeyTrigger(KeyInput.KEY_LSHIFT));
- inputManager.addMapping("Attack", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- inputManager.addListener(this, "Left");
- inputManager.addListener(this, "Right");
- inputManager.addListener(this, "Up");
- inputManager.addListener(this, "Down");
- inputManager.addListener(this, "Jump");
- inputManager.addListener(this, "CatchM");
- inputManager.addListener(this, "Run");
- inputManager.addListener(this, "Attack");
- }
- public void onAction(String name, boolean pressed, float k) {
- if (name.equals("Left"))
- left = pressed;
- else if (name.equals("Right"))
- right = pressed;
- else if (name.equals("Up"))
- up = pressed;
- else if (name.equals("Down"))
- down = pressed;
- else if (name.equals("Jump"))
- jump_pressed = true;
- else if (name.equals("CatchM") && !pressed) {
- capture_mouse ^= true;
- Mouse.setGrabbed(capture_mouse);
- chase_cam.setDragToRotate(!capture_mouse);
- }
- else if (name.equals("Run"))
- running = pressed;
- else if (name.equals("Attack")){
- if(capture_mouse && !jumping) {
- attack_pressed = pressed;
- if(pressed && !attacking)
- attacking = true;
- }
- }
- }
- // Animations ----------------------------------------------------
- private void init_anim() {
- act = player_model.getControl(AnimControl.class);
- act.addListener(this);
- ach = act.createChannel();
- ach.setAnim("Idle2");
- }
- public void onAnimCycleDone(AnimControl ctrl, AnimChannel ch, String name) {
- if(name.equals("Attack2") && attacking && !attack_pressed) {
- if (!ch.getAnimationName().equals("Idle2")) {
- ch.setAnim("Idle2", 0f);
- ch.setLoopMode(LoopMode.Loop);
- ch.setSpeed(1f);
- attacking = false;
- lock_movement = false;
- }
- }
- else if(name.equals("JumpNoHeight"))
- jump_pressed = false;
- }
- public void onAnimChange(AnimControl ctrl, AnimChannel ch, String name) {
- // Blah, unused but I still need to implement it D:
- }
- // Update Loop ---------------------------------------------------
- @Override
- public void simpleUpdate(float k) {
- float movement_amount = 0.3f;
- if(running) movement_amount *= 1.75;
- // Gets forward direction and moves it forward
- Vector3f direction = cam.getDirection().clone().multLocal(movement_amount);
- // Gets left direction and moves it to the left
- Vector3f left_direction = cam.getLeft().clone().multLocal(movement_amount * 0.75f);
- // We don't want to fly or go underground
- direction.y = 0;
- left_direction.y = 0;
- walk_direction.set(0, 0, 0); // The walk direction is initially null
- if(left) walk_direction.addLocal(left_direction);
- if(right) walk_direction.addLocal(left_direction.negate());
- if(up) walk_direction.addLocal(direction);
- if(down) walk_direction.addLocal(direction.negate());
- if(!player.onGround()) air_time += k;
- else {
- air_time = 0;
- jumping = false;
- }
- if ((air_time > 0.1f || jump_pressed) && !attacking) {
- jumping = true;
- // Stop movement if jumping while walking
- if(jump_pressed && ach.getAnimationName().equals("Walk"))
- lock_movement = true;
- if (!ach.getAnimationName().equals("JumpNoHeight")) {
- ach.setAnim("JumpNoHeight");
- ach.setSpeed(1f);
- ach.setLoopMode(LoopMode.DontLoop);
- }
- if(ach.getTime() >= 0.32f) { // Delay jump to make the animation look decent
- player.jump();
- lock_movement = false;
- }
- } else if(attacking) {
- lock_movement = true;
- if (!ach.getAnimationName().equals("Attack2")) {
- ach.setAnim("Attack2");
- ach.setSpeed(1f);
- ach.setLoopMode(LoopMode.Loop);
- }
- } else {
- // If we're not walking, set standing animation if not jumping
- if (walk_direction.length() == 0) {
- ach.setLoopMode(LoopMode.Loop);
- if (!ach.getAnimationName().equals("Idle2")) {
- ach.setAnim("Idle2", 0f);
- ach.setSpeed(1f);
- }
- } else {
- // ... otherwise, set the walking animation
- ach.setLoopMode(LoopMode.Loop);
- if(!ach.getAnimationName().equals("Walk"))
- ach.setAnim("Walk", 0.5f);
- if(running) ach.setSpeed(1.75f);
- else ach.setSpeed(1f);
- }
- }
- if(!lock_movement)
- player.setWalkDirection(walk_direction);
- else
- player.setWalkDirection(Vector3f.ZERO);
- // Rotate model to point walk direction if moving
- if(walk_direction.length() != 0)
- player.setViewDirection(walk_direction.negate());
- // negating cause the model is flipped
- // Rotate model to point camera direction if attacking
- if(attacking)
- player.setViewDirection(direction.negate());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement