evil-inside/src/ei/game/scene/units/Unit.java

129 lines
3.2 KiB
Java
Raw Normal View History

package ei.game.scene.units;
2007-04-04 18:16:37 +00:00
import ei.engine.math.Vector2f;
2007-04-04 14:44:25 +00:00
import ei.engine.math.Vector2i;
2007-04-16 13:25:25 +00:00
import ei.engine.math.Vector3f;
2007-04-16 11:34:57 +00:00
import ei.engine.scene.Node;
2007-04-04 14:44:25 +00:00
import ei.engine.scene.Sprite;
2007-04-04 16:28:57 +00:00
import ei.game.gamestate.InGameState;
import ei.game.scene.GameEntity;
2007-04-16 13:25:25 +00:00
import ei.game.scene.Map;
2007-04-04 14:44:25 +00:00
import ei.game.scene.weapons.Weapon;
2007-04-16 11:34:57 +00:00
/**
* The Unit class, handles the units in the game.
* @author Jesper Lundin
*
*/
public abstract class Unit extends GameEntity{
// The texture
2007-04-16 11:34:57 +00:00
private Node unitNode;
// The wepon of the unit
2007-04-04 14:44:25 +00:00
private Weapon weapon;
// Som temp pos for moving the unit
2007-04-04 16:28:57 +00:00
private Vector2i oldPos;
private Vector2f moveTo;
/**
* Creates a empty unit
*
* @param l The max life of the unit
*/
public Unit(int l) {
super(l);
2007-04-16 11:34:57 +00:00
unitNode = new Node("unit");
2007-04-04 18:15:11 +00:00
setPos(0, 0);
}
2007-04-16 11:34:57 +00:00
public void setSelected(boolean b) {
super.setSelected(b);
if(b) {
unitNode.add(getSelection());
}
else{
unitNode.remove(getSelection());
}
}
2007-04-04 14:44:25 +00:00
/**
* Returns the sprite for the unit
*
* @return The sprite for the unit
*/
2007-04-16 11:34:57 +00:00
public Node getNode(){
return unitNode;
2007-04-04 14:44:25 +00:00
}
/**
* Sets the pos of the unit whitout removing it from the old
*
* @param x The x pos to move to
* @param y The y pos to move to
*/
2007-04-04 16:28:57 +00:00
public void setPos(int x, int y) {
oldPos = new Vector2i(x, y);
InGameState.getMap().setPos(this, x, y);
}
/**
* Moving a unit to the given pos
*
* @param x The x pos to move to
* @param y The y pos to move to
*/
2007-04-04 16:28:57 +00:00
public void move(int x, int y) {
if(oldPos!=null) {
InGameState.getMap().removePos(oldPos.getX(), oldPos.getY());
}
setPos(x, y);
2007-04-16 13:25:25 +00:00
moveTo = Map.getPixelByPos((int)x, (int)y);
2007-04-04 16:28:57 +00:00
oldPos = new Vector2i(x, y);
}
/**
* Updating the unit
*/
public void update() {
if(moveTo!=null) {
2007-04-16 13:25:25 +00:00
Vector2i moveVect = Map.getPosByPixel(moveTo.getX(), moveTo.getY());
Vector2i currentVect = Map.getPosByPixel(unitNode.getLocation().getX(), unitNode.getLocation().getY());
//System.out.println("going to: "+moveVect);
//System.out.println("from: "+currentVect);
Vector3f currentRot = null;
if(moveVect.getX() < currentVect.getX()) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 90));
}
if(moveVect.getX() > currentVect.getX()) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -90));
}
if(moveVect.getY() < currentVect.getY()) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 180));
}
if(moveVect.getY() > currentVect.getY()) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 0));
}
//System.out.println(unitNode.get("Tank").getRotation());
2007-04-16 11:34:57 +00:00
if(moveTo.getX() > unitNode.getLocation().getX()) {
unitNode.getLocation().add(1.5f, 0f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getX() < unitNode.getLocation().getX()) {
unitNode.getLocation().add(-1.5f, 0f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getY() > unitNode.getLocation().getY()) {
unitNode.getLocation().add(0f, 1.5f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getY() < unitNode.getLocation().getY()) {
unitNode.getLocation().add(0f, -1.5f, 0f);
}
2007-04-16 11:34:57 +00:00
if(moveTo.getX() == unitNode.getLocation().getX()
&& moveTo.getY() == unitNode.getLocation().getY()) {
moveTo = null;
}
}
}
}