ComputerCraft32

Main

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