Guest User

Untitled

a guest
Sep 14th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.70 KB | None | 0 0
  1. package mygame;
  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.asset.TextureKey;
  9. import com.jme3.bullet.BulletAppState;
  10. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  11. import com.jme3.bullet.collision.shapes.CollisionShape;
  12. import com.jme3.bullet.control.CharacterControl;
  13. import com.jme3.bullet.control.RigidBodyControl;
  14. import com.jme3.bullet.util.CollisionShapeFactory;
  15. import com.jme3.collision.CollisionResult;
  16. import com.jme3.collision.CollisionResults;
  17. import com.jme3.font.BitmapText;
  18. import com.jme3.input.KeyInput;
  19. import com.jme3.input.MouseInput;
  20. import com.jme3.input.controls.ActionListener;
  21. import com.jme3.input.controls.KeyTrigger;
  22. import com.jme3.input.controls.MouseButtonTrigger;
  23. import com.jme3.light.AmbientLight;
  24. import com.jme3.light.DirectionalLight;
  25. import com.jme3.material.Material;
  26. import com.jme3.math.ColorRGBA;
  27. import com.jme3.math.Ray;
  28. import com.jme3.math.Vector3f;
  29. import com.jme3.niftygui.NiftyJmeDisplay;
  30. import com.jme3.scene.Geometry;
  31. import com.jme3.scene.Node;
  32. import com.jme3.scene.Spatial;
  33. import com.jme3.scene.shape.Sphere;
  34. import com.jme3.scene.shape.Sphere.TextureMode;
  35. import com.jme3.texture.Texture;
  36. import de.lessvoid.nifty.Nifty;
  37. import de.lessvoid.nifty.screen.Screen;
  38. import de.lessvoid.nifty.screen.ScreenController;
  39.  
  40. public class Main extends SimpleApplication implements ActionListener, ScreenController {
  41.    
  42.    
  43.   Material stone_mat;
  44.   private static Sphere sphere;
  45.   private Node model;
  46.   private Geometry mark;  
  47.   private Node shootables;
  48.   private Spatial sceneModel;
  49.   private RigidBodyControl PinShape;
  50.   private RigidBodyControl ball_phy;
  51.   private RigidBodyControl PinPhys;
  52.   private Spatial BowlingPin;
  53.   private BulletAppState bulletAppState;
  54.   private RigidBodyControl landscape;
  55.   private CharacterControl player;
  56.   private Vector3f walkDirection = new Vector3f();
  57.   private boolean left = false, right = false, up = false, down = false;
  58.   private AnimChannel channel;
  59.   private AnimControl control;
  60.  
  61.   //Temporary vectors used on each frame.
  62.   //They here to avoid instanciating new vectors on each frame
  63.   private Vector3f camDir = new Vector3f();
  64.   private Vector3f camLeft = new Vector3f();
  65.  
  66.   //Nifty Stuff
  67.   private Nifty nifty;
  68.   private Screen screen;
  69.   private SimpleApplication app;
  70.  
  71.  
  72.   public static void main(String[] args) {
  73.     Main app = new Main();
  74.     app.start();
  75.   }
  76.     static {
  77.     /** Initialize the cannon ball geometry */
  78.     sphere = new Sphere(32, 32, 0.4f, true, false);
  79.     sphere.setTextureMode(TextureMode.Projected);
  80.     }
  81.  
  82.   @Override
  83.   public void simpleInitApp() {
  84.     NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
  85.     assetManager, inputManager, audioRenderer, guiViewPort);
  86.     Nifty nifty = niftyDisplay.getNifty();
  87.     nifty.fromXml("Interface/Nifty/GameGUI.xml", "start", this);
  88.     guiViewPort.addProcessor(niftyDisplay);
  89.     nifty.loadControlFile("nifty-default-controls.xml");
  90.     flyCam.setDragToRotate(true);
  91.  
  92.  
  93.   }
  94.  
  95.   public void gameStart() {
  96.      
  97.     nifty.gotoScreen("");
  98.    
  99.    
  100.     /** Set up Physics */
  101.     bulletAppState = new BulletAppState();
  102.     stateManager.attach(bulletAppState);
  103.     //bulletAppState.getPhysicsSpace().enableDebug(assetManager);
  104.  
  105.     // We re-use the flyby camera for rotation, while positioning is handled by physics
  106.     flyCam.setDragToRotate(false);
  107.     viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
  108.     flyCam.setMoveSpeed(100);
  109.     setUpKeys();
  110.     setUpLight();
  111.     initCrossHairs();
  112.    
  113.    
  114.    
  115.     shootables = new Node("Shootables");
  116.         rootNode.attachChild(shootables);
  117.         shootables.attachChild(MakePin());
  118.  
  119.     // We load the scene from the zip file and adjust its size.
  120.     sceneModel = assetManager.loadModel("Scenes/Village/Village.j3o");
  121.     sceneModel.setLocalTranslation(5f, 0f, 1f);
  122.  
  123.     // We set up collision detection for the scene by creating a
  124.     // compound collision shape and a static RigidBodyControl with mass zero.
  125.     CollisionShape sceneShape =
  126.     CollisionShapeFactory.createMeshShape((Node) sceneModel);
  127.     landscape = new RigidBodyControl(sceneShape, 0);
  128.     sceneModel.addControl(landscape);
  129.  
  130.  
  131.     // We set up collision detection for the player by creating
  132.     // a capsule collision shape and a CharacterControl.
  133.     // The CharacterControl offers extra settings for
  134.     // size, stepheight, jumping, falling, and gravity.
  135.     // We also put the player in its starting position.
  136.     CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(.9f, 1.0f, 1);
  137.     player = new CharacterControl(capsuleShape, .05f);
  138.     model = (Node) assetManager.loadModel("Models/MeshMan/MeshMan.j3o");
  139.     model.setLocalScale(.5f);
  140.     model.addControl(player);
  141.     player.setUpAxis(1);
  142.     player.setMaxSlope(.1f);
  143.     player.setJumpSpeed(20);
  144.     player.setFallSpeed(30);
  145.     player.setGravity(60);
  146.     player.setPhysicsLocation(new Vector3f(0, 10, 0));
  147.     // We attach the scene and the player to the rootnode and the physics space,
  148.     // to make them appear in the game world.
  149.     rootNode.attachChild(sceneModel);
  150.     rootNode.attachChild(model);
  151.     bulletAppState.getPhysicsSpace().add(landscape);
  152.     bulletAppState.getPhysicsSpace().add(player);
  153.   }
  154.  
  155.   //These are the bowling Pin Physics
  156.       private Spatial MakePin(){
  157.     BowlingPin = assetManager.loadModel("Models/BowlingPin/BowlingPin.j3o");
  158.     BowlingPin.setLocalTranslation(15f, 0f, 15f);
  159.     BowlingPin.setLocalScale(.5f);
  160.     rootNode.attachChild(BowlingPin);
  161.     CollisionShape PinShape =
  162.     CollisionShapeFactory.createDynamicMeshShape(BowlingPin);
  163.     PinPhys = new RigidBodyControl(PinShape);
  164.     BowlingPin.addControl(PinPhys);
  165.     PinPhys.setMass(1f);
  166.     PinPhys.setKinematic(false);
  167.     bulletAppState.getPhysicsSpace().add(PinPhys);
  168.         return BowlingPin;
  169.     }
  170.  
  171.   private void setUpLight() {
  172.     // We add light so we see the scene
  173.     AmbientLight al = new AmbientLight();
  174.     al.setColor(ColorRGBA.White.mult(1.3f));
  175.     rootNode.addLight(al);
  176.  
  177.    
  178.     DirectionalLight dl = new DirectionalLight();
  179.     dl.setColor(ColorRGBA.White);
  180.     dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
  181.     rootNode.addLight(dl);
  182.   }
  183.  
  184.  
  185.   /** We over-write some navigational key mappings here, so we can
  186.    * add physics-controlled walking and jumping: */
  187.   private void setUpKeys() {
  188.     inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
  189.     inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
  190.     inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
  191.     inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
  192.     inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
  193.     inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  194.     inputManager.addListener(this, "Shoot");
  195.     inputManager.addListener(this, "Left");
  196.     inputManager.addListener(this, "Right");
  197.     inputManager.addListener(this, "Up");
  198.     inputManager.addListener(this, "Down");
  199.     inputManager.addListener(this, "Jump");
  200.   }
  201.  
  202.  
  203.  
  204.   /** These are our custom actions triggered by key presses.
  205.    * We do not walk yet, we just keep track of the direction the user pressed. */
  206.   public void onAction(String binding, boolean isPressed, float tpf) {
  207.     if (binding.equals("Left")) {
  208.       left = isPressed;
  209.      
  210.     } else if (binding.equals("Right")) {
  211.       right= isPressed;
  212.      
  213.     } else if (binding.equals("Up")) {
  214.       up = isPressed;
  215.      
  216.     } else if (binding.equals("Down")) {
  217.       down = isPressed;
  218.      
  219.     } else if (binding.equals("Jump")) {
  220.       if (isPressed) { player.jump(); }
  221.      
  222.     } else if (binding.equals("Shoot") && !isPressed) {
  223.  
  224.         makeCannonBall();
  225.         }
  226.    
  227.     }
  228.  
  229.      public void makeCannonBall() {
  230.     /** Create a cannon ball geometry and attach to scene graph. */
  231.     Geometry ball_geo = new Geometry("cannon ball", sphere);
  232.     stone_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  233.     TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
  234.     key2.setGenerateMips(true);
  235.     Texture tex2 = assetManager.loadTexture(key2);
  236.     stone_mat.setTexture("ColorMap", tex2);
  237.     ball_geo.setMaterial(stone_mat);
  238.     rootNode.attachChild(ball_geo);
  239.     /** Position the cannon ball  */
  240.     ball_geo.setLocalTranslation(cam.getLocation());
  241.     /** Make the ball physcial with a mass > 0.0f */
  242.     ball_phy = new RigidBodyControl(1f);
  243.     /** Add physical ball to physics space. */
  244.     ball_geo.addControl(ball_phy);
  245.     bulletAppState.getPhysicsSpace().add(ball_phy);
  246.     /** Accelerate the physcial ball to shoot it. */
  247.     ball_phy.setLinearVelocity(cam.getDirection().mult(25));
  248.   }
  249.  
  250.  
  251.  
  252.   /** A centred plus sign to help the player aim. */
  253.   protected void initCrossHairs() {
  254.     setDisplayStatView(false);
  255.     guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
  256.     BitmapText ch = new BitmapText(guiFont, false);
  257.     ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
  258.     ch.setText("+"); // crosshairs
  259.     ch.setLocalTranslation( // center
  260.       settings.getWidth() / 2 - ch.getLineWidth()/2, settings.getHeight() / 2 + ch.getLineHeight()/2, 0);
  261.     guiNode.attachChild(ch);
  262.   }
  263.  
  264.     public void backToStart(String start){
  265.       nifty.gotoScreen("start");
  266.     }
  267.    
  268.     public void bind(Nifty nifty, Screen screen) {
  269.     this.nifty = nifty;
  270.     this.screen = screen;
  271.   }
  272.  
  273.   public void onStartScreen() {
  274.  
  275.   }
  276.  
  277.   public void onEndScreen() {
  278.      
  279.   }
  280.     /**
  281.    * This is the main event loop--walking happens here.
  282.    * We check in which direction the player is walking by interpreting
  283.    * the camera direction forward (camDir) and to the side (camLeft).
  284.    * The setWalkDirection() command is what lets a physics-controlled player walk.
  285.    * We also make sure here that the camera moves with player.
  286.    */
  287.   @Override
  288.     public void simpleUpdate(float tpf) {
  289.         camDir.set(cam.getDirection()).multLocal(0.6f);
  290.         camLeft.set(cam.getLeft()).multLocal(0.4f);
  291.         walkDirection.set(0, 0, 0);
  292.         if (left) {
  293.             walkDirection.addLocal(camLeft);
  294.         }
  295.         if (right) {
  296.             walkDirection.addLocal(camLeft.negate());
  297.         }
  298.         if (up) {
  299.             walkDirection.addLocal(camDir);
  300.         }
  301.         if (down) {
  302.             walkDirection.addLocal(camDir.negate());
  303.         }
  304.        
  305.         //player.setWalkDirection(walkDirection);
  306.         //cam.setLocation(player.getPhysicsLocation());
  307.     }
  308.   }
Advertisement
Add Comment
Please, Sign In to add comment