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.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 * @author Ziver koc * */ 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, Vector2i pos) { super(l); unitNode = new Node("unit"); unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY())); setPos(pos.getX(), pos.getY()); } 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; } /** * Changes the pos of the unit in the map * * @param x The x pos to move to * @param y The y pos to move to */ public void setPos(int x, int y) { if(oldPos!=null) { InGameState.getMap().removePos(oldPos.getX(), oldPos.getY()); } 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) { path = (LinkedList) new AStar().startSearch(oldPos,new Vector2i(x,y)); if(path != null && !path.isEmpty()){ AStarNode temp = path.poll(); moveTo = Map.getPixelByPos(temp.getX(), temp.getY()); setPos(temp.getX(), temp.getY()); } } /** * 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()); Vector3f lastRotation = new Vector3f(0, 0, 0); //The rotation animation is done here. 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()); //The moving is done here. 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(Math.abs(moveTo.getX() - unitNode.getLocation().getX()) < 2 && Math.abs(moveTo.getY() - unitNode.getLocation().getY())< 2 ){ if(path != null && !path.isEmpty()){ AStarNode temp = path.poll(); if(InGameState.getMap().isPosEmpty(temp.getX(), temp.getY())){ moveTo = Map.getPixelByPos(temp.getX(), temp.getY()); setPos(temp.getX(), temp.getY()); } else if(!path.isEmpty()){ move(path.getLast().getX(), path.getLast().getY()); } } else{ moveTo = null; } } } } }