Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mygame;
- import com.jme3.app.SimpleApplication;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
- import com.jme3.bullet.control.CharacterControl;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.light.DirectionalLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Mesh;
- import com.jme3.scene.Node;
- import com.jme3.scene.VertexBuffer;
- import com.jme3.texture.Texture;
- import com.jme3.util.BufferUtils;
- public class Main extends SimpleApplication implements ActionListener {
- /** Essentially a 1x1x1 Box */
- static class VoxelBlockMesh extends Mesh {
- private static final short[] GEOMETRY_INDICES_DATA = {
- 2, 1, 0, 3, 2, 0, // back
- 6, 5, 4, 7, 6, 4, // right
- 10, 9, 8, 11, 10, 8, // front
- 14, 13, 12, 15, 14, 12, // left
- 18, 17, 16, 19, 18, 16, // top
- 22, 21, 20, 23, 22, 20 // bottom
- };
- private static final float[] GEOMETRY_VERTICES_DATA = {
- 0, 0, -1, 1, 0, -1, 1, 1, -1, 0, 1, -1, // back
- 1, 0, -1, 1, 0, 0, 1, 1, 0, 1, 1, -1, // right
- 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, // front
- 0, 0, 0, 0, 0, -1, 0, 1, -1, 0, 1, 0, // left
- 1, 1, -1, 1, 1, 0, 0, 1, 0, 0, 1, -1, // top
- 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, -1 // bottom
- };
- private static final float[] GEOMETRY_NORMALS_DATA = {
- 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // back
- 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // right
- 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front
- -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // left
- 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // top
- 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 // bottom
- };
- private static final float[] GEOMETRY_TEXTURE_DATA = {
- 1, 0, 0, 0, 0, 1, 1, 1, // back
- 1, 0, 0, 0, 0, 1, 1, 1, // right
- 1, 0, 0, 0, 0, 1, 1, 1, // front
- 1, 0, 0, 0, 0, 1, 1, 1, // left
- 1, 0, 0, 0, 0, 1, 1, 1, // top
- 1, 0, 0, 0, 0, 1, 1, 1 // bottom
- };
- public VoxelBlockMesh() {
- }
- public VoxelBlockMesh init() {
- setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(GEOMETRY_VERTICES_DATA));
- setBuffer(VertexBuffer.Type.Normal, 3, BufferUtils.createFloatBuffer(GEOMETRY_NORMALS_DATA));
- setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(GEOMETRY_TEXTURE_DATA));
- setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createShortBuffer(GEOMETRY_INDICES_DATA));
- setStatic();
- updateBound();
- return this;
- }
- }
- public static final Main APP = new Main();
- public static final BulletAppState PHYSICS = new BulletAppState();
- public static void main(String[] args) {
- APP.start();
- }
- protected final Vector3f startPos = new Vector3f(8, 10, 8);
- protected CharacterControl player;
- private final Vector3f walkDirection = new Vector3f();
- private boolean left = false, right = false, up = false, down = false;
- private final Vector3f camDir = new Vector3f();
- private final Vector3f camLeft = new Vector3f();
- @Override
- public void simpleInitApp() {
- stateManager.attach(PHYSICS);
- cam.setLocation(startPos);
- rootNode.attachChild(generateChunk());
- rootNode.addLight(new DirectionalLight(new Vector3f(1, -1, -1).normalizeLocal(), ColorRGBA.White));
- this.getFlyByCamera().setRotationSpeed(1.8f);
- final CapsuleCollisionShape playerShape = new CapsuleCollisionShape(1.5f, 6f, 1);
- player = new CharacterControl(playerShape, 0.05f);
- player.setJumpSpeed(10);
- player.setFallSpeed(10);
- player.setGravity(10);
- player.setPhysicsLocation(startPos);
- PHYSICS.getPhysicsSpace().add(player);
- setUpKeys();
- }
- protected Material createMaterial() {
- final Texture grass = assetManager.loadTexture("Textures/Terrain/grass.png");
- grass.setAnisotropicFilter(16);
- grass.setMagFilter(Texture.MagFilter.Nearest);
- grass.setMinFilter(Texture.MinFilter.Trilinear);
- final Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
- mat.setTexture("DiffuseMap", grass);
- mat.setBoolean("UseMaterialColors", true);
- mat.setColor("Diffuse", ColorRGBA.White);
- mat.setColor("Specular", ColorRGBA.White);
- mat.setFloat("Shininess", 1f);
- return mat;
- }
- protected Geometry createBlock(final Mesh mesh, final Material mat, final Vector3f position) {
- final Geometry result = new Geometry("Block", mesh);
- result.setMaterial(mat);
- result.setLocalTranslation(position);
- final RigidBodyControl physics = new RigidBodyControl(0);
- physics.setSpatial(result);
- PHYSICS.getPhysicsSpace().add(physics);
- return result;
- }
- protected static final int CHUNK_SIZE = 16;
- protected Node generateChunk() {
- final Mesh blockMesh = new VoxelBlockMesh().init();
- final Material blockMaterial = createMaterial();
- final Vector3f childPosition = new Vector3f(0, -2, 0);
- final Node result = new Node();
- for (childPosition.x = 0; childPosition.x < CHUNK_SIZE; ++childPosition.x) {
- for (childPosition.z = 0; childPosition.z < CHUNK_SIZE; ++childPosition.z) {
- result.attachChild(createBlock(blockMesh, blockMaterial, childPosition));
- }
- }
- return result;
- }
- private void setUpKeys() {
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
- inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
- inputManager.addListener(this, "Left");
- inputManager.addListener(this, "Right");
- inputManager.addListener(this, "Up");
- inputManager.addListener(this, "Down");
- inputManager.addListener(this, "Jump");
- }
- @Override
- public void onAction(final String binding, boolean isPressed, float tpf) {
- switch (binding) {
- case "Left":
- left = isPressed;
- break;
- case "Right":
- right = isPressed;
- break;
- case "Up":
- up = isPressed;
- break;
- case "Down":
- down = isPressed;
- break;
- case "Jump":
- if (isPressed) {
- player.jump();
- }
- break;
- }
- }
- @Override
- public void simpleUpdate(float tpf) {
- camDir.set(cam.getDirection()).multLocal(0.2f);
- camLeft.set(cam.getLeft()).multLocal(0.1f);
- walkDirection.set(0, 0, 0);
- if (left) {
- walkDirection.addLocal(camLeft);
- }
- if (right) {
- walkDirection.addLocal(camLeft.negate());
- }
- if (up) {
- walkDirection.addLocal(camDir);
- }
- if (down) {
- walkDirection.addLocal(camDir.negate());
- }
- player.setWalkDirection(walkDirection);
- cam.setLocation(player.getPhysicsLocation());
- listener.setLocation(cam.getLocation());
- }
- @Override
- public void simpleRender(RenderManager rm) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement