package ei.game.scene.units; import java.util.LinkedList; import ei.engine.math.Vector2f; import ei.engine.math.Vector2i; import ei.engine.math.Vector3f; import ei.engine.scene.Node; import ei.engine.scene.Sprite; import ei.game.algo.AStar; import ei.game.algo.AStarNode; import ei.game.gamestate.InGameState; import ei.game.scene.GameEntity; import ei.game.scene.Map; import ei.game.scene.weapons.Weapon; /** * The Unit class, handles the units in the game. * @author Jesper Lundin * */ public abstract class Unit extends GameEntity{ // The texture private Node unitNode; // The wepon of the unit private Weapon weapon; // Som temp pos for moving the unit private Vector2i oldPos; private Vector2f moveTo; private LinkedList path; /** * Creates a empty unit * * @param l The max life of the unit */ public Unit(int l) { super(l); unitNode = new Node("unit"); setPos(0, 0); } public void setSelected(boolean b) { super.setSelected(b); if(b) { unitNode.add(getSelection()); } else{ unitNode.remove(getSelection()); } } /** * Returns the sprite for the unit * * @return The sprite for the unit */ public Node getNode(){ return unitNode; } /** * 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 */ 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 */ public void move(int x, int y) { if(oldPos!=null) { InGameState.getMap().removePos(oldPos.getX(), oldPos.getY()); } setPos(x, y); path = (LinkedList) new AStar().findPath(new AStarNode(oldPos,null), new AStarNode(new Vector2i(x,y),null)); moveTo = Map.getPixelByPos((int)x, (int)y); oldPos = new Vector2i(x, y); } /** * Updating the unit */ public void update() { if(moveTo!=null) { 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()); if(moveTo.getX() > unitNode.getLocation().getX()) { unitNode.getLocation().add(1.5f, 0f, 0f); } if(moveTo.getX() < unitNode.getLocation().getX()) { unitNode.getLocation().add(-1.5f, 0f, 0f); } if(moveTo.getY() > unitNode.getLocation().getY()) { unitNode.getLocation().add(0f, 1.5f, 0f); } if(moveTo.getY() < unitNode.getLocation().getY()) { unitNode.getLocation().add(0f, -1.5f, 0f); } if(moveTo.getX() == unitNode.getLocation().getX() && moveTo.getY() == unitNode.getLocation().getY()) { if(!path.isEmpty()){ Vector2i temp = path.poll().getPos(); moveTo = Map.getPixelByPos(temp.getX(), temp.getY()); } else{ moveTo = null; } } } } }