FIXED ASTAR NOW WORKING!!!!!!!
This commit is contained in:
parent
acde8264af
commit
5e17bdd5e5
10 changed files with 592 additions and 19 deletions
|
|
@ -7,7 +7,6 @@ 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;
|
||||
|
|
@ -18,6 +17,7 @@ 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{
|
||||
|
|
@ -61,12 +61,15 @@ public abstract class Unit extends GameEntity{
|
|||
return unitNode;
|
||||
}
|
||||
/**
|
||||
* Sets the pos of the unit whitout removing it from the old
|
||||
* 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);
|
||||
}
|
||||
|
|
@ -78,24 +81,22 @@ public abstract class Unit extends GameEntity{
|
|||
* @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());
|
||||
path = (LinkedList<AStarNode>) 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());
|
||||
}
|
||||
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) {
|
||||
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 lastRotation = new Vector3f(0, 0, 0);
|
||||
|
||||
//The rotation animation is done here.
|
||||
|
|
@ -131,11 +132,17 @@ public abstract class Unit extends GameEntity{
|
|||
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());
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue