Search code examples
javalibgdxpath-findingtiled

Showing actor movement range on tiles


I am trying to write something that will take a X and Y coordinate and a value that represents the available movement points the selected actor has. It should then output a list of the reachable locations so that I can highlight those tiles when the actor is choosing where to move.

I managed to use a function from my pathfinding library (https://github.com/xaguzman/pathfinding) that gives me the neighbouring tiles of a tile as a List of grid cells. It can also check my Tile-map and see if the tile is walkable.

What I can't really get my head around is how I would be able to set this up so that it would run as many times as the movement points.

List<GridCell> neighbours;
NavigationTiledMapLayer navLayer;

public void getMovementPossibilities(int tileX, int tileY) {
    GridCell cell1;
    GridCell cell2;

    cell1 = navLayer.getCell(tileX, tileY);


    GridFinderOptions opt = new GridFinderOptions();
    opt.allowDiagonal = true;
    opt.dontCrossCorners = false;


    neighbours = navLayer.getNeighbors(cell1, opt);


    for (int i = 0; i < neighbours.size(); i++) {
        int nX = neighbours.get(i).getX();
        int nY = neighbours.get(i).getY();

        cell2 = navLayer.getCell(nX, nY);
        neighbours.addAll(navLayer.getNeighbors(cell2, opt));
    }
}

Solution

  • Sounds like a case for recursion. I can't see how you're keeping track of movement points but your current method finds all tiles 1 distance away. Inside this method, you need to recall the same method, but with each of those neighbours as your new start point and with the movement points reduced by 1.

    This in turn generates all the second neighbours, and then the method will be called recursively and so on...

    You will want a check at the top of the method so that if the distance points left are 0, it just immediately adds the current tile to neighbours and then returns (to stop the recursive chain going on forever).