Package net.cis.client.game

Source Code of net.cis.client.game.TestAI$NoRollLACam

package net.cis.client.game;

import java.util.HashMap;

import net.cis.client.game.scenery.factory.SkyBoxFactory;
import net.cis.client.game.ui.debug.DebugGUI;
import net.cis.client.game.ui.debug.DebugLogLevel;
import net.cis.client.game.util.ScreenRecorder;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;

public class TestAI extends SimpleApplication implements ActionListener{
  public static void main(String[] args) {
    new TestAI().start();
  }

  private NoRollLACam LACam;
  private DebugGUI debugGUI;
  private float TimePassed = 0;
  private boolean showMenu = false;
  private float nextGUIAction = 0;
  private ScreenRecorder screenRecorder;
 
  public TestAI()
  {
    super();
    AppSettings sets = new AppSettings(true);
    sets.setFullscreen(false);
    sets.setResolution(1024, 768);
    setSettings(sets);
    setShowSettings(false);
  }

  public void simpleInitApp() {

    viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));

    LACam = new NoRollLACam(cam){{CanJavaDoThis="IndeedItCan";}};
   
    setUpKeys();
    setUpLight();
    setUpEnvironment();

    debugGUI.addDebugOutput("TestAI App initialised");
    screenRecorder = new ScreenRecorder(this, "./screenshots/", null, true);
  }
 
  private void setUpEnvironment()  {
    rootNode.attachChild(SkyBoxFactory.createSimpleSkyBox(assetManager));

    Spatial asteroid = assetManager.loadModel("spaceobject/asteroid/dusty/Asteroid.mesh.xml");
    asteroid.setLocalTranslation(0f, 0f, 20f);
    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);
   
   
    debugGUI = new DebugGUI(this);
  }

  private void setUpLight() {
    // We add light so we see the scene
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(1.3f));
    rootNode.addLight(al);

    DirectionalLight dl = new DirectionalLight();
    dl.setColor(ColorRGBA.White);
    dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
    rootNode.addLight(dl);
  }

  private void setUpKeys() {
    getContext().getKeyInput().destroy();
    getContext().getKeyInput().initialize();
    inputManager.clearMappings();
   
    inputManager.addMapping("W", new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("A", new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("S", new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("D", new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping("Q", new KeyTrigger(KeyInput.KEY_Q));
    inputManager.addMapping("Y", new KeyTrigger(KeyInput.KEY_Y));
    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT));
    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT));
    inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_UP));
    inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_DOWN));
    inputManager.addMapping("Shift", new KeyTrigger(KeyInput.KEY_LSHIFT));
    inputManager.addListener(LACam, new String[]{"W", "A", "S", "D", "Q", "Y", "Right", "Left", "Up", "Down", "Shift"});
   
    inputManager.addMapping("MouseXleft", new MouseAxisTrigger(MouseInput.AXIS_X, true));
    inputManager.addMapping("MouseXright", new MouseAxisTrigger(MouseInput.AXIS_X, false));
    inputManager.addMapping("MouseYup", new MouseAxisTrigger(MouseInput.AXIS_Y, true));
    inputManager.addMapping("MouseYdown", new MouseAxisTrigger(MouseInput.AXIS_Y, false));
    inputManager.addListener(LACam, new String[]{"MouseXleft", "MouseXright", "MouseYup", "MouseYdown"});
   

    inputManager.addMapping("ESC", new KeyTrigger(KeyInput.KEY_ESCAPE));
    inputManager.addListener(this, new String[]{"ESC"});

  }

  /**
   * These are our custom actions triggered by key presses. We do not walk yet, we just keep track of the direction
   * the user pressed.
   */
  public void onAction(String name, boolean value, float tpf) {
   
    if(nextGUIAction > 0)
      return;
   
    if(name.equals("ESC"))
      {
      if(value)
        toggleMenu();
      }
    else
      debugGUI.addDebugOutput("Unrecognized command: " + name + " " + (value ? "Down" : "Up"), DebugLogLevel.Warning);
   
    nextGUIAction += .5f;
  }
 
  public void toggleMenu()
  {
    showMenu = !showMenu;
    LACam.setEnabled(!showMenu);
    debugGUI.togglePauseMode();
   
    debugGUI.addDebugOutput("Menu is now " + (showMenu ? "shown" : "hidden"));
  }

  /**
   * This is the main event loop--walking happens here. We check in which direction the player is walking by
   * interpreting the camera direction forward (camDir) and to the side (camLeft). The setWalkDirection() command is
   * what lets a physics-controlled player walk. We also make sure here that the camera moves with player.
   */
  @Override
  public void simpleUpdate(float tpf) {
    super.simpleUpdate(tpf);
   
   
    //ugly ugly ugly ugly
    if(TimePassed > 4)
      LACam.Update(tpf);
    else
      LACam.Reset();
   
    nextGUIAction -= tpf;
   
    TimePassed += tpf;
  }
 
  public static class NoRollLACam implements ActionListener, AnalogListener
  {
    public String CanJavaDoThis;
    private boolean enabled = true;
    private Camera Camera;
    private HashMap<String, Boolean> KeyState = new HashMap<String, Boolean>();
   
    private Vector3f Rotation = new Vector3f(0,0,0);
    private Vector3f Position = new Vector3f(0,0,0);
   
    private static float MovementSpeed = 50;
    private static float RotationSpeed = (float)Math.PI / 4f;
   
    private Vector3f CurrentMovement = new Vector3f(0, 0, 0);
    private Vector3f CurrentRotation = new Vector3f(0, 0, 0);
   
    private static Vector3f View = new Vector3f(0,0,1);
    private static Vector3f Up = new Vector3f(0,1,0);
   
    public NoRollLACam(Camera cam)
    {
      Camera = cam;
      Reset();
    }
   
    public void setEnabled(boolean ena)
    {
      enabled = ena;
      if(enabled)
        Reset(true);
    }

    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
      KeyState.put(name, isPressed);
    }
    @Override
    public void onAnalog(String name, float value, float tpf) {
      if(name.equals("MouseXleft"))
        CurrentRotation.y += value * 1000;
      if(name.equals("MouseXright"))
        CurrentRotation.y -= value * 1000;
      if(name.equals("MouseYup"))
        CurrentRotation.x += value * 1000;
      if(name.equals("MouseYdown"))
        CurrentRotation.x -= value * 1000;
     
    }
   
    public void Update(float tpf)
    {
      if(!enabled)
        return;
     
      if(KeyState.containsKey("W") && KeyState.get("W"))
        CurrentMovement.z++;
      if(KeyState.containsKey("S") && KeyState.get("S"))
        CurrentMovement.z--;
      if(KeyState.containsKey("A") && KeyState.get("A"))
        CurrentMovement.x++;
      if(KeyState.containsKey("D") && KeyState.get("D"))
        CurrentMovement.x--;
      if(KeyState.containsKey("Q") && KeyState.get("Q"))
        CurrentMovement.y++;
      if(KeyState.containsKey("Y") && KeyState.get("Y"))
        CurrentMovement.y--;
     
      if(KeyState.containsKey("Shift") && KeyState.get("Shift"))
        CurrentMovement.multLocal(10);

      if(KeyState.containsKey("Left") && KeyState.get("Left"))
        CurrentRotation.y++;
      if(KeyState.containsKey("Right") && KeyState.get("Right"))
        CurrentRotation.y--;
      if(KeyState.containsKey("Up") && KeyState.get("Up"))
        CurrentRotation.x--;
      if(KeyState.containsKey("Down") && KeyState.get("Down"))
        CurrentRotation.x++;

      Quaternion q = new Quaternion();//Quaternion.DIRECTION_Z;
      q.fromAngles(Rotation.x, Rotation.y, Rotation.z);
     
     
      q.mult(CurrentMovement, CurrentMovement);
     
      Position.addLocal(CurrentMovement.multLocal(MovementSpeed * tpf));
      Rotation.addLocal(CurrentRotation.multLocal(RotationSpeed * tpf));
     
      Camera.setLocation(Position);
      Camera.lookAt(q.mult(View).addLocal(Position), q.mult(Up));
     
      CurrentMovement.set(0,0,0);
      CurrentRotation.set(0,0,0);
    }
   
    public void Reset(){Reset(false);}
    public void Reset(boolean soft)
    {
      CurrentMovement.set(0,0,0);
      CurrentRotation.set(0,0,0);

      if(soft)
        return;
     
      Rotation.set(0,0,0);
      Position.set(0,0,0);
    }
  }

}
TOP

Related Classes of net.cis.client.game.TestAI$NoRollLACam

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.