Advertisement
Guest User

Untitled

a guest
May 10th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.87 KB | None | 0 0
  1. package mygame;
  2.  
  3. import com.cubes.Block;
  4. import com.cubes.BlockChunkControl;
  5. import com.cubes.BlockChunkListener;
  6. import com.cubes.BlockManager;
  7. import com.cubes.BlockNavigator;
  8. import com.cubes.BlockSkin;
  9. import com.cubes.BlockSkin_TextureLocation;
  10. import com.cubes.BlockTerrainControl;
  11. import com.cubes.CubesSettings;
  12. import com.cubes.Vector3Int;
  13. import com.cubes.test.CubesTestAssets;
  14. import com.jme3.app.SimpleApplication;
  15. import com.jme3.bullet.BulletAppState;
  16. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  17. import com.jme3.bullet.collision.shapes.MeshCollisionShape;
  18. import com.jme3.bullet.control.CharacterControl;
  19. import com.jme3.bullet.control.RigidBodyControl;
  20. import com.jme3.collision.CollisionResults;
  21. import com.jme3.input.KeyInput;
  22. import com.jme3.input.MouseInput;
  23. import com.jme3.input.controls.ActionListener;
  24. import com.jme3.input.controls.KeyTrigger;
  25. import com.jme3.input.controls.MouseButtonTrigger;
  26. import com.jme3.light.AmbientLight;
  27. import com.jme3.light.DirectionalLight;
  28. import com.jme3.material.Material;
  29. import com.jme3.math.ColorRGBA;
  30. import com.jme3.math.Ray;
  31. import com.jme3.math.Vector2f;
  32. import com.jme3.math.Vector3f;
  33. import com.jme3.post.FilterPostProcessor;
  34. import com.jme3.post.filters.LightScatteringFilter;
  35. import com.jme3.renderer.RenderManager;
  36. import com.jme3.renderer.queue.RenderQueue;
  37. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  38. import com.jme3.scene.Geometry;
  39. import com.jme3.scene.Node;
  40. import com.jme3.shadow.DirectionalLightShadowRenderer;
  41.  
  42. /**
  43.  * test
  44.  * @author normenhansen
  45.  */
  46. public class CubesTest extends SimpleApplication implements ActionListener{
  47.  
  48.     public static void main(String[] args) {
  49.         CubesTest app = new CubesTest();
  50.         app.start();
  51.     }
  52.    
  53.     BlockTerrainControl blockTerrain;
  54.     private BulletAppState bulletAppState;
  55.     Node terrainNode;
  56.     private CharacterControl playerControl;
  57.     private Vector3f walkDirection = new Vector3f();
  58.     private boolean[] arrowKeys = new boolean[4];
  59.    
  60.     public static final Block BlockGrass = new Block(new BlockSkin(new BlockSkin_TextureLocation(0, 0), false));
  61.     public static final Block BlockDirt = new Block(new BlockSkin(new BlockSkin_TextureLocation(1, 0), false));
  62.     public static final Block BlockBedrock = new Block(new BlockSkin(new BlockSkin_TextureLocation(2, 0), false));
  63.     public static final Block BlockSand = new Block(new BlockSkin(new BlockSkin_TextureLocation(3, 0), false));
  64.  
  65.     @Override
  66.     public void simpleInitApp() {
  67.         bulletAppState = new BulletAppState();
  68.         stateManager.attach(bulletAppState);
  69.    
  70.        
  71.        
  72.         CubesSettings settings = new CubesSettings(this);
  73.         settings.setDefaultBlockMaterial("Textures/cubes/terrainBig.png");
  74.         settings.setChunkSizeX(32);
  75.         settings.setChunkSizeY(128);
  76.         settings.setChunkSizeZ(32);
  77.        
  78.        
  79.         /*Material material = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
  80.         material.setTexture("DiffuseMap", assetManager.loadTexture("Textures/cubes/terrainBig.png"));
  81.         material.setTexture("NormalMap", assetManager.loadTexture("Textures/cubes/n_terrainBig.png"));
  82.         material.setColor("Diffuse",ColorRGBA.White);
  83.         material.setColor("Specular",ColorRGBA.White);
  84.         material.setFloat("Shininess", 64f);
  85.         settings.setBlockMaterial(material);*/
  86.        
  87.         /*Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  88.         material.setTexture("DiffuseMap", assetManager.loadTexture("Textures/cubes/terrainBig.png"));
  89.         material.setTexture("NormalMap", assetManager.loadTexture("Textures/cubes/n_terrainBig.png"));
  90.         material.setBoolean("UseMaterialColors",false);    
  91.         material.setColor("Diffuse",ColorRGBA.White);
  92.         material.setColor("Specular",ColorRGBA.White);
  93.         material.setFloat("Shininess", 64f);
  94.         settings.setBlockMaterial(material);*/
  95.        
  96.         BlockManager.register(BlockGrass);
  97.         BlockManager.register(BlockDirt);
  98.         BlockManager.register(BlockBedrock);
  99.         BlockManager.register(BlockSand);
  100.  
  101.         blockTerrain = new BlockTerrainControl(settings, new Vector3Int(2, 1, 2));
  102.         blockTerrain.addChunkListener(new BlockChunkListener(){
  103.  
  104.             @Override
  105.             public void onSpatialUpdated(BlockChunkControl blockChunk){
  106.                 Geometry optimizedGeometry = blockChunk.getOptimizedGeometry_Opaque();
  107.                 RigidBodyControl rigidBodyControl = optimizedGeometry.getControl(RigidBodyControl.class);
  108.                 if(rigidBodyControl == null){
  109.                     rigidBodyControl = new RigidBodyControl(0);
  110.                     optimizedGeometry.addControl(rigidBodyControl);
  111.                     bulletAppState.getPhysicsSpace().add(rigidBodyControl);
  112.                 }
  113.                 rigidBodyControl.setCollisionShape(new MeshCollisionShape(optimizedGeometry.getMesh()));
  114.             }
  115.         });
  116.        
  117.         blockTerrain.setBlockArea(new Vector3Int(0, 0, 0), new Vector3Int(64, 1, 64), BlockBedrock);
  118.         blockTerrain.setBlockArea(new Vector3Int(0, 1, 0), new Vector3Int(64, 3, 64), BlockDirt);
  119.         blockTerrain.setBlockArea(new Vector3Int(0, 3, 0), new Vector3Int(64, 1, 64), BlockGrass);
  120.         //blockTerrain.setBlock(new Vector3Int(0, 0, 0), BlockGrass);
  121.         //blockTerrain.setBlock(new Vector3Int(1, 0, 0), BlockDirt);
  122.  
  123.         terrainNode = new Node();
  124.         terrainNode.addControl(blockTerrain);
  125.         rootNode.attachChild(terrainNode);
  126.        
  127.         cam.setLocation(new Vector3f(128, 10, 128));
  128.         cam.lookAtDirection(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
  129.         flyCam.setMoveSpeed(50);
  130.        
  131.         terrainNode.setShadowMode(ShadowMode.CastAndReceive);
  132.  
  133.         terrainNode.addControl(new RigidBodyControl(0));
  134.         bulletAppState.getPhysicsSpace().addAll(terrainNode);
  135.        
  136.             /** A white ambient light source. */
  137.         AmbientLight ambient = new AmbientLight();
  138.         ambient.setColor(ColorRGBA.White);
  139.         rootNode.addLight(ambient);
  140.             /** Show scattered light beams when camera looks into "sun". */
  141.         /*FilterPostProcessor fpp=new FilterPostProcessor(assetManager);
  142.         LightScatteringFilter sunlight = new LightScatteringFilter(new Vector3f(.5f,.5f,.5f).multLocal(-3000));
  143.         fpp.addFilter(sunlight);
  144.         viewPort.addProcessor(fpp); */
  145.             /** A white, directional light source */
  146.         DirectionalLight sun = new DirectionalLight();
  147.         sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
  148.         sun.setColor(ColorRGBA.White);
  149.         rootNode.addLight(sun);
  150.             /* this shadow needs a directional light */
  151.         DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 1024, 2);
  152.         dlsr.setLight(sun);
  153.         viewPort.addProcessor(dlsr);
  154.        
  155.         initControls();
  156.         initPlayer();
  157.     }
  158.  
  159.     private long lastModificationTimestamp = 0;
  160.     private Vector3Int lastModificationLocation = new Vector3Int(0, 4, 15);
  161.    
  162.     @Override
  163.     public void simpleUpdate(float tpf) {
  164.         float playerMoveSpeed = ((blockTerrain.getSettings().getBlockSize() * 60.5f) * tpf);
  165.         Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
  166.         Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
  167.         walkDirection.set(0, 0, 0);
  168.         if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
  169.         if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
  170.         if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
  171.         if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
  172.         walkDirection.setY(0);
  173.         playerControl.setWalkDirection(walkDirection);
  174.         cam.setLocation(playerControl.getPhysicsLocation());
  175.        
  176.        
  177.         if((System.currentTimeMillis() - lastModificationTimestamp) > 50){
  178.             for(int x = 0; x < blockTerrain.getSettings().getChunkSizeX(); x++) {
  179.                 for(int y = 0; y < blockTerrain.getSettings().getChunkSizeY(); y++) {
  180.                     for(int z = 0; z < blockTerrain.getSettings().getChunkSizeZ(); z++) {
  181.                         if(blockTerrain.getBlock(x, y, z) != null &&
  182.                             blockTerrain.getBlock(x, y, z).equals(BlockSand) &&
  183.                            blockTerrain.getBlock(x, y - 1, z) == null) {
  184.                             blockTerrain.removeBlock(x, y, z);
  185.                             blockTerrain.setBlock(x, y - 1, z, BlockSand);
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.             /*blockTerrain.removeBlock(lastModificationLocation);
  191.             lastModificationLocation.addLocal(1, 0, 0);
  192.             if(lastModificationLocation.getX() > 31){
  193.                 lastModificationLocation.setX(0);
  194.             }
  195.             blockTerrain.setBlock(lastModificationLocation, CubesTestAssets.BLOCK_GRASS);*/
  196.             lastModificationTimestamp = System.currentTimeMillis();
  197.         }
  198.     }
  199.  
  200.     private void initControls(){
  201.         inputManager.addMapping("move_left", new KeyTrigger(KeyInput.KEY_A));
  202.         inputManager.addMapping("move_right", new KeyTrigger(KeyInput.KEY_D));
  203.         inputManager.addMapping("move_up", new KeyTrigger(KeyInput.KEY_W));
  204.         inputManager.addMapping("move_down", new KeyTrigger(KeyInput.KEY_S));
  205.         inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
  206.         inputManager.addListener(this, "move_left");
  207.         inputManager.addListener(this, "move_right");
  208.         inputManager.addListener(this, "move_up");
  209.         inputManager.addListener(this, "move_down");
  210.         inputManager.addListener(this, "jump");
  211.        
  212.         inputManager.addMapping("set_block", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  213.         inputManager.addListener(this, "set_block");
  214.         inputManager.addMapping("remove_block", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
  215.         inputManager.addListener(this, "remove_block");
  216.     }
  217.  
  218.     private void initPlayer(){
  219.         playerControl = new CharacterControl(new CapsuleCollisionShape((blockTerrain.getSettings().getBlockSize() / 2),
  220.                                                                         blockTerrain.getSettings().getBlockSize() * 2), 0.05f);
  221.         playerControl.setJumpSpeed(25);
  222.         playerControl.setFallSpeed(200);
  223.         playerControl.setGravity(90);
  224.         playerControl.setPhysicsLocation(new Vector3f(5, blockTerrain.getSettings().getChunkSizeY() + 5, 5).mult(blockTerrain.getSettings().getBlockSize()));
  225.         bulletAppState.getPhysicsSpace().add(playerControl);
  226.     }
  227.    
  228.     @Override
  229.     public void onAction(String actionName, boolean value, float lastTimePerFrame){
  230.         if(actionName.equals("move_up")){
  231.             arrowKeys[0] = value;
  232.         }
  233.         else if(actionName.equals("move_right")){
  234.             arrowKeys[1] = value;
  235.         }
  236.         else if(actionName.equals("move_left")){
  237.             arrowKeys[3] = value;
  238.         }
  239.         else if(actionName.equals("move_down")){
  240.             arrowKeys[2] = value;
  241.         }
  242.         else if(actionName.equals("jump")){
  243.             playerControl.jump();
  244.         }
  245.        
  246.         if(actionName.equals("set_block") && value){
  247.             Vector3Int blockLocation = getCurrentPointedBlockLocation(true);
  248.             if(blockLocation != null){
  249.                 blockTerrain.setBlock(blockLocation, BlockGrass);
  250.             }
  251.         }
  252.         else if(actionName.equals("remove_block") && value){
  253.             Vector3Int blockLocation = getCurrentPointedBlockLocation(false);
  254.             if((blockLocation != null) && (blockLocation.getY() > 0)){
  255.                 blockTerrain.removeBlock(blockLocation);
  256.             }
  257.         }
  258.     }
  259.    
  260.     private Vector3Int getCurrentPointedBlockLocation(boolean getNeighborLocation){
  261.         CollisionResults results = getRayCastingResults(terrainNode);
  262.         if(results.size() > 0){
  263.             Vector3f collisionContactPoint = results.getClosestCollision().getContactPoint();
  264.             return BlockNavigator.getPointedBlockLocation(blockTerrain, collisionContactPoint, getNeighborLocation);
  265.         }
  266.         return null;
  267.     }
  268.    
  269.     private CollisionResults getRayCastingResults(Node node){
  270.         Vector3f origin = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.0f);
  271.         Vector3f direction = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.3f);
  272.         direction.subtractLocal(origin).normalizeLocal();
  273.         Ray ray = new Ray(origin, direction);
  274.         CollisionResults results = new CollisionResults();
  275.         node.collideWith(ray, results);
  276.         return results;
  277.     }
  278.    
  279.     @Override
  280.     public void simpleRender(RenderManager rm) {
  281.         //TODO: add render code
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement