Advertisement
ComputerCraft32

Main

Jul 7th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 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.PhysicsSpace;
  6. import com.jme3.bullet.PhysicsTickListener;
  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.material.Material;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.renderer.RenderManager;
  14. import com.jme3.scene.Geometry;
  15. import com.jme3.scene.Spatial;
  16. import com.jme3.scene.shape.Box;
  17.  
  18.  
  19. /**
  20. * This is the Main Class of your Game. You should only do initialization here.
  21. * Move your Logic into AppStates or Controls
  22. * @author normenhansen
  23. */
  24. public class Main extends SimpleApplication implements PhysicsTickListener {
  25. RigidBodyControl vehicleControler;
  26. int i;
  27. BulletAppState appState;
  28.  
  29.  
  30. public static void main(String[] args) {
  31.  
  32. Main app = new Main();
  33. app.setShowSettings(false);
  34. app.start();
  35. }
  36.  
  37. ActionListener actionListener = new ActionListener() {
  38.  
  39. @Override
  40. public void onAction(String name, boolean keyPressed, float tpf) {
  41. if(keyPressed){
  42. if ("Up".equals(name))
  43. i = 1;
  44. if ("Down".equals(name));
  45. i = 2;
  46. if ("Left".equals(name))
  47. i = 3;
  48. if ("Right".equals(name))
  49. i = 4;
  50. }
  51. }
  52. };
  53.  
  54.  
  55.  
  56. @Override
  57. public void simpleInitApp() {
  58. flyCam.setMoveSpeed(100);
  59.  
  60. appState = new BulletAppState();
  61. stateManager.attach(appState);
  62.  
  63. Spatial terrain = assetManager.loadModel("Textures/GameScene.j3o");
  64. terrain.setLocalTranslation(0, -100, 0);
  65.  
  66.  
  67. RigidBodyControl terrainPhysics = new RigidBodyControl(0);
  68. terrain.addControl(terrainPhysics);
  69. appState.getPhysicsSpace().add(terrainPhysics);
  70.  
  71. rootNode.attachChild(terrain);
  72.  
  73. Box box = new Box(5, 2f, 5);
  74. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  75. Geometry geo = new Geometry("Vehicle", box);
  76. geo.setMaterial(mat);
  77. geo.setCullHint(Spatial.CullHint.Never);
  78.  
  79.  
  80. vehicleControler = new RigidBodyControl(2);
  81. geo.addControl(vehicleControler);
  82. appState.getPhysicsSpace().add(vehicleControler);
  83. appState.getPhysicsSpace().setGravity(new Vector3f(0, -9.8f, 0));
  84. vehicleControler.setPhysicsLocation(new Vector3f(0, -50, 0));
  85.  
  86. rootNode.attachChild(geo);
  87.  
  88. inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_1));
  89. inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_2));
  90. inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
  91. inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
  92.  
  93. inputManager.addListener(actionListener, "Up");
  94. inputManager.addListener(actionListener, "Down");
  95. inputManager.addListener(actionListener, "Left");
  96. inputManager.addListener(actionListener, "Right");
  97.  
  98.  
  99.  
  100. }
  101.  
  102. @Override
  103. public void simpleUpdate(float tpf) {
  104. //TODO: add update code
  105. }
  106.  
  107. @Override
  108. public void simpleRender(RenderManager rm) {
  109. //TODO: add render code
  110. }
  111.  
  112. @Override
  113. public void prePhysicsTick(PhysicsSpace space, float tpf) {
  114. switch (i) {
  115. case 1:
  116. vehicleControler.setLinearVelocity(new Vector3f(10, 0, 0));
  117. break;
  118. case 2:
  119. vehicleControler.setLinearVelocity(new Vector3f(-10, 0, 0));
  120. break;
  121. case 3:
  122. vehicleControler.setLinearVelocity(new Vector3f(-10, 0, 0));
  123. break;
  124. case 4:
  125. vehicleControler.setLinearVelocity(new Vector3f(+10, 0, 0));
  126. break;
  127.  
  128. }
  129. System.out.println("preTick called! Var i is :" + i);
  130. }
  131.  
  132. @Override
  133. public void physicsTick(PhysicsSpace space, float tpf) {
  134. System.out.println("phsyicsTick called!");
  135. // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement