Greetings all,
I am new to JMonkey and I am trying to make a custom control. I have made a copy of the program I am trying to write and reduced it down in size. The basics of what the program is trying to be is a side scrolling fighting game, like mortal combat or the like, with boxes acting as place holders for actual 3d models. There are two boxes a blue one and the red one. The blue box should apporoch the red box and then attack it. I had the same kind of controls working for the update loop and that worked quite nicely, but now I would like to move that logic to a custom control. I am having issues with the control interacting with the object it is assigned to that being the blue box. I thought that “this.getWorldTranslation()” would work, I have also tried super but that did not know either. I think this should be easy for someone who accutally knows what they are doing but I clearly do not. In the custom control “LogingControl” the variable “b” is the state variable 1 is idle and moves the box inside attacking distance of the other box. 2 is the attack move, a headbut in the case of the box. 3 is a pain move that the box will react to being hit. If someone could also tell me how to make a reference of the red box so that the control attached to the blue box can tell the red box to use the pain move that would be great. I will attach my code.
- package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import java.util.ResourceBundle.Control;
public class SSF1 extends SimpleApplication {
Geometry blue = null;
Geometry red = null;
public float tot = 0;
public static void main(String[] args){
final SSF1 app = new SSF1();
app.start();
}
@Override
public void simpleInitApp() {
/** create a blue box at coordinates (1,-1,1) */
Box box1 = new Box( Vector3f.ZERO, 1f,2f,.5f);
blue = new Geometry("Box", box1);
Material mat1 = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.Blue);
blue.setMaterial(mat1);
blue.move(-5,0,-3);
/** create a red box straight above the blue one at (1,3,1) */
Box box2 = new Box( Vector3f.ZERO, 1f,2f,.5f);
red = new Geometry("Box", box2);
Material mat2 = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Red);
red.setMaterial(mat2);
red.move(5,0,-3);
rootNode.attachChild(blue);
rootNode.attachChild(red);
blue.lookAt(red.getWorldTranslation(), new Vector3f(0,1,0) );
red.lookAt(blue.getWorldTranslation(), new Vector3f(0,1,0) );
}
/* This is the update loop */
@Override
public void simpleUpdate(float tpf) {
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText helloText = new BitmapText(guiFont, false);
helloText.setSize(guiFont.getCharSet().getRenderedSize());
//helloText.setText( Float.toString(tot) );
helloText.setText(Float.toString(blue.getWorldTranslation().distance(red.getWorldTranslation())));
helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
guiNode.attachChild(helloText);
}
}
class LogicControl extends AbstractControl {
float t = 0;
public float tot = 0;
boolean dir = true;
int b = 0;
public LogicControl(){} // empty serialization constructor
@Override
protected void controlUpdate(float tpf){
if ( b == 0 ) {
if ( this.getWorldTranslation().distance(super.red.getWorldTranslation()) >= 2f) {
this.setLocalTranslation(new Vector3f( (this.getLocalTranslation().getX() + .02f), (this.getLocalTranslation().getY()) , (this.getLocalTranslation().getZ() )));
} else {
b = 1;
}
} else if ( b == 1 ) {
if ( (tot <= .7f) && (dir==true) ){
t = 1.7f * tpf;
tot += t;
this.rotate(t,0f,0f);
} else if(tot > 0) {
dir = false;
t = -1.7f * tpf;
if ( tot < 0 ) { t = 0; }
tot += t;
this.rotate(t,0f,0f);
} else if ( tot <= 0 ) {
b = 0;
dir = true;
tot = 0;
this.rotate( ( this.getLocalRotation().getX() * -1 ),0f,0f);
}
} else if ( b == 2 ) {
if ( (tot >= -.7f) && (dir==true) ){
t = -1.7f * tpf;
tot += t;
} else if(tot < 0) {
dir = false;
t = 1.7f * tpf;
if ( tot > 0 ) { t = 0; }
tot += t;
this.rotate(t,0f,0f);
} else if ( tot >= 0 ) {
b = 0;
}
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp){
// Optional: rendering manipulation (for advanced users)
}
@Override
public LogicControl cloneForSpatial(Spatial spatial){
final LogicControl control = new LogicControl();
//Optional: use setters to copy userdata into the cloned control
// control.setIndex(i); // example
control.setSpatial(spatial);
return control;
}
}