ComputerCraft32

GameState.java

Jul 9th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 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.Application;
  9. import com.jme3.app.SimpleApplication;
  10. import com.jme3.app.state.AbstractAppState;
  11. import com.jme3.app.state.AppStateManager;
  12. import com.jme3.asset.AssetManager;
  13. import com.jme3.bullet.BulletAppState;
  14. import com.jme3.bullet.control.RigidBodyControl;
  15. import com.jme3.input.InputManager;
  16. import com.jme3.material.Material;
  17. import com.jme3.math.ColorRGBA;
  18. import com.jme3.math.Vector3f;
  19. import com.jme3.scene.Geometry;
  20. import com.jme3.scene.Node;
  21. import com.jme3.scene.Spatial;
  22. import com.jme3.scene.shape.Box;
  23. import java.util.Random;
  24.  
  25. /**
  26.  *
  27.  
  28.  */
  29. public class GameState extends AbstractAppState {
  30.    
  31.     Spatial terrain;
  32.     SimpleApplication app;
  33.     InputManager inputManager;
  34.     AssetManager assetManager;
  35.     Node rootNode;
  36.     BulletAppState appState;
  37.     Geometry vehicle;
  38.     RigidBodyControl vehiclePhysics;
  39.     Random random = new Random();
  40.    
  41.     @Override
  42.       public void initialize(AppStateManager stateManager, Application app) {
  43.         super.initialize(stateManager, app);
  44.         this.app = (SimpleApplication) app;
  45.         this.inputManager = this.app.getInputManager();
  46.         this.assetManager = this.app.getAssetManager();
  47.         this.rootNode = this.app.getRootNode();
  48.         this.app.getFlyByCamera().setMoveSpeed(100);
  49.        
  50.         appState = new BulletAppState();
  51.         stateManager.attach(appState);
  52.        
  53.         createTerrain();
  54.         createVehicle();
  55.     }
  56.  
  57.       private void createTerrain() {
  58.           terrain = assetManager.loadModel("Textures/GameScene.j3o");
  59.           terrain.setLocalTranslation(0, 0, 0);
  60.          
  61.           RigidBodyControl terrainControl = new RigidBodyControl(0);
  62.           terrain.addControl(terrainControl);
  63.           appState.getPhysicsSpace().add(terrainControl);
  64.          
  65.           rootNode.attachChild(terrain);  
  66.       }
  67.      
  68.       private void createVehicle() {
  69.           Box vehicleBox = new Box(10, 2, 10);
  70.           vehicle = new Geometry("Vehicle", vehicleBox);
  71.           Material vehicleMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  72.           vehicleMaterial.setColor("Color", ColorRGBA.Blue);
  73.           vehicleMaterial.getAdditionalRenderState().setWireframe(true);
  74.           vehicle.setMaterial(vehicleMaterial);
  75.           vehicle.setLocalTranslation(0, 50, 0);
  76.          
  77.           vehiclePhysics = new RigidBodyControl(50);
  78.           vehicle.addControl(vehiclePhysics);
  79.           vehiclePhysics.setKinematic(true);
  80.           appState.getPhysicsSpace().add(vehiclePhysics);
  81.           appState.getPhysicsSpace().setGravity(new Vector3f(0, -15, 0));
  82.          
  83.           Box collisionBox = new Box(10, 2, 10);
  84.           Geometry collisionGeo = new Geometry("CollisonBox", collisionBox);
  85.           vehicle.setLocalTranslation(0, 50, 0);
  86.          
  87.           RigidBodyControl collisionControl = new RigidBodyControl(50);
  88.           collisionGeo.addControl(collisionControl);
  89.           appState.getPhysicsSpace().add(collisionControl);
  90.          
  91.           VehicleController vehicleController = new VehicleController(inputManager, vehicle, terrain, vehiclePhysics, appState);
  92.           vehicle.addControl(vehicleController);
  93.           appState.getPhysicsSpace().addCollisionListener(vehicleController);
  94.           rootNode.attachChild(vehicle);
  95.       }
  96.      
  97.       @Override
  98.         public void update(float tpf) {
  99.            
  100. //            if (VehicleController.signal == 1) {
  101. //                vehiclePhysics.applyImpulse(new Vector3f(random.nextInt(30), random.nextInt(30), random.nextInt(30)), Vector3f.ZERO);
  102. //                vehiclePhysics.applyCentralForce(new Vector3f(random.nextInt(50),random.nextInt(50), random.nextInt(50) ));
  103. //            }
  104.        
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment