Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mygame.controls_states;
- import com.jme3.app.SimpleApplication;
- import com.jme3.input.InputManager;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.control.AbstractControl;
- import static java.lang.Float.max;
- import static java.lang.Float.min;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- /**
- *
- */
- public class VehicleControl extends AbstractControl {
- static double count = 1;
- static float deltaTime = 0;
- InputManager inputManager;
- SimpleApplication app;
- public VehicleControl(InputManager inputManager) {
- this.inputManager = inputManager;
- //this.app = app;
- initKeys();
- }
- ActionListener actionListener = new ActionListener() {
- @Override
- public void onAction(String name, boolean keyPressed, float tpf) {
- if(keyPressed){
- if ("Up".equals(name))
- GameState.getVehicleController().setLinearVelocity(new Vector3f (50, 0, 0));
- if ("Down".equals(name));
- GameState.getVehicleController().setLinearVelocity(new Vector3f (-50, 0, 0));
- if ("Left".equals(name))
- GameState.getVehicleController().setLinearVelocity(new Vector3f (0, 0, -50));
- if ("Right".equals(name))
- GameState.getVehicleController().setLinearVelocity(new Vector3f (0, 0, 50));
- }
- }
- };
- private void initKeys() {
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_1));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_2));
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
- inputManager.addListener(actionListener, "Up");
- inputManager.addListener(actionListener, "Down");
- inputManager.addListener(actionListener, "Left");
- inputManager.addListener(actionListener, "Right");
- }
- @Override
- protected void controlUpdate(float tpf) {
- deltaTime += tpf;
- //// double low = -7.0f;
- // double high = 10.0f;
- // double i = 0;
- // Random random = new Random();
- // if (deltaTime == 0.1){
- // double randomer = ThreadLocalRandom.current().nextDouble(low, high);
- if(deltaTime > 0.1) {
- // if ( count == 1) {
- // i = 5;
- // count = -1;
- // }
- // else if ( count == -1) {
- // i = -1;
- // count = 1;
- // }
- GameState.getVehicleController().applyCentralForce(new Vector3f(0, 200, 0));
- deltaTime = 0;
- count = -count;
- }
- }
- @Override
- protected void controlRender(RenderManager rm, ViewPort vp) {
- // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement