Advertisement
TwoThe

JME TestCase

Apr 18th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.25 KB | None | 0 0
  1. package mygame;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.bullet.BulletAppState;
  5. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  6. import com.jme3.bullet.control.CharacterControl;
  7. import com.jme3.bullet.control.RigidBodyControl;
  8. import com.jme3.input.KeyInput;
  9. import com.jme3.input.controls.ActionListener;
  10. import com.jme3.input.controls.KeyTrigger;
  11. import com.jme3.light.DirectionalLight;
  12. import com.jme3.material.Material;
  13. import com.jme3.math.ColorRGBA;
  14. import com.jme3.math.Vector3f;
  15. import com.jme3.renderer.RenderManager;
  16. import com.jme3.scene.Geometry;
  17. import com.jme3.scene.Mesh;
  18. import com.jme3.scene.Node;
  19. import com.jme3.scene.VertexBuffer;
  20. import com.jme3.texture.Texture;
  21. import com.jme3.util.BufferUtils;
  22.  
  23. public class Main extends SimpleApplication implements ActionListener {
  24.  
  25.   /** Essentially a 1x1x1 Box */
  26.   static class VoxelBlockMesh extends Mesh {
  27.  
  28.     private static final short[] GEOMETRY_INDICES_DATA = {
  29.       2, 1, 0, 3, 2, 0, // back
  30.       6, 5, 4, 7, 6, 4, // right
  31.       10, 9, 8, 11, 10, 8, // front
  32.       14, 13, 12, 15, 14, 12, // left
  33.       18, 17, 16, 19, 18, 16, // top
  34.       22, 21, 20, 23, 22, 20 // bottom
  35.     };
  36.  
  37.     private static final float[] GEOMETRY_VERTICES_DATA = {
  38.       0, 0, -1, 1, 0, -1, 1, 1, -1, 0, 1, -1, // back
  39.       1, 0, -1, 1, 0, 0, 1, 1, 0, 1, 1, -1, // right
  40.       1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, // front
  41.       0, 0, 0, 0, 0, -1, 0, 1, -1, 0, 1, 0, // left
  42.       1, 1, -1, 1, 1, 0, 0, 1, 0, 0, 1, -1, // top
  43.       0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, -1 // bottom
  44.     };
  45.  
  46.     private static final float[] GEOMETRY_NORMALS_DATA = {
  47.       0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // back
  48.       1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // right
  49.       0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front
  50.       -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // left
  51.       0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // top
  52.       0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 // bottom
  53.     };
  54.  
  55.     private static final float[] GEOMETRY_TEXTURE_DATA = {
  56.       1, 0, 0, 0, 0, 1, 1, 1, // back
  57.       1, 0, 0, 0, 0, 1, 1, 1, // right
  58.       1, 0, 0, 0, 0, 1, 1, 1, // front
  59.       1, 0, 0, 0, 0, 1, 1, 1, // left
  60.       1, 0, 0, 0, 0, 1, 1, 1, // top
  61.       1, 0, 0, 0, 0, 1, 1, 1 // bottom
  62.     };
  63.  
  64.     public VoxelBlockMesh() {
  65.     }
  66.  
  67.     public VoxelBlockMesh init() {
  68.       setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(GEOMETRY_VERTICES_DATA));
  69.       setBuffer(VertexBuffer.Type.Normal, 3, BufferUtils.createFloatBuffer(GEOMETRY_NORMALS_DATA));
  70.       setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(GEOMETRY_TEXTURE_DATA));
  71.       setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createShortBuffer(GEOMETRY_INDICES_DATA));
  72.       setStatic();
  73.       updateBound();
  74.       return this;
  75.     }
  76.  
  77.   }
  78.  
  79.   public static final Main APP = new Main();
  80.   public static final BulletAppState PHYSICS = new BulletAppState();
  81.  
  82.   public static void main(String[] args) {
  83.     APP.start();
  84.   }
  85.  
  86.   protected final Vector3f startPos = new Vector3f(8, 10, 8);
  87.   protected CharacterControl player;
  88.   private final Vector3f walkDirection = new Vector3f();
  89.   private boolean left = false, right = false, up = false, down = false;
  90.   private final Vector3f camDir = new Vector3f();
  91.   private final Vector3f camLeft = new Vector3f();
  92.  
  93.   @Override
  94.   public void simpleInitApp() {
  95.     stateManager.attach(PHYSICS);
  96.     cam.setLocation(startPos);
  97.  
  98.     rootNode.attachChild(generateChunk());
  99.  
  100.     rootNode.addLight(new DirectionalLight(new Vector3f(1, -1, -1).normalizeLocal(), ColorRGBA.White));
  101.  
  102.     this.getFlyByCamera().setRotationSpeed(1.8f);
  103.  
  104.     final CapsuleCollisionShape playerShape = new CapsuleCollisionShape(1.5f, 6f, 1);
  105.     player = new CharacterControl(playerShape, 0.05f);
  106.     player.setJumpSpeed(10);
  107.     player.setFallSpeed(10);
  108.     player.setGravity(10);
  109.     player.setPhysicsLocation(startPos);
  110.     PHYSICS.getPhysicsSpace().add(player);
  111.  
  112.     setUpKeys();
  113.   }
  114.  
  115.   protected Material createMaterial() {
  116.     final Texture grass = assetManager.loadTexture("Textures/Terrain/grass.png");
  117.     grass.setAnisotropicFilter(16);
  118.     grass.setMagFilter(Texture.MagFilter.Nearest);
  119.     grass.setMinFilter(Texture.MinFilter.Trilinear);
  120.  
  121.     final Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  122.     mat.setTexture("DiffuseMap", grass);
  123.     mat.setBoolean("UseMaterialColors", true);
  124.     mat.setColor("Diffuse", ColorRGBA.White);
  125.     mat.setColor("Specular", ColorRGBA.White);
  126.     mat.setFloat("Shininess", 1f);
  127.     return mat;
  128.   }
  129.  
  130.   protected Geometry createBlock(final Mesh mesh, final Material mat, final Vector3f position) {
  131.     final Geometry result = new Geometry("Block", mesh);
  132.     result.setMaterial(mat);
  133.     result.setLocalTranslation(position);
  134.  
  135.     final RigidBodyControl physics = new RigidBodyControl(0);
  136.     physics.setSpatial(result);
  137.     PHYSICS.getPhysicsSpace().add(physics);
  138.     return result;
  139.   }
  140.  
  141.   protected static final int CHUNK_SIZE = 16;
  142.  
  143.   protected Node generateChunk() {
  144.     final Mesh blockMesh = new VoxelBlockMesh().init();
  145.     final Material blockMaterial = createMaterial();
  146.     final Vector3f childPosition = new Vector3f(0, -2, 0);
  147.     final Node result = new Node();
  148.    
  149.     for (childPosition.x = 0; childPosition.x < CHUNK_SIZE; ++childPosition.x) {
  150.       for (childPosition.z = 0; childPosition.z < CHUNK_SIZE; ++childPosition.z) {
  151.         result.attachChild(createBlock(blockMesh, blockMaterial, childPosition));
  152.       }
  153.     }
  154.     return result;
  155.   }
  156.  
  157.   private void setUpKeys() {
  158.     inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
  159.     inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
  160.     inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
  161.     inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
  162.     inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
  163.     inputManager.addListener(this, "Left");
  164.     inputManager.addListener(this, "Right");
  165.     inputManager.addListener(this, "Up");
  166.     inputManager.addListener(this, "Down");
  167.     inputManager.addListener(this, "Jump");
  168.   }
  169.  
  170.   @Override
  171.   public void onAction(final String binding, boolean isPressed, float tpf) {
  172.     switch (binding) {
  173.       case "Left":
  174.         left = isPressed;
  175.         break;
  176.       case "Right":
  177.         right = isPressed;
  178.         break;
  179.       case "Up":
  180.         up = isPressed;
  181.         break;
  182.       case "Down":
  183.         down = isPressed;
  184.         break;
  185.       case "Jump":
  186.         if (isPressed) {
  187.           player.jump();
  188.         }
  189.         break;
  190.     }
  191.   }
  192.  
  193.   @Override
  194.   public void simpleUpdate(float tpf) {
  195.     camDir.set(cam.getDirection()).multLocal(0.2f);
  196.     camLeft.set(cam.getLeft()).multLocal(0.1f);
  197.     walkDirection.set(0, 0, 0);
  198.     if (left) {
  199.       walkDirection.addLocal(camLeft);
  200.     }
  201.     if (right) {
  202.       walkDirection.addLocal(camLeft.negate());
  203.     }
  204.     if (up) {
  205.       walkDirection.addLocal(camDir);
  206.     }
  207.     if (down) {
  208.       walkDirection.addLocal(camDir.negate());
  209.     }
  210.     player.setWalkDirection(walkDirection);
  211.  
  212.     cam.setLocation(player.getPhysicsLocation());
  213.     listener.setLocation(cam.getLocation());
  214.   }
  215.  
  216.   @Override
  217.   public void simpleRender(RenderManager rm) {
  218.   }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement