Add WeaponProjectileCollisonControl and refactor WeaponProjectileControl

This commit is contained in:
Chris@Chris-HP 2013-05-17 13:47:41 +02:00
parent 068be09309
commit c60601f63e
3 changed files with 114 additions and 30 deletions

View File

@ -8,6 +8,7 @@ import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
@ -17,9 +18,14 @@ import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
private Spatial player;
private Spatial projectile;
private Spatial star;
private Spatial star2;
private PlayerMouseControl playerMouseControl;
private PlayerShootControl playerShootControl;
private WeaponProjectileControl weaponProjectileControl;
private WeaponProjectileCollisionControl weaponProjectileCollisionControl;
private Node starNode;
public Main() {
super(new StatsAppState(), new DebugKeysAppState());
@ -32,11 +38,37 @@ public class Main extends SimpleApplication {
@Override
public void simpleInitApp() {
starNode = new Node("Star Node");
rootNode.attachChild(starNode);
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
player = new Geometry("Player",b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
player.setMaterial(mat);
Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
star = new Geometry("Star",b2);
star.move(4f, 2f, 0);
Material mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat3.setColor("Color", ColorRGBA.Orange);
star.setMaterial(mat3);
starNode.attachChild(star);
Box b3 = new Box(Vector3f.ZERO, 1, 1, 1);
star2 = new Geometry("Star2",b3);
star2.move(-4f, 2f, 0);
Material mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat4.setColor("Color", ColorRGBA.Green);
star2.setMaterial(mat4);
starNode.attachChild(star2);
Box box = new Box(Vector3f.ZERO, 1, 1, 2);
projectile = new Geometry("Projectile", box);
projectile.setLocalTranslation(player.getLocalTranslation());
Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Red);
projectile.setMaterial(mat2);
inputManager.setCursorVisible(false);
playerMouseControl = new PlayerMouseControl(inputManager,cam);
@ -47,7 +79,11 @@ public class Main extends SimpleApplication {
player.addControl(playerShootControl);
weaponProjectileControl = new WeaponProjectileControl(assetManager, rootNode, inputManager);
player.addControl(weaponProjectileControl);
weaponProjectileControl.setPlayer(player);
projectile.addControl(weaponProjectileControl);
weaponProjectileCollisionControl = new WeaponProjectileCollisionControl(starNode);
projectile.addControl(weaponProjectileCollisionControl);
rootNode.attachChild(player);
}

View File

@ -0,0 +1,54 @@
package mygame;
import com.jme3.bounding.BoundingVolume;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
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 com.jme3.scene.control.Control;
/**
*
* @author Snowsun
*/
public class WeaponProjectileCollisionControl extends AbstractControl {
private Node starNode;
private BoundingVolume boundingVolume;
private CollisionResults results;
public WeaponProjectileCollisionControl(Node starNode) {
this.starNode = starNode;
results = new CollisionResults();
}
@Override
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
boundingVolume = spatial.getWorldBound();
}
@Override
protected void controlUpdate(float tpf) {
for (Spatial s : starNode.getChildren()) {
s.collideWith(boundingVolume, results);
if (results.size() > 0) {
starNode.detachChild(s);
System.out.println("What was hit? " + s.getName());
}
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
public Control cloneForSpatial(Spatial spatial) {
WeaponProjectileCollisionControl control = new WeaponProjectileCollisionControl(starNode);
spatial.addControl(control);
return control;
}
}

View File

@ -7,19 +7,15 @@ 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.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Box;
/**
*
@ -27,11 +23,11 @@ import com.jme3.scene.shape.Box;
*/
public class WeaponProjectileControl extends AbstractControl implements ActionListener{
private Spatial projectile;
private AssetManager assetManager;
private Node rootNode;
private Vector3f location;
private InputManager inputManager;
private Spatial player;
public WeaponProjectileControl(AssetManager assetManager, Node rootNode, InputManager inputManager) {
this.assetManager = assetManager;
@ -43,16 +39,14 @@ public class WeaponProjectileControl extends AbstractControl implements ActionLi
@Override
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
location = spatial.getLocalTranslation();
initProjectile();
}
@Override
protected void controlUpdate(float tpf) {
if(spatial.getControl(PlayerShootControl.class).getIsShooting()) {
if(player.getControl(PlayerShootControl.class).getIsShooting()) {
shoot();
} else {
rootNode.detachChild(projectile);
rootNode.detachChild(spatial);
}
}
@ -68,42 +62,42 @@ public class WeaponProjectileControl extends AbstractControl implements ActionLi
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("PLAYER_Mouse_Left_Click") && isPressed) {
rootNode.attachChild(projectile);
rootNode.attachChild(spatial);
} else if (name.equals("PLAYER_Mouse_Left_Click") && !isPressed) {
rootNode.detachChild(projectile);
rootNode.detachChild(spatial);
}
}
public void setPlayer(Spatial player) {
this.player = player;
location = player.getLocalTranslation();
}
public Spatial getPlayer() {
return this.player;
}
private void initMappings() {
inputManager.addMapping("PLAYER_Mouse_Left_Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT),
new KeyTrigger(KeyInput.KEY_DOWN));
inputManager.addListener(this, new String[]{"PLAYER_Mouse_Left_Click"});
}
private void initProjectile() {
Box box = new Box(Vector3f.ZERO, 1, 1, 2);
projectile = new Geometry("Projectile", box);
projectile.setLocalTranslation(spatial.getLocalTranslation());
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Red);
projectile.setMaterial(mat);
}
/*
* Fires a projectile
*/
private void shoot() {
projectile.setLocalTranslation(location.add(0, 0, -4));
spatial.setLocalTranslation(location.add(0, 0, -4));
Vector3f vectorDifference = new Vector3f(spatial.getLocalTranslation().subtract(spatial.getWorldTranslation()));
projectile.setLocalTranslation(vectorDifference.addLocal(spatial.getLocalTranslation()));
Vector3f vectorDifference = new Vector3f(player.getLocalTranslation().subtract(player.getWorldTranslation()));
spatial.setLocalTranslation(vectorDifference.addLocal(player.getLocalTranslation()));
Quaternion worldDiff = new Quaternion(spatial.getLocalRotation().subtract(spatial.getWorldRotation()));
projectile.setLocalRotation(worldDiff.addLocal(spatial.getLocalRotation()));
Quaternion worldDiff = new Quaternion(player.getLocalRotation().subtract(player.getWorldRotation()));
spatial.setLocalRotation(worldDiff.addLocal(player.getLocalRotation()));
projectile.move(spatial.getLocalRotation().getRotationColumn(2).mult(-3f));
projectile.move(spatial.getLocalRotation().getRotationColumn(1).mult(0f));
projectile.move(spatial.getLocalRotation().getRotationColumn(0).mult(0));
projectile.rotate(0f, FastMath.PI, 0);
spatial.move(player.getLocalRotation().getRotationColumn(2).mult(-3.5f));
// spatial.move(player.getLocalRotation().getRotationColumn(1).mult(0f));
// spatial.move(player.getLocalRotation().getRotationColumn(0).mult(0));
// spatial.rotate(0f, FastMath.PI, 0);
}
}