Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ractoc.games.asteroid;
- import com.jme3.app.SimpleApplication;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Matrix3f;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.shape.Box;
- import com.jme3.scene.shape.Sphere;
- /**
- * test
- * @author normenhansen
- */
- public class Main extends SimpleApplication {
- private static final String INPUT_ROTATE_LEFT = "ROTATE_LEFT";
- private static final String INPUT_ROTATE_RIGHT = "ROTATE_RIGHT";
- private static final String INPUT_THRUST_FORWARD = "THRUST_FORWARD";
- private static final String INPUT_THRUST_BACKWARD = "THRUST_BACKWARD";
- private Node ship = null;
- private AppActionListener actionListener = new AppActionListener();
- public static void main(String[] args) {
- Main app = new Main();
- app.start();
- }
- @Override
- public void simpleInitApp() {
- rootNode.attachChild(createShip());
- this.getFlyByCamera().setEnabled(false);
- inputManager.addMapping(INPUT_ROTATE_LEFT, new KeyTrigger(KeyInput.KEY_LEFT));
- inputManager.addMapping(INPUT_ROTATE_RIGHT, new KeyTrigger(KeyInput.KEY_RIGHT));
- inputManager.addMapping(INPUT_THRUST_FORWARD, new KeyTrigger(KeyInput.KEY_UP));
- inputManager.addMapping(INPUT_THRUST_BACKWARD, new KeyTrigger(KeyInput.KEY_DOWN));
- inputManager.addListener(actionListener, INPUT_ROTATE_LEFT,
- INPUT_ROTATE_RIGHT, INPUT_THRUST_FORWARD, INPUT_THRUST_BACKWARD);
- }
- @Override
- public void simpleUpdate(float tpf) {
- //TODO: add update code
- }
- @Override
- public void simpleRender(RenderManager rm) {
- //TODO: add render code
- }
- private Node createShip() {
- Box n = new Box(new Vector3f(0, 0.75f, 0), 0.25f, 0.5f, 0.25f);
- Geometry nose = new Geometry("Nose", n);
- Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- mat.setColor("Color", ColorRGBA.Blue);
- nose.setMaterial(mat);
- Sphere b = new Sphere(30, 30, 1f, true, false);
- Geometry body = new Geometry("Body", b);
- body.setMaterial(mat);
- body.setLocalTranslation(0, -0.5f, 0);
- ship = new Node("Ship");
- ship.attachChild(nose);
- ship.attachChild(body);
- ship.setLocalTranslation(0, 0, -20);
- return ship;
- }
- private class AppActionListener implements AnalogListener {
- public void onAnalog(String name, float value, float tpf) {
- if (name.equals(INPUT_ROTATE_LEFT)) {
- ship.rotate(0, 0, tpf*speed);
- } else if (name.equals(INPUT_ROTATE_RIGHT)) {
- ship.rotate(0, 0, -tpf*speed);
- } else if (name.equals(INPUT_THRUST_FORWARD)) {
- System.out.println("thrust forward");
- } else if (name.equals(INPUT_THRUST_BACKWARD)) {
- System.out.println("thrust backward");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment