Package net.cis.client.game

Source Code of net.cis.client.game.BaseGame

/**
*
*/
package net.cis.client.game;

import net.cis.client.game.input.listener.InputListener;
import net.cis.client.game.scenery.factory.SkyBoxFactory;
import net.cis.client.game.scenery.model.Player;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.Arrow;
import com.jme3.system.AppSettings;

/**
* @author Matze
*
*/
public class BaseGame extends SimpleApplication {

  private BulletAppState bulletAppState;
  private RigidBodyControl vehicleControl;
  private Player player;
  private ChaseCamera chaseCam;

  public BaseGame() {
    AppSettings settings = new AppSettings(true);
    settings.setFullscreen(false);
    settings.setResolution(1024, 800);
    setSettings(settings);
    setShowSettings(false);
  }

  /*
   * (non-Javadoc)
   *
   * @see com.jme3.app.SimpleApplication#simpleInitApp()
   */
  @Override
  public void simpleInitApp() {

    // Physics
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);

    rootNode.attachChild(SkyBoxFactory.createSimpleSkyBox(assetManager));

    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

    vehicleControl = new RigidBodyControl(capsuleShape, 1f);
    vehicleControl.setKinematic(false);
    player = new Player(assetManager);
    player.setLocalTranslation(0.0f, -5.0f, -2.0f);

    bulletAppState.getPhysicsSpace().setGravity(Vector3f.ZERO);
    bulletAppState.getPhysicsSpace().add(vehicleControl);
    player.addControl(vehicleControl);

    System.out.println(rootNode.getCullHint());
    System.out.println(player.getCullHint());
    rootNode.attachChild(player);

    // Disable the default first-person cam!
    flyCam.setEnabled(false);

    // Enable a chase cam
    chaseCam = new ChaseCamera(cam, player, inputManager);
    chaseCam.setDefaultDistance(5f);

    // CameraControl cc = new CameraControl(cam, ControlDirection.SpatialToCamera);
    // cc.setSpatial(player);

    Spatial asteroid = assetManager.loadModel("spaceobject/asteroid/dusty/Asteroid.mesh.xml");
    asteroid.setLocalTranslation(10.0f, -5.0f, -2.0f);
    Material mat_asteroid = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat_asteroid.setTexture("ColorMap",
        assetManager.loadTexture("spaceobject/asteroid/dusty/asteroidtextur_512.jpg"));
    asteroid.setMaterial(mat_asteroid);

    rootNode.attachChild(asteroid);

    // You must add a light to make the model visible
    DirectionalLight sun = new DirectionalLight();
    sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
    rootNode.addLight(sun);

    initializeInput();

    attachCoordinateAxes(new Vector3f(0.0f, -5.0f, -2.0f));
  }

  private void initializeInput() {
    inputManager.clearMappings();

    // Mouse Input
    inputManager.addMapping("RotateUp", new KeyTrigger(KeyInput.KEY_Y));
    inputManager.addMapping("RotateDown", new KeyTrigger(KeyInput.KEY_X));
    inputManager.addMapping("RotateLeft", new MouseAxisTrigger(MouseInput.AXIS_X, false));
    inputManager.addMapping("RotateRight", new MouseAxisTrigger(MouseInput.AXIS_X, true));

    // Keyboard Input
    inputManager.addMapping("Accelerate", new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("Brake", new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("RollLeft", new KeyTrigger(KeyInput.KEY_Q));
    inputManager.addMapping("RollRight", new KeyTrigger(KeyInput.KEY_E));
    inputManager.addMapping("StrafeLeft", new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("StrafeRight", new KeyTrigger(KeyInput.KEY_D));

    inputManager.addMapping("bla", new KeyTrigger(KeyInput.KEY_LEFT));
    inputManager.addMapping("blubb", new KeyTrigger(KeyInput.KEY_RIGHT));

    inputManager.addListener(new InputListener(vehicleControl, player), new String[] { "RotateUp", "RotateDown",
        "RotateLeft", "RotateRight", "Accelerate", "Brake", "RollLeft", "RollRight", "StrafeLeft",
        "StrafeRight", "bla", "blubb" });
  }

  @Override
  public void simpleUpdate(float tpf) {
    super.simpleUpdate(tpf);
  }

  public static void main(String[] args) {
    new BaseGame().start();
  }

  private void attachCoordinateAxes(Vector3f pos) {
    Arrow arrow = new Arrow(Vector3f.UNIT_X);
    arrow.setLineWidth(4); // make arrow thicker
    putShape(arrow, ColorRGBA.Red).setLocalTranslation(pos);

    arrow = new Arrow(Vector3f.UNIT_Y);
    arrow.setLineWidth(4); // make arrow thicker
    putShape(arrow, ColorRGBA.Green).setLocalTranslation(pos);

    arrow = new Arrow(Vector3f.UNIT_Z);
    arrow.setLineWidth(4); // make arrow thicker
    putShape(arrow, ColorRGBA.Blue).setLocalTranslation(pos);
  }

  private Geometry putShape(Mesh shape, ColorRGBA color) {
    Geometry g = new Geometry("coordinate axis", shape);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", color);
    g.setMaterial(mat);
    rootNode.attachChild(g);
    return g;
  }

}
TOP

Related Classes of net.cis.client.game.BaseGame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.