Added Astar algo
This commit is contained in:
parent
79ae1d0dcc
commit
148c0c739f
4 changed files with 258 additions and 2 deletions
|
|
@ -1,11 +1,15 @@
|
|||
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;
|
||||
|
|
@ -24,6 +28,7 @@ public abstract class Unit extends GameEntity{
|
|||
// Som temp pos for moving the unit
|
||||
private Vector2i oldPos;
|
||||
private Vector2f moveTo;
|
||||
private LinkedList<AStarNode> path;
|
||||
|
||||
/**
|
||||
* Creates a empty unit
|
||||
|
|
@ -76,6 +81,7 @@ public abstract class Unit extends GameEntity{
|
|||
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);
|
||||
}
|
||||
|
|
@ -120,9 +126,14 @@ public abstract class Unit extends GameEntity{
|
|||
|
||||
if(moveTo.getX() == unitNode.getLocation().getX()
|
||||
&& moveTo.getY() == unitNode.getLocation().getY()) {
|
||||
moveTo = null;
|
||||
if(!path.isEmpty()){
|
||||
Vector2i temp = path.poll().getPos();
|
||||
moveTo = Map.getPixelByPos(temp.getX(), temp.getY());
|
||||
}
|
||||
else{
|
||||
moveTo = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue