Guest User

Untitled

a guest
May 11th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.17 KB | None | 0 0
  1. package com.ractoc.games.asteroid;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.input.KeyInput;
  5. import com.jme3.input.controls.ActionListener;
  6. import com.jme3.input.controls.AnalogListener;
  7. import com.jme3.input.controls.KeyTrigger;
  8. import com.jme3.material.Material;
  9. import com.jme3.math.ColorRGBA;
  10. import com.jme3.math.Matrix3f;
  11. import com.jme3.math.Vector3f;
  12. import com.jme3.renderer.RenderManager;
  13. import com.jme3.scene.Geometry;
  14. import com.jme3.scene.Node;
  15. import com.jme3.scene.shape.Box;
  16. import com.jme3.scene.shape.Sphere;
  17.  
  18. /**
  19.  * test
  20.  * @author normenhansen
  21.  */
  22. public class Main extends SimpleApplication {
  23.  
  24.     private static final String INPUT_ROTATE_LEFT = "ROTATE_LEFT";
  25.     private static final String INPUT_ROTATE_RIGHT = "ROTATE_RIGHT";
  26.     private static final String INPUT_THRUST_FORWARD = "THRUST_FORWARD";
  27.     private static final String INPUT_THRUST_BACKWARD = "THRUST_BACKWARD";
  28.     private Node ship = null;
  29.     private AppActionListener actionListener = new AppActionListener();
  30.  
  31.     public static void main(String[] args) {
  32.         Main app = new Main();
  33.         app.start();
  34.     }
  35.  
  36.     @Override
  37.     public void simpleInitApp() {
  38.         rootNode.attachChild(createShip());
  39.         this.getFlyByCamera().setEnabled(false);
  40.  
  41.         inputManager.addMapping(INPUT_ROTATE_LEFT, new KeyTrigger(KeyInput.KEY_LEFT));
  42.         inputManager.addMapping(INPUT_ROTATE_RIGHT, new KeyTrigger(KeyInput.KEY_RIGHT));
  43.         inputManager.addMapping(INPUT_THRUST_FORWARD, new KeyTrigger(KeyInput.KEY_UP));
  44.         inputManager.addMapping(INPUT_THRUST_BACKWARD, new KeyTrigger(KeyInput.KEY_DOWN));
  45.         inputManager.addListener(actionListener, INPUT_ROTATE_LEFT,
  46.                 INPUT_ROTATE_RIGHT, INPUT_THRUST_FORWARD, INPUT_THRUST_BACKWARD);
  47.     }
  48.  
  49.     @Override
  50.     public void simpleUpdate(float tpf) {
  51.         //TODO: add update code
  52.     }
  53.  
  54.     @Override
  55.     public void simpleRender(RenderManager rm) {
  56.         //TODO: add render code
  57.     }
  58.  
  59.     private Node createShip() {
  60.         Box n = new Box(new Vector3f(0, 0.75f, 0), 0.25f, 0.5f, 0.25f);
  61.         Geometry nose = new Geometry("Nose", n);
  62.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  63.         mat.setColor("Color", ColorRGBA.Blue);
  64.         nose.setMaterial(mat);
  65.  
  66.         Sphere b = new Sphere(30, 30, 1f, true, false);
  67.         Geometry body = new Geometry("Body", b);
  68.         body.setMaterial(mat);
  69.         body.setLocalTranslation(0, -0.5f, 0);
  70.         ship = new Node("Ship");
  71.         ship.attachChild(nose);
  72.         ship.attachChild(body);
  73.         ship.setLocalTranslation(0, 0, -20);
  74.  
  75.         return ship;
  76.     }
  77.  
  78.     private class AppActionListener implements AnalogListener {
  79.  
  80.         public void onAnalog(String name, float value, float tpf) {
  81.             if (name.equals(INPUT_ROTATE_LEFT)) {
  82.                 ship.rotate(0, 0, tpf*speed);
  83.             } else if (name.equals(INPUT_ROTATE_RIGHT)) {
  84.                 ship.rotate(0, 0, -tpf*speed);
  85.             } else if (name.equals(INPUT_THRUST_FORWARD)) {
  86.                 System.out.println("thrust forward");
  87.             } else if (name.equals(INPUT_THRUST_BACKWARD)) {
  88.                 System.out.println("thrust backward");
  89.             }
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment