ShootingStars/ShootingTest/src/mygame/WeaponProjectileControl.java

104 lines
3.4 KiB
Java

package mygame;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
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.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.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
/**
*
* @author Snowsun
*/
public class WeaponProjectileControl extends AbstractControl implements ActionListener{
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;
this.rootNode = rootNode;
this.inputManager = inputManager;
initMappings();
}
@Override
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
}
@Override
protected void controlUpdate(float tpf) {
if(player.getControl(PlayerShootControl.class).getIsShooting()) {
shoot();
} else {
rootNode.detachChild(spatial);
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
public Control cloneForSpatial(Spatial spatial) {
WeaponProjectileControl control = new WeaponProjectileControl(assetManager, rootNode, inputManager);
spatial.addControl(control);
return control;
}
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("PLAYER_Mouse_Left_Click") && isPressed) {
rootNode.attachChild(spatial);
} else if (name.equals("PLAYER_Mouse_Left_Click") && !isPressed) {
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"});
}
/*
* Fires a projectile
*/
private void shoot() {
spatial.setLocalTranslation(location.add(0, 0, -4));
Vector3f vectorDifference = new Vector3f(player.getLocalTranslation().subtract(player.getWorldTranslation()));
spatial.setLocalTranslation(vectorDifference.addLocal(player.getLocalTranslation()));
Quaternion worldDiff = new Quaternion(player.getLocalRotation().subtract(player.getWorldRotation()));
spatial.setLocalRotation(worldDiff.addLocal(player.getLocalRotation()));
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);
}
}