Advertisement
Guest User

Untitled

a guest
Aug 9th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.09 KB | None | 0 0
  1. import com.jme3.app.SimpleApplication;
  2. import com.jme3.bullet.BulletAppState;
  3. import com.jme3.bullet.control.RigidBodyControl;
  4. import com.jme3.input.KeyInput;
  5. import com.jme3.input.controls.ActionListener;
  6. import com.jme3.input.controls.AnalogListener;
  7. import com.jme3.input.controls.KeyTrigger;
  8. import com.jme3.light.AmbientLight;
  9. import com.jme3.material.Material;
  10. import com.jme3.math.ColorRGBA;
  11. import com.jme3.math.FastMath;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.scene.Geometry;
  14. import com.jme3.scene.shape.Box;
  15. import com.jme3.scene.shape.Sphere;
  16. import com.jme3.system.AppSettings;
  17. import com.jme3.system.JmeContext.Type;
  18. import com.jme3.texture.Texture;
  19.  
  20. public class Main extends SimpleApplication {
  21.    
  22.     public static void main(String[] args) {
  23.         Main app = new Main();
  24.         AppSettings settings = new AppSettings(true);
  25.         settings.setResolution(800, 600);
  26.         settings.setFullscreen(false);
  27.         settings.setSamples(1);
  28.         settings.setVSync(true);
  29.         settings.setBitsPerPixel(32);
  30.         settings.setTitle("Pong");
  31.         app.setSettings(settings);
  32.         app.setPauseOnLostFocus(false);
  33.         app.start(Type.Display); // start the game
  34.     }
  35.    
  36.     // Cenas importantes
  37.     private BulletAppState bulletAppState;
  38.            
  39.     // Nodes
  40.  
  41.     // Variáveis
  42.     private int spdMult = 1;
  43.    
  44.     // Geometries
  45.    
  46.     // Materiais
  47.     Material raquetMat;
  48.     Material wallMat;
  49.     Material bolaMat;
  50.    
  51.     // Physics
  52.     RigidBodyControl raquetPhy;
  53.     RigidBodyControl raquetPhy2;
  54.     private static final Box raquetShape;
  55.     RigidBodyControl wallPhy;
  56.     private static final Box wallShape;
  57.     RigidBodyControl bolaPhy;
  58.     private static final Sphere bolaShape;
  59.    
  60.     // Inicializar Shapes
  61.     static {
  62.         raquetShape = new Box(Vector3f.ZERO, 0.5f, 3, 0.5f);
  63.         wallShape = new Box(Vector3f.ZERO, 18, 2, 2);
  64.         bolaShape = new Sphere(32, 32, 0.6f);
  65.        
  66.     }
  67.            
  68.     public void simpleInitApp() {
  69.        
  70.         //Camera
  71.         flyCam.setEnabled(false);
  72.         if (flyCam.isEnabled())      
  73.             flyCam.setMoveSpeed(20);
  74.         else
  75.             setupKeys();
  76.        
  77.         cam.setLocation(new Vector3f(0, 0, 80));
  78.         cam.setFrustumFar(cam.getFrustumFar() + 10000);
  79.  
  80.         // ...
  81.    
  82.         bulletAppState = new BulletAppState();
  83.         stateManager.attach(bulletAppState);
  84.        
  85.         initMaterials();
  86.         initWalls();
  87.         initRaquets();
  88.         initBola();
  89.  
  90.         bulletAppState.getPhysicsSpace().enableDebug(assetManager);
  91.         bulletAppState.setSpeed(1);
  92.     }
  93.    
  94.     @Override
  95.     public void simpleUpdate(float tpf) {
  96.         Vector3f pos2 = raquetPhy2.getPhysicsLocation();
  97.         Vector3f pos = raquetPhy.getPhysicsLocation();
  98.         Vector3f posBall = bolaPhy.getPhysicsLocation();
  99.         raquetPhy2.setPhysicsLocation(new Vector3f(pos2.x, posBall.y, pos2.z));
  100.         if (posBall.x < -50 || posBall.x > 50) {
  101.             bolaPhy.setPhysicsLocation(Vector3f.ZERO);
  102.             bolaPhy.setLinearVelocity(new Vector3f(FastMath.nextRandomInt(-30, -40), FastMath.nextRandomInt(-10 , 10), 0));
  103.         }
  104.         raquetPhy.setEnabled(true);
  105.     }
  106.    
  107.     private void initMaterials() {
  108.         wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  109.         wallMat.setColor("Color", ColorRGBA.Red);
  110.        
  111.         raquetMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  112.         raquetMat.setColor("Color", ColorRGBA.Blue);
  113.        
  114.         bolaMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  115.         bolaMat.setColor("Color", ColorRGBA.Yellow);
  116.     }
  117.    
  118.     private void initWalls() {
  119.         Geometry wallGeo = new Geometry("walls", wallShape);
  120.         wallGeo.setMaterial(wallMat);
  121.         wallGeo.setLocalTranslation(new Vector3f(0, 20, 0));
  122.         rootNode.attachChild(wallGeo);
  123.         wallPhy = new RigidBodyControl(0);
  124.         wallGeo.addControl(wallPhy);
  125.         wallPhy.setRestitution(1);
  126.         bulletAppState.getPhysicsSpace().add(wallPhy);
  127.        
  128.         Geometry wallGeo2 = wallGeo.clone();
  129.         wallGeo2.setLocalTranslation(new Vector3f(0, -20, 0));
  130.         rootNode.attachChild(wallGeo2);
  131.         wallPhy = new RigidBodyControl(0);
  132.         wallGeo2.addControl(wallPhy);
  133.         wallPhy.setRestitution(1);
  134.         bulletAppState.getPhysicsSpace().add(wallPhy);
  135.     }
  136.    
  137.     private void initRaquets() {
  138.         Geometry raquetGeo = new Geometry("raquet", raquetShape);
  139.         raquetGeo.setMaterial(raquetMat);
  140.         raquetGeo.setLocalTranslation(new Vector3f(-20, 0, 0));
  141.         rootNode.attachChild(raquetGeo);
  142.         raquetPhy = new RigidBodyControl(0);
  143.         raquetGeo.addControl(raquetPhy);
  144.         bulletAppState.getPhysicsSpace().add(raquetPhy);
  145.         raquetPhy.setRestitution(1);
  146.         raquetPhy.setGravity(Vector3f.ZERO);
  147.        
  148.         Geometry raquetGeo2 = raquetGeo.clone();
  149.         raquetGeo2.setLocalTranslation(new Vector3f(20, 0, 0));
  150.         rootNode.attachChild(raquetGeo2);
  151.         raquetPhy2 = new RigidBodyControl(0);
  152.         raquetGeo2.addControl(raquetPhy2);
  153.         bulletAppState.getPhysicsSpace().add(raquetPhy2);
  154.         raquetPhy2.setRestitution(1);
  155.         raquetPhy2.setGravity(Vector3f.ZERO);
  156.     }
  157.    
  158.     private void initBola() {
  159.         Geometry bolaGeo = new Geometry("bola", bolaShape);
  160.         bolaGeo.setMaterial(bolaMat);
  161.         bolaGeo.setLocalTranslation(new Vector3f(0, 0, 0));
  162.         rootNode.attachChild(bolaGeo);
  163.         bolaPhy = new RigidBodyControl(2);
  164.         bolaGeo.addControl(bolaPhy);
  165.         bolaPhy.setLinearVelocity(new Vector3f(30, 10, 0));
  166.         bolaPhy.setRestitution(1);
  167.         bulletAppState.getPhysicsSpace().add(bolaPhy);
  168.         bolaPhy.setGravity(Vector3f.ZERO);
  169.     }
  170.    
  171.     private void setupKeys() {
  172.  
  173.         inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
  174.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
  175.         inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
  176.         inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
  177.         inputManager.addMapping("ZoomIn", new KeyTrigger(KeyInput.KEY_Q));
  178.         inputManager.addMapping("ZoomOut", new KeyTrigger(KeyInput.KEY_E));
  179.         inputManager.addMapping("IncCamSpeed", new KeyTrigger(KeyInput.KEY_F));
  180.         inputManager.addMapping("DecCamSpeed", new KeyTrigger(KeyInput.KEY_R));
  181.         inputManager.addMapping("FlyCamToggle", new KeyTrigger(KeyInput.KEY_G));
  182.         inputManager.addMapping("TestTrigger", new KeyTrigger(KeyInput.KEY_T));
  183.         inputManager.addMapping("TestTrigger2", new KeyTrigger(KeyInput.KEY_Y));
  184.  
  185.  
  186.         //inputManager.addMapping("AxisView", new KeyTrigger(KeyInput.KEY_C));
  187.  
  188.         inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up", "Down", "ZoomIn", "ZoomOut"});
  189.         inputManager.addListener(actionListener, new String[]{"IncCamSpeed", "DecCamSpeed", "TestTrigger", "TestTrigger2"});
  190.     }
  191.    
  192.     private ActionListener actionListener = new ActionListener() {
  193.  
  194.         public void onAction(String name, boolean keyPressed, float tpf) {
  195.            
  196.             if (name.equals("TestTrigger") && !keyPressed) {
  197.             }
  198.  
  199.             if (name.equals("TestTrigger2") && !keyPressed) {
  200.             }
  201.            
  202.             if (name.equals("IncCamSpeed") && !keyPressed) {
  203.                 if (spdMult < 10) {
  204.                     spdMult += 1;
  205.                 }
  206.             }
  207.  
  208.             if (name.equals("DecCamSpeed") && !keyPressed) {
  209.                 if (spdMult > 1) {
  210.                     spdMult -= 1;
  211.                 }  
  212.             }
  213.            
  214.         }
  215.     };
  216.      
  217.     private AnalogListener analogListener = new AnalogListener() {
  218.  
  219.         public void onAnalog(String name, float value, float tpf) {
  220.            
  221.             if (name.equals("Right")) {
  222.                 Vector3f pos = cam.getLocation();
  223.                 cam.setLocation(new Vector3f(pos.x + tpf * 100 * spdMult, pos.y, pos.z));
  224.             }
  225.             if (name.equals("Left")) {
  226.                 Vector3f pos = cam.getLocation();
  227.                 cam.setLocation(new Vector3f(pos.x - tpf * 100 * spdMult, pos.y, pos.z));
  228.             }
  229.             if (name.equals("ZoomIn")) {
  230.                 Vector3f pos = cam.getLocation();
  231.                 cam.setLocation(new Vector3f(pos.x, pos.y, pos.z - tpf * 100 * spdMult));
  232.             }
  233.             if (name.equals("ZoomOut")) {
  234.                 Vector3f pos = cam.getLocation();
  235.                 cam.setLocation(new Vector3f(pos.x, pos.y, pos.z + tpf * 100 * spdMult));
  236.             }
  237.             if (name.equals("Up")) {
  238.                 Vector3f pos = raquetPhy.getPhysicsLocation();
  239.                 raquetPhy.setPhysicsLocation(new Vector3f(pos.x, pos.y + tpf * 30, pos.z));
  240.             }
  241.             if (name.equals("Down")) {
  242.                 Vector3f pos = raquetPhy.getPhysicsLocation();
  243.                 raquetPhy.setPhysicsLocation(new Vector3f(pos.x, pos.y - tpf * 30, pos.z));
  244.             }
  245.         }
  246.     };
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement