Advertisement
Guest User

Untitled

a guest
Nov 1st, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package net.gyroninja.Cavein;
  6.  
  7. import com.jme3.app.Application;
  8. import com.jme3.app.SimpleApplication;
  9. import com.jme3.app.state.AppStateManager;
  10. import com.jme3.asset.AssetManager;
  11. import com.jme3.bullet.BulletAppState;
  12. import com.jme3.bullet.collision.shapes.SphereCollisionShape;
  13. import com.jme3.bullet.control.RigidBodyControl;
  14. import com.jme3.bullet.objects.PhysicsCharacter;
  15. import com.jme3.bullet.util.CollisionShapeFactory;
  16. import com.jme3.input.InputManager;
  17. import com.jme3.input.KeyInput;
  18. import com.jme3.input.controls.ActionListener;
  19. import com.jme3.input.controls.KeyTrigger;
  20. import com.jme3.light.DirectionalLight;
  21. import com.jme3.material.Material;
  22. import com.jme3.math.Vector3f;
  23. import com.jme3.renderer.Camera;
  24. import com.jme3.renderer.RenderManager;
  25. import com.jme3.renderer.ViewPort;
  26. import com.jme3.scene.Node;
  27. import com.jme3.scene.Spatial;
  28.  
  29. /**
  30. *
  31. * @author gyroninja
  32. */
  33. public class CaveGameState extends BulletAppState implements ActionListener{
  34.  
  35. boolean left = false, right = false, up = false, down = false;
  36.  
  37. public SimpleApplication app;
  38. Node rootNode;
  39. AssetManager assetManager;
  40. AppStateManager stateManager;
  41. InputManager inputManager;
  42. ViewPort viewPort;
  43. BulletAppState bullet;
  44.  
  45. Spatial Level;
  46. Material mat;
  47. DirectionalLight sun;
  48. Camera cam;
  49.  
  50. PhysicsCharacter player;
  51. Vector3f walkDirection = new Vector3f();
  52.  
  53. @Override
  54. public void initialize(AppStateManager stateManager, Application app) {
  55.  
  56. super.initialize(stateManager, app);
  57.  
  58. this.app = (SimpleApplication) app;
  59.  
  60. this.rootNode = this.app.getRootNode();
  61. this.assetManager = this.app.getAssetManager();
  62. this.stateManager = this.app.getStateManager();
  63. this.inputManager = this.app.getInputManager();
  64. this.viewPort = this.app.getViewPort();
  65. this.bullet = this.stateManager.getState(BulletAppState.class);
  66. this.cam = app.getCamera();
  67.  
  68. this.setUpKeys();
  69.  
  70. Level = assetManager.loadModel("Models/cave/cave.j3o");
  71. mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  72. mat.setTexture("ColorMap", assetManager.loadTexture("Models/cave/stone.png"));
  73. Level.setMaterial(mat);
  74.  
  75. sun = new DirectionalLight();
  76. sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
  77.  
  78. rootNode.attachChild(Level);
  79.  
  80. rootNode.addLight(sun);
  81.  
  82. player = new PhysicsCharacter(new SphereCollisionShape(5), .01f);
  83. player.setJumpSpeed(20);
  84. player.setFallSpeed(30);
  85. player.setGravity(30);
  86.  
  87. player.setPhysicsLocation(new Vector3f(10, 10, 10));
  88.  
  89. RigidBodyControl landscape = new RigidBodyControl(CollisionShapeFactory.createMeshShape(Level), 0);
  90. Level.addControl(landscape);
  91.  
  92. // this.getPhysicsSpace().add(Level);
  93. this.getPhysicsSpace().add(player);
  94.  
  95. this.getPhysicsSpace().enableDebug(assetManager);
  96. }
  97.  
  98. @Override
  99. public void cleanup() {
  100.  
  101.  
  102. }
  103.  
  104. @Override
  105. public void setEnabled(boolean enabled) {
  106.  
  107.  
  108. }
  109.  
  110. @Override
  111. public void update(float tpf) {
  112.  
  113. Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
  114. Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
  115.  
  116. walkDirection.set(0, 0, 0);
  117.  
  118. if (left) {
  119.  
  120. walkDirection.addLocal(camLeft);
  121. }
  122.  
  123. if (right) {
  124.  
  125. walkDirection.addLocal(camLeft.negate());
  126. }
  127.  
  128. if (up) {
  129.  
  130. walkDirection.addLocal(camDir);
  131. }
  132.  
  133. if (down) {
  134.  
  135. walkDirection.addLocal(camDir.negate());
  136. }
  137.  
  138. player.setWalkDirection(walkDirection);
  139. System.out.println(player.getWalkDirection());
  140. cam.setLocation(player.getPhysicsLocation());
  141. System.out.println(cam.getLocation());
  142. }
  143.  
  144. @Override
  145. public void render(RenderManager rm) {
  146.  
  147.  
  148. }
  149.  
  150. public void setUpKeys() {
  151.  
  152. inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
  153. inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
  154. inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
  155. inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
  156. inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
  157. inputManager.addListener(this, "Left");
  158. inputManager.addListener(this, "Right");
  159. inputManager.addListener(this, "Up");
  160. inputManager.addListener(this, "Down");
  161. inputManager.addListener(this, "Jump");
  162.  
  163. }
  164.  
  165. @Override
  166. public void onAction(String binding, boolean value, float tpf) {
  167.  
  168. if (binding.equals("Left")) {
  169.  
  170. left = value;
  171. }
  172.  
  173. if (binding.equals("Right")) {
  174.  
  175. right = value;
  176. }
  177.  
  178. if (binding.equals("Up")) {
  179.  
  180. up = value;
  181. }
  182.  
  183. if (binding.equals("Down")) {
  184.  
  185. down = value;
  186. }
  187.  
  188. if (binding.equals("Jump")) {
  189.  
  190. player.jump();
  191. }
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement