Okay, I had for 2 days no Internet.
Anyway, here is the Main class:
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
public class Main extends SimpleApplication {
private BulletAppState bulletAppState;
private Node eyeR;
private BetterCharacterControl characterControl;
private Spatial playerModel;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
initTerrain();
initLight();
initPlayer();
initInput();
}
private void initTerrain() {
Spatial terrain = assetManager.loadModel("Scenes/TestScene.j3o");
terrain.setLocalTranslation(0, 0, 0);
bulletAppState.getPhysicsSpace().addAll(terrain);
rootNode.attachChild(terrain);
}
private void initLight() {
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);
rootNode.addLight(ambient);
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
}
private void initPlayer() {
characterControl = new BetterCharacterControl(1.3f, 7.5f, 80);
playerModel = assetManager.loadModel("Models/Characters/DefaultCharacterNew.j3o");
playerModel.setLocalTranslation(0, 0, 0);
playerModel.scale(4f);
playerModel.addControl(characterControl);
Node playerNode = (Node) playerModel;
characterControl.setGravity(new Vector3f(0, 10, 0));
characterControl.setJumpForce(new Vector3f(0, 400, 0));
characterControl.warp(new Vector3f(0, 3, 0));
// add to physicSpace and to rootNode
bulletAppState.getPhysicsSpace().addAll(playerModel);
rootNode.attachChild(playerModel);
// add controls
playerModel.addControl(new PlayerNavControl(cam));
playerModel.addControl(new PlayerAnimControl());
eyeR = (Node) playerNode.getChild("eye.R");
}
private void initInput() {
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Esc", new KeyTrigger(KeyInput.KEY_ESCAPE));
inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("ButtonRight", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("ButtonLeft", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Up")) {
playerModel.getControl(PlayerNavControl.class).up = isPressed;
}
if (name.equals("Down")) {
playerModel.getControl(PlayerNavControl.class).down = isPressed;
}
if (name.equals("Left")) {
playerModel.getControl(PlayerNavControl.class).left = isPressed;
}
if (name.equals("Right")) {
playerModel.getControl(PlayerNavControl.class).right = isPressed;
}
}
}, new String[]{"Up", "Down", "Left", "Right"});
}
@Override
public void simpleUpdate(float tpf) {
cam.setLocation(eyeR.getWorldTranslation());
characterControl.setViewDirection(cam.getDirection());
}
@Override
public void simpleRender(RenderManager rm) {
}
}
Player Anim:
package mygame;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
/**
*
-
@author Domenic
-
-
Handling animations for the player’s spatial
*/
public class PlayerAnimControl extends AbstractControl implements AnimEventListener {
private AnimControl animControl;
private AnimChannel armsChannel;
public PlayerAnimControl() {
}
@Override
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
if (spatial != null) {
animControl = spatial.getControl(AnimControl.class);
animControl.addListener(this);
armsChannel = animControl.createChannel();
armsChannel.setAnim("HoldM16Arms");
}
}
@Override
protected void controlUpdate(float tpf) {
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
}
@Override
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
// empty, not used
}
}
package mygame;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;
/**
*
-
@author Domenic
-
-
Rules the walking of the player
*/
public class PlayerNavControl extends AbstractControl {
public boolean up, down, left, right;
public boolean walking = true, ladder = false;
private Vector3f walkDirection;
private Camera cam;
public PlayerNavControl(Camera cam) {
this.cam = cam;
walkDirection = new Vector3f(0, 0, 0);
}
@Override
protected void controlUpdate(float tpf) {
getWalkDirection().set(Vector3f.ZERO);
if (walking) {
if (up) {
getWalkDirection().addLocal(cam.getDirection().multLocal(10f));
}
if (down) {
getWalkDirection().addLocal(cam.getDirection().multLocal(3f).negate());
}
if (left) {
getWalkDirection().addLocal(cam.getLeft().multLocal(3f));
}
if (right) {
getWalkDirection().addLocal(cam.getLeft().multLocal(3f).negate());
}
getWalkDirection().setY(0); // dass der Spieler nicht in die “Luft” gehen kann
spatial.getControl(BetterCharacterControl.class).setWalkDirection(getWalkDirection());
// return;
} else if (ladder) {
// empty
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
/**
-
@return the walkDirection
*/
public Vector3f getWalkDirection() {
return walkDirection;
}
}