package edu.ups.gamedev.player;
import java.net.MalformedURLException;
import com.jme.bounding.BoundingBox;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;
import com.jmex.physics.impl.ode.DynamicPhysicsNodeImpl;
import com.jmex.physics.impl.ode.OdePhysicsSpace;
import com.jmex.physics.impl.ode.StaticPhysicsNodeImpl;
import edu.ups.gamedev.game.TankGame;
import edu.ups.gamedev.net.message.TankSynchronizeCreateMessage;
import edu.ups.gamedev.scene.Collidable;
import edu.ups.gamedev.scene.Model;
import edu.ups.gamedev.weapons.Gadget;
import edu.ups.gamedev.weapons.Weapon;
/**
* Describes a drivable <code>Tank</code>.
*
* @author stefan
* @version $Revision$, $Date$
*/
public class Tank extends StaticPhysicsNodeImpl implements Collidable{
private static final long serialVersionUID = 1L;
public static final String LIGHT_TANK = "resources/models/light_tank.obj";
public static final String MEDIUM_TANK = "resources/models/med_tank.obj";
public static final String HEAVY_TANK = "resources/models/heavy_tank.obj";
protected Node model;
protected String tankType;
protected Turret turret;
protected ColorRGBA color;
protected Weapon primaryWeapon;
protected Weapon secondaryWeapon;
protected Gadget gadget;
protected Health health;
// orientation
// coordinate system
protected final Vector3f leftVector = new Vector3f();
protected final Vector3f upVector = new Vector3f();
protected final Vector3f forwardVector = new Vector3f();
protected final Vector3f[] axes = { leftVector, upVector, forwardVector };
protected final float[] worldAngles = new float[3];
protected final float[] velocityAngles = new float[3];
protected final Quaternion worldRotation = new Quaternion();
protected final Quaternion velocityRotation = new Quaternion();
/**
* Constructor that takes the type of tank. In reality, this could be the file name of whatever model you wanted to use for the <code>Tank</code>.
*
* @param type
* type of tank to create
* @throws MalformedURLException
*/
public Tank(String type) throws MalformedURLException {
this(type, ColorRGBA.gray.clone());
}
/**
* Constructor that takes a <code>TankSynchronizeCreateMessage</code> as a parameter. This simplifies the networking code by allowing JGN to simply pass the creation message to the
* <code>Tank</code> constructor. It is also beneficial because it makes it easy to figure out what all information the message needs to contain to fully reconstruct a <code>
* Tank</code>.
*
* @param msg
* <code>TankSynchronizeCreateMessage</code> that contains information about the <code>Tank</code> to create
* @throws MalformedURLException
*/
public Tank(TankSynchronizeCreateMessage msg) throws MalformedURLException {
this(msg.getTankType(), msg.getColor());
}
/**
* Constructor that takes the type of tank and its color. In reality, <code>
* type</code> could be the file name of whatever model you wanted to use for the <code>Tank</code>.
*
* @param type
* type of <code>Tank</code> to create
* @param color
* color of this <code>Tank</code>
* @throws MalformedURLException
*/
public Tank(String type, ColorRGBA color) throws MalformedURLException {
super((OdePhysicsSpace) TankGame.PHYSICS.getPhysicsSpace());
this.color = color;
this.tankType = type;
this.health = new Health();
model = Model.getModelNode(type);
this.attachChild(model);
turret = new Turret(this.getLocalTranslation());
this.attachChild(turret);
// TODO un-hardcode turret position
turret.setLocalTranslation(0, 10, 0);
turret.computeOrigin();
this.generatePhysicsGeometry(true);
setActive(true);
MaterialState material = DisplaySystem.getDisplaySystem().getRenderer()
.createMaterialState(); // use a factory method to generate a new material
material.setDiffuse(color); // set it's diffuse color (when light hits it) to red
setRenderState(material);
//this.setMass(35000);
}
public boolean canCollideWith(Collidable object){
return true;
}
public void firePrimaryWeapon() {
if (primaryWeapon != null) {
primaryWeapon.fire(turret.ray, new Vector3f(0, 0, 0),
TankGame.GAMESTATE.getRoot());
}
}
public void fireSecondaryWeapon() {
if (secondaryWeapon != null) {
secondaryWeapon.fire(turret.ray, new Vector3f(0, 0, 0),
TankGame.GAMESTATE.getRoot());
}
}
/**
* Returns the color of this <code>Tank</code>.
*
* @return the color
*/
public ColorRGBA getColor() {
return color;
}
public String getTankType() {
return tankType;
}
public Node getModel() {
return model;
}
public Turret getTurret() {
return turret;
}
public Weapon getPrimaryWeapon() {
return primaryWeapon;
}
public Weapon getSecondaryWeapon() {
return secondaryWeapon;
}
public Gadget getGadget() {
return gadget;
}
/**
* Sets the color of this <code>Tank</code>
*
* @param color
* the color to set
*/
public void setColor(ColorRGBA color) {
this.color = color;
}
public void setPrimaryWeapon(Weapon weapon) {
primaryWeapon = weapon;
}
public void setSecondaryWeapon(Weapon weapon) {
secondaryWeapon = weapon;
}
protected Health getHealth() {
return health;
}
protected void setHealth(Health health) {
this.health = health;
}
public void doDamage(int damage){
this.health.setReamainingHealth(this.health.getRemainingHealth() - damage);
if (this.health.isDead())
{
//do something
TankGame.NETWORK.unregister(this);
TankGame.GAME.lock();
this.removeFromParent();
TankGame.GAME.unlock();
System.out.println("ur ded");
/*TankGame.GAME.lock();
TankGame.GAMESTATE.respawn();
TankGame.GAME.unlock();*/
}
}
}