Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jme3.app.SimpleApplication;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.light.AmbientLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.FastMath;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.shape.Box;
- import com.jme3.scene.shape.Sphere;
- import com.jme3.system.AppSettings;
- import com.jme3.system.JmeContext.Type;
- import com.jme3.texture.Texture;
- public class Main extends SimpleApplication {
- public static void main(String[] args) {
- Main app = new Main();
- AppSettings settings = new AppSettings(true);
- settings.setResolution(800, 600);
- settings.setFullscreen(false);
- settings.setSamples(1);
- settings.setVSync(true);
- settings.setBitsPerPixel(32);
- settings.setTitle("Pong");
- app.setSettings(settings);
- app.setPauseOnLostFocus(false);
- app.start(Type.Display); // start the game
- }
- // Cenas importantes
- private BulletAppState bulletAppState;
- // Nodes
- // Variáveis
- private int spdMult = 1;
- // Geometries
- // Materiais
- Material raquetMat;
- Material wallMat;
- Material bolaMat;
- // Physics
- RigidBodyControl raquetPhy;
- RigidBodyControl raquetPhy2;
- private static final Box raquetShape;
- RigidBodyControl wallPhy;
- private static final Box wallShape;
- RigidBodyControl bolaPhy;
- private static final Sphere bolaShape;
- // Inicializar Shapes
- static {
- raquetShape = new Box(Vector3f.ZERO, 0.5f, 3, 0.5f);
- wallShape = new Box(Vector3f.ZERO, 18, 2, 2);
- bolaShape = new Sphere(32, 32, 0.6f);
- }
- public void simpleInitApp() {
- //Camera
- flyCam.setEnabled(false);
- if (flyCam.isEnabled())
- flyCam.setMoveSpeed(20);
- else
- setupKeys();
- cam.setLocation(new Vector3f(0, 0, 80));
- cam.setFrustumFar(cam.getFrustumFar() + 10000);
- // ...
- bulletAppState = new BulletAppState();
- stateManager.attach(bulletAppState);
- initMaterials();
- initWalls();
- initRaquets();
- initBola();
- bulletAppState.getPhysicsSpace().enableDebug(assetManager);
- bulletAppState.setSpeed(1);
- }
- @Override
- public void simpleUpdate(float tpf) {
- Vector3f pos2 = raquetPhy2.getPhysicsLocation();
- Vector3f pos = raquetPhy.getPhysicsLocation();
- Vector3f posBall = bolaPhy.getPhysicsLocation();
- raquetPhy2.setPhysicsLocation(new Vector3f(pos2.x, posBall.y, pos2.z));
- if (posBall.x < -50 || posBall.x > 50) {
- bolaPhy.setPhysicsLocation(Vector3f.ZERO);
- bolaPhy.setLinearVelocity(new Vector3f(FastMath.nextRandomInt(-30, -40), FastMath.nextRandomInt(-10 , 10), 0));
- }
- raquetPhy.setEnabled(true);
- }
- private void initMaterials() {
- wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- wallMat.setColor("Color", ColorRGBA.Red);
- raquetMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- raquetMat.setColor("Color", ColorRGBA.Blue);
- bolaMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- bolaMat.setColor("Color", ColorRGBA.Yellow);
- }
- private void initWalls() {
- Geometry wallGeo = new Geometry("walls", wallShape);
- wallGeo.setMaterial(wallMat);
- wallGeo.setLocalTranslation(new Vector3f(0, 20, 0));
- rootNode.attachChild(wallGeo);
- wallPhy = new RigidBodyControl(0);
- wallGeo.addControl(wallPhy);
- wallPhy.setRestitution(1);
- bulletAppState.getPhysicsSpace().add(wallPhy);
- Geometry wallGeo2 = wallGeo.clone();
- wallGeo2.setLocalTranslation(new Vector3f(0, -20, 0));
- rootNode.attachChild(wallGeo2);
- wallPhy = new RigidBodyControl(0);
- wallGeo2.addControl(wallPhy);
- wallPhy.setRestitution(1);
- bulletAppState.getPhysicsSpace().add(wallPhy);
- }
- private void initRaquets() {
- Geometry raquetGeo = new Geometry("raquet", raquetShape);
- raquetGeo.setMaterial(raquetMat);
- raquetGeo.setLocalTranslation(new Vector3f(-20, 0, 0));
- rootNode.attachChild(raquetGeo);
- raquetPhy = new RigidBodyControl(0);
- raquetGeo.addControl(raquetPhy);
- bulletAppState.getPhysicsSpace().add(raquetPhy);
- raquetPhy.setRestitution(1);
- raquetPhy.setGravity(Vector3f.ZERO);
- Geometry raquetGeo2 = raquetGeo.clone();
- raquetGeo2.setLocalTranslation(new Vector3f(20, 0, 0));
- rootNode.attachChild(raquetGeo2);
- raquetPhy2 = new RigidBodyControl(0);
- raquetGeo2.addControl(raquetPhy2);
- bulletAppState.getPhysicsSpace().add(raquetPhy2);
- raquetPhy2.setRestitution(1);
- raquetPhy2.setGravity(Vector3f.ZERO);
- }
- private void initBola() {
- Geometry bolaGeo = new Geometry("bola", bolaShape);
- bolaGeo.setMaterial(bolaMat);
- bolaGeo.setLocalTranslation(new Vector3f(0, 0, 0));
- rootNode.attachChild(bolaGeo);
- bolaPhy = new RigidBodyControl(2);
- bolaGeo.addControl(bolaPhy);
- bolaPhy.setLinearVelocity(new Vector3f(30, 10, 0));
- bolaPhy.setRestitution(1);
- bulletAppState.getPhysicsSpace().add(bolaPhy);
- bolaPhy.setGravity(Vector3f.ZERO);
- }
- private void setupKeys() {
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
- inputManager.addMapping("ZoomIn", new KeyTrigger(KeyInput.KEY_Q));
- inputManager.addMapping("ZoomOut", new KeyTrigger(KeyInput.KEY_E));
- inputManager.addMapping("IncCamSpeed", new KeyTrigger(KeyInput.KEY_F));
- inputManager.addMapping("DecCamSpeed", new KeyTrigger(KeyInput.KEY_R));
- inputManager.addMapping("FlyCamToggle", new KeyTrigger(KeyInput.KEY_G));
- inputManager.addMapping("TestTrigger", new KeyTrigger(KeyInput.KEY_T));
- inputManager.addMapping("TestTrigger2", new KeyTrigger(KeyInput.KEY_Y));
- //inputManager.addMapping("AxisView", new KeyTrigger(KeyInput.KEY_C));
- inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up", "Down", "ZoomIn", "ZoomOut"});
- inputManager.addListener(actionListener, new String[]{"IncCamSpeed", "DecCamSpeed", "TestTrigger", "TestTrigger2"});
- }
- private ActionListener actionListener = new ActionListener() {
- public void onAction(String name, boolean keyPressed, float tpf) {
- if (name.equals("TestTrigger") && !keyPressed) {
- }
- if (name.equals("TestTrigger2") && !keyPressed) {
- }
- if (name.equals("IncCamSpeed") && !keyPressed) {
- if (spdMult < 10) {
- spdMult += 1;
- }
- }
- if (name.equals("DecCamSpeed") && !keyPressed) {
- if (spdMult > 1) {
- spdMult -= 1;
- }
- }
- }
- };
- private AnalogListener analogListener = new AnalogListener() {
- public void onAnalog(String name, float value, float tpf) {
- if (name.equals("Right")) {
- Vector3f pos = cam.getLocation();
- cam.setLocation(new Vector3f(pos.x + tpf * 100 * spdMult, pos.y, pos.z));
- }
- if (name.equals("Left")) {
- Vector3f pos = cam.getLocation();
- cam.setLocation(new Vector3f(pos.x - tpf * 100 * spdMult, pos.y, pos.z));
- }
- if (name.equals("ZoomIn")) {
- Vector3f pos = cam.getLocation();
- cam.setLocation(new Vector3f(pos.x, pos.y, pos.z - tpf * 100 * spdMult));
- }
- if (name.equals("ZoomOut")) {
- Vector3f pos = cam.getLocation();
- cam.setLocation(new Vector3f(pos.x, pos.y, pos.z + tpf * 100 * spdMult));
- }
- if (name.equals("Up")) {
- Vector3f pos = raquetPhy.getPhysicsLocation();
- raquetPhy.setPhysicsLocation(new Vector3f(pos.x, pos.y + tpf * 30, pos.z));
- }
- if (name.equals("Down")) {
- Vector3f pos = raquetPhy.getPhysicsLocation();
- raquetPhy.setPhysicsLocation(new Vector3f(pos.x, pos.y - tpf * 30, pos.z));
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement