Hmm, I wonder what I'm doing wrong then.
Here is my entire TileBasedMap class:
Code:
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.pathfinding.PathFindingContext;
import org.newdawn.slick.util.pathfinding.TileBasedMap;
public class RTSMap implements TileBasedMap
{ TiledMap map;
boolean[][] collisionmap;
float[][] costMap;
public RTSMap(TiledMap m)
{ map=m;
collisionmap=new boolean[m.getWidth()][m.getHeight()];
for(int x=0; x<m.getWidth(); x++)
{
for(int y=0; y<m.getHeight(); y++)
{
int property=Integer.parseInt(m.getTileProperty(m.getTileId(x, y, 1), "Passable", "1"));
if(property==1)
collisionmap[x][y]=true;
}
}
}
@Override
public boolean blocked(PathFindingContext pathfinder, int x, int y)
{
System.out.println(!(collisionmap[x][y]));
return !(collisionmap[x][y]);
}
@Override
public float getCost(PathFindingContext pathfinder, int x, int y)
{
return 1;
}
@Override
public int getHeightInTiles()
{
return map.getHeight();
}
@Override
public int getWidthInTiles()
{
return map.getWidth();
}
@Override
public void pathFinderVisited(int x, int y)
{
}
}
And I know for a fact using debug data that the collision map parsing works just fine.
The System.out.println in blocked() reveals that the AstarPathfinder looks at one tile, finds it to not be blocked and then returns null.
Here is some code for my Mover:
Code:
//Find a path between you and the pixel coordinate target "target"
public void setTarget(Vector2f target)
{
this.target = target;
fitTargettoGrid();
findPath();
currentStep=0;
}
//Convert pixel coordinates of "target" to grid coordinates.
public void fitTargettoGrid()
{
int currentTileX=(int) (target.x/Game.currentMap.getTileWidth());
int currentTileY=(int) (target.y/Game.currentMap.getTileHeight());
target=new Vector2f(currentTileX,currentTileY);
}
//Fit your own coordinates to grid coordinates.
public void findGridPosition()
{
int currentTileX=(int) (parent.getMoveSystem().pos.x/Game.currentMap.getTileWidth());
int currentTileY=(int) (parent.getMoveSystem().pos.y/Game.currentMap.getTileHeight());
gridPosition=new Vector2f(currentTileX,currentTileY);
}
//Find a path from you to the target.
public void findPath()
{ findGridPosition();
pathtoTarget=pathfinder.findPath((Mover)this, (int)gridPosition.getX(), (int)gridPosition.getY(), (int)target.getX(), (int)target.getY());
if(pathtoTarget==null)
{
System.out.println("ITS NULL");
}
}