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

147 lines
3.8 KiB
Java
Raw Normal View History

package ei.game.scene.units;
2007-04-04 18:16:37 +00:00
2007-04-16 15:52:24 +00:00
import java.util.LinkedList;
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-16 15:52:24 +00:00
import ei.game.algo.AStar;
import ei.game.algo.AStarNode;
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;
2007-04-16 15:52:24 +00:00
private LinkedList<AStarNode> path;
/**
* Creates a empty unit
*
* @param l The max life of the unit
*/
2007-04-16 16:13:44 +00:00
public Unit(int l, Vector2i pos) {
super(l);
2007-04-16 11:34:57 +00:00
unitNode = new Node("unit");
2007-04-16 16:13:44 +00:00
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY()));
setPos(pos.getX(), pos.getY());
}
2007-04-16 16:13:44 +00:00
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);
2007-04-16 16:13:44 +00:00
InGameState.getMap().setPos(this, x, y);
2007-04-04 16:28:57 +00:00
}
/**
* 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 15:52:24 +00:00
path = (LinkedList) new AStar().findPath(new AStarNode(oldPos,null), new AStarNode(new Vector2i(x,y),null));
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());
2007-04-16 15:59:30 +00:00
System.out.println("going to: "+moveVect);
System.out.println("from: "+currentVect);
Vector3f lastRotation = new Vector3f(0, 0, 0);
//The rotation animation is done here.
2007-04-16 13:25:25 +00:00
if(moveVect.getX() < currentVect.getX()) {
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 90));
2007-04-16 15:59:30 +00:00
}
if(moveVect.getX() > currentVect.getX()){
2007-04-16 13:25:25 +00:00
unitNode.get("Tank").setRotation(new Vector3f(0, 0, -90));
2007-04-16 15:59:30 +00:00
}
if(moveVect.getY() < currentVect.getY()){
2007-04-16 13:25:25 +00:00
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 180));
2007-04-16 15:59:30 +00:00
}
if(moveVect.getY() > currentVect.getY()) {
2007-04-16 13:25:25 +00:00
unitNode.get("Tank").setRotation(new Vector3f(0, 0, 0));
2007-04-16 15:59:30 +00:00
}
2007-04-16 13:25:25 +00:00
//System.out.println(unitNode.get("Tank").getRotation());
2007-04-16 15:59:30 +00:00
//The moving is done here.
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()) {
2007-04-16 15:52:24 +00:00
if(!path.isEmpty()){
Vector2i temp = path.poll().getPos();
moveTo = Map.getPixelByPos(temp.getX(), temp.getY());
}
else{
moveTo = null;
}
}
}
}
}