Package edu.ups.gamedev.examples.one

Source Code of edu.ups.gamedev.examples.one.ExampleGame$MoveForward

package edu.ups.gamedev.examples.one;

import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.input.KeyInput;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;

import edu.ups.gamedev.player.PlayerSphere;

public class ExampleGame extends SimpleGame {
  PlayerSphere localSphere;
  Node scene;
  static final float MOVEINC = .25f;

  public static void main(String[] args) {
    ExampleGame app = new ExampleGame();
    app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);
    app.start();
  }
 
  @Override
  protected void simpleInitGame() {
    scene = rootNode; //copy rootNode into scene so that we can use reflection to get it later
    localSphere = new PlayerSphere("localSphere", ColorRGBA.red.clone()); //the sphere for our player, we'll make it red
    rootNode.attachChild(localSphere); //add our sphere to the scenegraph
   
    //setup additional keyboard commands
    //note that SimpleGame (which our class extends) already gives us WASD movement and mouse-look controls
    //so we'll use UHJK to move the client sphere, like WASD, but on the other side of the keyboard
    input.addAction(new MoveForward(), "MoveForward", KeyInput.KEY_J, true);
    input.addAction(new MoveBack(), "MoveBack", KeyInput.KEY_U, true);
    input.addAction(new MoveRight(), "MoveRight", KeyInput.KEY_K, true);
    input.addAction(new MoveLeft(), "MoveLeft", KeyInput.KEY_H, true);
  }
 
  //The events that occur when a key is pressed trigger actions.
  //We can create custom actions by extending KeyInput action.
  class MoveForward extends KeyInputAction {
    public void performAction(InputActionEvent evt) {
      //we get the location of the sphere, add a small vector to it, and set the resulting vector as the new position
      localSphere.setLocalTranslation(localSphere.getLocalTranslation().add(new Vector3f(0, 0, MOVEINC)));
     
    }
  }
 
  class MoveBack extends KeyInputAction {
    public void performAction(InputActionEvent evt) {
      localSphere.setLocalTranslation(localSphere.getLocalTranslation().add(new Vector3f(0, 0, -MOVEINC)));
     
    }
  }
 
  class MoveRight extends KeyInputAction {
    public void performAction(InputActionEvent evt) {
      localSphere.setLocalTranslation(localSphere.getLocalTranslation().add(new Vector3f(MOVEINC, 0, 0)));
     
    }
  }
 
  class MoveLeft extends KeyInputAction {
    public void performAction(InputActionEvent evt) {
      localSphere.setLocalTranslation(localSphere.getLocalTranslation().add(new Vector3f(-MOVEINC, 0, 0)));
     
    }
  }
 
 
}
TOP

Related Classes of edu.ups.gamedev.examples.one.ExampleGame$MoveForward

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.