Advertisement
ComputerCraft32

VehicleControl.java

Jul 7th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mygame.controls_states;
  7.  
  8. import com.jme3.app.SimpleApplication;
  9. import com.jme3.input.InputManager;
  10. import com.jme3.input.KeyInput;
  11. import com.jme3.input.controls.ActionListener;
  12. import com.jme3.input.controls.KeyTrigger;
  13. import com.jme3.math.Vector3f;
  14. import com.jme3.renderer.RenderManager;
  15. import com.jme3.renderer.ViewPort;
  16. import com.jme3.scene.control.AbstractControl;
  17. import static java.lang.Float.max;
  18. import static java.lang.Float.min;
  19. import java.util.Random;
  20. import java.util.concurrent.ThreadLocalRandom;
  21.  
  22.  
  23. /**
  24.  *
  25.  
  26.  */
  27. public class VehicleControl extends AbstractControl {
  28.   static double count = 1;
  29.     static  float deltaTime = 0;
  30.     InputManager inputManager;
  31.     SimpleApplication app;
  32.    
  33.     public VehicleControl(InputManager inputManager) {
  34.         this.inputManager = inputManager;
  35.         //this.app = app;
  36.         initKeys();
  37.        
  38.            
  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.                 GameState.getVehicleController().setLinearVelocity(new Vector3f (50, 0, 0));
  48.          if ("Down".equals(name));
  49.                 GameState.getVehicleController().setLinearVelocity(new Vector3f (-50, 0, 0));
  50.          if ("Left".equals(name))  
  51.                 GameState.getVehicleController().setLinearVelocity(new Vector3f (0, 0, -50));
  52.          if ("Right".equals(name))
  53.                 GameState.getVehicleController().setLinearVelocity(new Vector3f (0, 0, 50));
  54.         }
  55.          }
  56.         };
  57.  
  58.    
  59.     private void initKeys() {
  60.        
  61.        
  62.         inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_1));
  63.         inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_2));
  64.         inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
  65.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
  66.        
  67.         inputManager.addListener(actionListener, "Up");
  68.         inputManager.addListener(actionListener, "Down");
  69.         inputManager.addListener(actionListener, "Left");
  70.         inputManager.addListener(actionListener, "Right");
  71.        
  72.        
  73.            
  74.     }
  75.    
  76.     @Override
  77.     protected void controlUpdate(float tpf) {
  78.        
  79.         deltaTime += tpf;
  80.        
  81.        
  82.        
  83.     ////    double low = -7.0f;
  84.     //    double high = 10.0f;
  85.       //  double i = 0;
  86.        
  87.    //     Random random = new Random();
  88.     //     if (deltaTime == 0.1){
  89.       //  double randomer = ThreadLocalRandom.current().nextDouble(low, high);
  90.  if(deltaTime > 0.1)   {
  91.      // if ( count == 1) {
  92.        //   i = 5;
  93.     //      count = -1;
  94.     //  }
  95.    //   else if ( count == -1) {
  96.     //      i = -1;
  97.   //        count = 1;
  98.  //     }
  99.      
  100.        GameState.getVehicleController().applyCentralForce(new Vector3f(0, 200, 0));
  101.         deltaTime = 0;
  102.        count = -count;
  103.    }
  104.  
  105.     }
  106.  
  107.     @Override
  108.     protected void controlRender(RenderManager rm, ViewPort vp) {
  109.      //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  110.     }
  111.    
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement