Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mygame;
- import com.cubes.Block;
- import com.cubes.BlockChunkControl;
- import com.cubes.BlockChunkListener;
- import com.cubes.BlockManager;
- import com.cubes.BlockNavigator;
- import com.cubes.BlockSkin;
- import com.cubes.BlockSkin_TextureLocation;
- import com.cubes.BlockTerrainControl;
- import com.cubes.CubesSettings;
- import com.cubes.Vector3Int;
- import com.cubes.test.CubesTestAssets;
- import com.jme3.app.SimpleApplication;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
- import com.jme3.bullet.collision.shapes.MeshCollisionShape;
- import com.jme3.bullet.control.CharacterControl;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.collision.CollisionResults;
- import com.jme3.input.KeyInput;
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.input.controls.MouseButtonTrigger;
- import com.jme3.light.AmbientLight;
- import com.jme3.light.DirectionalLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Ray;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.post.FilterPostProcessor;
- import com.jme3.post.filters.LightScatteringFilter;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.queue.RenderQueue;
- import com.jme3.renderer.queue.RenderQueue.ShadowMode;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.shadow.DirectionalLightShadowRenderer;
- /**
- * test
- * @author normenhansen
- */
- public class CubesTest extends SimpleApplication implements ActionListener{
- public static void main(String[] args) {
- CubesTest app = new CubesTest();
- app.start();
- }
- BlockTerrainControl blockTerrain;
- private BulletAppState bulletAppState;
- Node terrainNode;
- private CharacterControl playerControl;
- private Vector3f walkDirection = new Vector3f();
- private boolean[] arrowKeys = new boolean[4];
- public static final Block BlockGrass = new Block(new BlockSkin(new BlockSkin_TextureLocation(0, 0), false));
- public static final Block BlockDirt = new Block(new BlockSkin(new BlockSkin_TextureLocation(1, 0), false));
- public static final Block BlockBedrock = new Block(new BlockSkin(new BlockSkin_TextureLocation(2, 0), false));
- public static final Block BlockSand = new Block(new BlockSkin(new BlockSkin_TextureLocation(3, 0), false));
- @Override
- public void simpleInitApp() {
- bulletAppState = new BulletAppState();
- stateManager.attach(bulletAppState);
- CubesSettings settings = new CubesSettings(this);
- settings.setDefaultBlockMaterial("Textures/cubes/terrainBig.png");
- settings.setChunkSizeX(32);
- settings.setChunkSizeY(128);
- settings.setChunkSizeZ(32);
- /*Material material = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
- material.setTexture("DiffuseMap", assetManager.loadTexture("Textures/cubes/terrainBig.png"));
- material.setTexture("NormalMap", assetManager.loadTexture("Textures/cubes/n_terrainBig.png"));
- material.setColor("Diffuse",ColorRGBA.White);
- material.setColor("Specular",ColorRGBA.White);
- material.setFloat("Shininess", 64f);
- settings.setBlockMaterial(material);*/
- /*Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
- material.setTexture("DiffuseMap", assetManager.loadTexture("Textures/cubes/terrainBig.png"));
- material.setTexture("NormalMap", assetManager.loadTexture("Textures/cubes/n_terrainBig.png"));
- material.setBoolean("UseMaterialColors",false);
- material.setColor("Diffuse",ColorRGBA.White);
- material.setColor("Specular",ColorRGBA.White);
- material.setFloat("Shininess", 64f);
- settings.setBlockMaterial(material);*/
- BlockManager.register(BlockGrass);
- BlockManager.register(BlockDirt);
- BlockManager.register(BlockBedrock);
- BlockManager.register(BlockSand);
- blockTerrain = new BlockTerrainControl(settings, new Vector3Int(2, 1, 2));
- blockTerrain.addChunkListener(new BlockChunkListener(){
- @Override
- public void onSpatialUpdated(BlockChunkControl blockChunk){
- Geometry optimizedGeometry = blockChunk.getOptimizedGeometry_Opaque();
- RigidBodyControl rigidBodyControl = optimizedGeometry.getControl(RigidBodyControl.class);
- if(rigidBodyControl == null){
- rigidBodyControl = new RigidBodyControl(0);
- optimizedGeometry.addControl(rigidBodyControl);
- bulletAppState.getPhysicsSpace().add(rigidBodyControl);
- }
- rigidBodyControl.setCollisionShape(new MeshCollisionShape(optimizedGeometry.getMesh()));
- }
- });
- blockTerrain.setBlockArea(new Vector3Int(0, 0, 0), new Vector3Int(64, 1, 64), BlockBedrock);
- blockTerrain.setBlockArea(new Vector3Int(0, 1, 0), new Vector3Int(64, 3, 64), BlockDirt);
- blockTerrain.setBlockArea(new Vector3Int(0, 3, 0), new Vector3Int(64, 1, 64), BlockGrass);
- //blockTerrain.setBlock(new Vector3Int(0, 0, 0), BlockGrass);
- //blockTerrain.setBlock(new Vector3Int(1, 0, 0), BlockDirt);
- terrainNode = new Node();
- terrainNode.addControl(blockTerrain);
- rootNode.attachChild(terrainNode);
- cam.setLocation(new Vector3f(128, 10, 128));
- cam.lookAtDirection(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
- flyCam.setMoveSpeed(50);
- terrainNode.setShadowMode(ShadowMode.CastAndReceive);
- terrainNode.addControl(new RigidBodyControl(0));
- bulletAppState.getPhysicsSpace().addAll(terrainNode);
- /** A white ambient light source. */
- AmbientLight ambient = new AmbientLight();
- ambient.setColor(ColorRGBA.White);
- rootNode.addLight(ambient);
- /** Show scattered light beams when camera looks into "sun". */
- /*FilterPostProcessor fpp=new FilterPostProcessor(assetManager);
- LightScatteringFilter sunlight = new LightScatteringFilter(new Vector3f(.5f,.5f,.5f).multLocal(-3000));
- fpp.addFilter(sunlight);
- viewPort.addProcessor(fpp); */
- /** A white, directional light source */
- DirectionalLight sun = new DirectionalLight();
- sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
- sun.setColor(ColorRGBA.White);
- rootNode.addLight(sun);
- /* this shadow needs a directional light */
- DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 1024, 2);
- dlsr.setLight(sun);
- viewPort.addProcessor(dlsr);
- initControls();
- initPlayer();
- }
- private long lastModificationTimestamp = 0;
- private Vector3Int lastModificationLocation = new Vector3Int(0, 4, 15);
- @Override
- public void simpleUpdate(float tpf) {
- float playerMoveSpeed = ((blockTerrain.getSettings().getBlockSize() * 60.5f) * tpf);
- Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
- Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
- walkDirection.set(0, 0, 0);
- if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
- if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
- if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
- if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
- walkDirection.setY(0);
- playerControl.setWalkDirection(walkDirection);
- cam.setLocation(playerControl.getPhysicsLocation());
- if((System.currentTimeMillis() - lastModificationTimestamp) > 50){
- for(int x = 0; x < blockTerrain.getSettings().getChunkSizeX(); x++) {
- for(int y = 0; y < blockTerrain.getSettings().getChunkSizeY(); y++) {
- for(int z = 0; z < blockTerrain.getSettings().getChunkSizeZ(); z++) {
- if(blockTerrain.getBlock(x, y, z) != null &&
- blockTerrain.getBlock(x, y, z).equals(BlockSand) &&
- blockTerrain.getBlock(x, y - 1, z) == null) {
- blockTerrain.removeBlock(x, y, z);
- blockTerrain.setBlock(x, y - 1, z, BlockSand);
- }
- }
- }
- }
- /*blockTerrain.removeBlock(lastModificationLocation);
- lastModificationLocation.addLocal(1, 0, 0);
- if(lastModificationLocation.getX() > 31){
- lastModificationLocation.setX(0);
- }
- blockTerrain.setBlock(lastModificationLocation, CubesTestAssets.BLOCK_GRASS);*/
- lastModificationTimestamp = System.currentTimeMillis();
- }
- }
- private void initControls(){
- inputManager.addMapping("move_left", new KeyTrigger(KeyInput.KEY_A));
- inputManager.addMapping("move_right", new KeyTrigger(KeyInput.KEY_D));
- inputManager.addMapping("move_up", new KeyTrigger(KeyInput.KEY_W));
- inputManager.addMapping("move_down", new KeyTrigger(KeyInput.KEY_S));
- inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
- inputManager.addListener(this, "move_left");
- inputManager.addListener(this, "move_right");
- inputManager.addListener(this, "move_up");
- inputManager.addListener(this, "move_down");
- inputManager.addListener(this, "jump");
- inputManager.addMapping("set_block", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- inputManager.addListener(this, "set_block");
- inputManager.addMapping("remove_block", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
- inputManager.addListener(this, "remove_block");
- }
- private void initPlayer(){
- playerControl = new CharacterControl(new CapsuleCollisionShape((blockTerrain.getSettings().getBlockSize() / 2),
- blockTerrain.getSettings().getBlockSize() * 2), 0.05f);
- playerControl.setJumpSpeed(25);
- playerControl.setFallSpeed(200);
- playerControl.setGravity(90);
- playerControl.setPhysicsLocation(new Vector3f(5, blockTerrain.getSettings().getChunkSizeY() + 5, 5).mult(blockTerrain.getSettings().getBlockSize()));
- bulletAppState.getPhysicsSpace().add(playerControl);
- }
- @Override
- public void onAction(String actionName, boolean value, float lastTimePerFrame){
- if(actionName.equals("move_up")){
- arrowKeys[0] = value;
- }
- else if(actionName.equals("move_right")){
- arrowKeys[1] = value;
- }
- else if(actionName.equals("move_left")){
- arrowKeys[3] = value;
- }
- else if(actionName.equals("move_down")){
- arrowKeys[2] = value;
- }
- else if(actionName.equals("jump")){
- playerControl.jump();
- }
- if(actionName.equals("set_block") && value){
- Vector3Int blockLocation = getCurrentPointedBlockLocation(true);
- if(blockLocation != null){
- blockTerrain.setBlock(blockLocation, BlockGrass);
- }
- }
- else if(actionName.equals("remove_block") && value){
- Vector3Int blockLocation = getCurrentPointedBlockLocation(false);
- if((blockLocation != null) && (blockLocation.getY() > 0)){
- blockTerrain.removeBlock(blockLocation);
- }
- }
- }
- private Vector3Int getCurrentPointedBlockLocation(boolean getNeighborLocation){
- CollisionResults results = getRayCastingResults(terrainNode);
- if(results.size() > 0){
- Vector3f collisionContactPoint = results.getClosestCollision().getContactPoint();
- return BlockNavigator.getPointedBlockLocation(blockTerrain, collisionContactPoint, getNeighborLocation);
- }
- return null;
- }
- private CollisionResults getRayCastingResults(Node node){
- Vector3f origin = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.0f);
- Vector3f direction = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.3f);
- direction.subtractLocal(origin).normalizeLocal();
- Ray ray = new Ray(origin, direction);
- CollisionResults results = new CollisionResults();
- node.collideWith(ray, results);
- return results;
- }
- @Override
- public void simpleRender(RenderManager rm) {
- //TODO: add render code
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement