I’ve been trying to make a simple fighting game where 2 boxes each with an arm try to punch each other off the stage. I have no idea how to simulate an attack however. The arm I added moves left and right. How do I get it to stay put?
GameRunningAppState.java
[java]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package mygame;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.InputListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
/**
*
-
@author oscar
*/
public class GameRunningAppState extends AbstractAppState {private AssetManager assetManager;
private Node rootNode;
private SimpleApplication app;
private Camera cam;
private InputManager inputManager;
private InputListener inputListener;
private BulletAppState bulletAppState;
private AppStateManager stateManager;
private final static String INPUT_MAPPING_EXIT = “Exit”;
private final static Trigger INPUT_TRIGGER_EXIT = new KeyTrigger(KeyInput.KEY_ESCAPE);
Node nodeStage = new Node();
private Node nodeP1 = new Node();
private Node nodeP2 = new Node();
private Geometry geoP1 = new Geometry();
private Geometry geoP2 = new Geometry();
private Geometry geoArmP1;
RigidBodyControl phyArmP1;private Geometry geoStage;
private BetterCharacterControl phyP1;
private BetterCharacterControl phyP2;
private final static Trigger TRIGGER_P1_LEFT = new KeyTrigger(KeyInput.KEY_A);
private final static Trigger TRIGGER_P1_RIGHT = new KeyTrigger(KeyInput.KEY_D);
private final static Trigger TRIGGER_P1_UP = new KeyTrigger(KeyInput.KEY_W);
private final static String MAPPING_P1_LEFT = “P1: Move Left”;
private final static String MAPPING_P1_RIGHT = “P1: Move Right”;
private final static String MAPPING_P1_UP = “P1: Jump”;
private final static Trigger TRIGGER_P2_LEFT = new KeyTrigger(KeyInput.KEY_LEFT);
private final static Trigger TRIGGER_P2_RIGHT = new KeyTrigger(KeyInput.KEY_RIGHT);
private final static Trigger TRIGGER_P2_UP = new KeyTrigger(KeyInput.KEY_UP);
private final static String MAPPING_P2_LEFT = “P2: Move Left”;
private final static String MAPPING_P2_RIGHT = “P2: Move Right”;
private final static String MAPPING_P2_UP = “P2: Jump”;
private boolean leftP1 = false, rightP1 = false, upP1 = false;
private boolean leftP2 = false, rightP2 = false, upP2 = false;
private Vector3f moveP1 = new Vector3f();
private Vector3f moveP2 = new Vector3f();@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
//TODO: initialize your AppState, e.g. attach spatials to rootNode
//this is called on the OpenGL thread after the AppState has been attached
this.app = (SimpleApplication) app;
this.cam = this.app.getCamera();
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.inputManager = this.app.getInputManager();
this.stateManager = this.app.getStateManager();
//this.bulletAppState=this.stateManager.getState(BulletAppState.class);
cam.setLocation(new Vector3f(0.0f, 0.0f, 50.0f));
inputManager.addMapping(INPUT_MAPPING_EXIT, INPUT_TRIGGER_EXIT);bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); rootNode.attachChild(nodeStage); initStage(); initPlayers(); initKeys(); bulletAppState.setDebugEnabled(true);
}
private void initStage() {
Box meshStage = new Box(500.0f, 1.0f, 8.0f);
geoStage = new Geometry(“Stage”, meshStage);
Material matStage = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
matStage.setColor(“Color”, ColorRGBA.White);
geoStage.setMaterial(matStage);
nodeStage.attachChild(geoStage);
nodeStage.move(0, -10.0f, -50.0f);
RigidBodyControl phyStage = new RigidBodyControl(0.0f);
geoStage.addControl(phyStage);
bulletAppState.getPhysicsSpace().add(phyStage);
}private void initPlayers() {
Box meshPlayer = new Box(2.0f, 4.0f, 2.0f); geoP1 = new Geometry("Player 1", meshPlayer); geoP2 = new Geometry("Player 2", meshPlayer); Material matP1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); Material matP2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matP1.setColor("Color", ColorRGBA.Blue); matP2.setColor("Color", ColorRGBA.Red); geoP1.setMaterial(matP1); geoP2.setMaterial(matP2); nodeP1 = new Node(); nodeP2 = new Node(); nodeStage.attachChild(nodeP1); nodeStage.attachChild(nodeP2); nodeP1.attachChild(geoP1); nodeP2.attachChild(geoP2); Box meshArm=new Box(2.0f,.5f,.5f); geoArmP1=new Geometry("Right Arm",meshArm); Material matArmP1=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md"); matArmP1.setColor("Color", ColorRGBA.Cyan); geoArmP1.setMaterial(matArmP1); nodeP1.attachChild(geoArmP1); geoArmP1.move(2.0f,6.0f,2.0f); nodeP1.move(-10.0f, 10.0f, 0f); nodeP2.move(10.0f, 10.0f, 0f); //Geometries are moved up because bottom of physics control is at //center(the middle of physics is not) geoP1.move(0, 4.0f, 0); geoP2.move(0, 4.0f, 0); phyP1 = new BetterCharacterControl(2f, 8.0f, 1f); phyP2 = new BetterCharacterControl(2f, 8.0f, 1f); phyP1.setJumpForce(new Vector3f(0, 20, 0)); phyP2.setJumpForce(new Vector3f(0, 20, 0)); phyP1.setPhysicsDamping(1); phyP2.setPhysicsDamping(1); nodeP1.addControl(phyP1); nodeP2.addControl(phyP2); bulletAppState.getPhysicsSpace().add(phyP1); bulletAppState.getPhysicsSpace().add(phyP2); //phyArmP1=new RigidBodyControl(1f); //geoArmP1.addControl(phyArmP1); //phyArmP1.setKinematic(true); //bulletAppState.getPhysicsSpace().add(phyArmP1);
}
private void initKeys() {
inputManager.addMapping(MAPPING_P1_LEFT, TRIGGER_P1_LEFT);
inputManager.addMapping(MAPPING_P1_RIGHT, TRIGGER_P1_RIGHT);
inputManager.addMapping(MAPPING_P1_UP, TRIGGER_P1_UP);
inputManager.addMapping(MAPPING_P2_LEFT, TRIGGER_P2_LEFT);
inputManager.addMapping(MAPPING_P2_RIGHT, TRIGGER_P2_RIGHT);
inputManager.addMapping(MAPPING_P2_UP, TRIGGER_P2_UP);
inputManager.addListener(actionListener, MAPPING_P1_LEFT, MAPPING_P1_RIGHT,
MAPPING_P1_UP, MAPPING_P2_UP,
MAPPING_P2_LEFT, MAPPING_P2_RIGHT);}
private ActionListener actionListener = new ActionListener() {
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals(MAPPING_P1_LEFT)) {
leftP1 = isPressed;
System.out.println(“ALeftP1”);
}
if (binding.equals(MAPPING_P1_RIGHT)) {
rightP1 = isPressed;
System.out.println(“ARightP1”);
}
if (binding.equals(MAPPING_P1_UP)) {
phyP1.jump();
System.out.println(“AUpP1”);
}
if (binding.equals(MAPPING_P2_LEFT)) {
leftP2 = isPressed;
System.out.println(“ALeftP2”);
}
if (binding.equals(MAPPING_P2_RIGHT)) {
rightP2 = isPressed;
System.out.println(“ARightP2”);
}
if (binding.equals(MAPPING_P2_UP)) {
phyP2.jump();
System.out.println(“AUpP2”);
}
}
};@Override
public void update(float tpf) {
//TODO: implement behavior during runtime
//Since the geometries were moved up earlier the base movement
// on the y axis should have 4 subtracted to not move up or down
moveP1.set(geoP1.getLocalTranslation().subtract(0, 4, 0));
moveP2.set(geoP2.getLocalTranslation().subtract(0, 4, 0));
//moveP1.add(3,3,3);
if (leftP1) {
moveP1.addLocal(-50, 0, 0);
System.out.println(“ULeftP1”);
}
if (rightP1) {
moveP1.addLocal(50, 0, 0);
System.out.println(“URightP1”);
}
if (leftP2) {
moveP2.addLocal(-50, 0, 0);
System.out.println(“ULeftP2”);
}
if (rightP2) {
moveP2.addLocal(50, 0, 0);
System.out.println(“URightP2”);
}
if (nodeP1.getLocalTranslation().getZ() != nodeStage.getLocalTranslation().getX()) {
moveP1.addLocal(0, 0, 5 * (geoStage.getLocalTranslation().getX() - nodeP1.getLocalTranslation().getZ()));
}
if (nodeP2.getLocalTranslation().getZ() != nodeStage.getLocalTranslation().getX()) {
moveP2.addLocal(0, 0, 5 * (geoStage.getLocalTranslation().getX() - nodeP2.getLocalTranslation().getZ()));
}
phyP1.setWalkDirection(moveP1);
//geoArmP1.setLocalTranslation(2.0f, 6f, 3.0f);
phyP2.setWalkDirection(moveP2);
}@Override
public void cleanup() {
super.cleanup();
//TODO: clean up what you initialized in the initialize method,
//e.g. remove all spatials from rootNode
//this is called on the OpenGL thread after the AppState has been detached
}
}[/java]
Main.java
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
/**
-
test
-
@author normenhansen
*/
public class Main extends SimpleApplication {public static void main(String[] args) {
Main app = new Main();
app.start();
}@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
//flyCam.setMoveSpeed(50);
GameRunningAppState playState=new GameRunningAppState();
stateManager.attach(playState);
}@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}[/java]