Hi, I’m having a lot of problems trying to implement pathfinding in my game, I’m using libgdx and tiled to do the map, but it seems that TiledMapTileLayer class don’t have any methods to get cell’s x and y coordinates and I don’t know if I’m doing right… so far I’ve got this:
package TLM.game;
import java.util.List;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.math.Vector2;
public class AI_Pathfinding
{
Map_Base map;
Vector2 pos;
Vector2 finalPos;
Vector2 currentPos;
Cell currentCell;
float g;
float h;
float f;
List<Cell> openList;
List<Cell> closedList;
public AI_Pathfinding(Map_Base map)
{
this.map = map;
pos = new Vector2(0,0);
finalPos = new Vector2(0,0);
}
public void createPath(Entity_Base e,float finalx, float finaly)
{
pos.set(e.pos.x / 32,e.pos.y / 32);
currentPos.set(pos.x,pos.y);
finalPos.set(finalx / 32, finaly / 32);
currentCell = map.collisionLayer.getCell((int)pos.x,(int)pos.y);
openList.add(currentCell);
/*
* Moral da historia pra mim fazer depois:
* adicionar todos os 8 quadrados adjacentes para a open list.
* escolher o quadrado com menor F e adicionar ele para o ClosedList.
*
*/
//right
if(!map.collisionLayer.getCell(((int)currentPos.x) +1,(int)currentPos.y).getTile().getProperties().containsKey("Solid"))
{
openList.add(map.collisionLayer.getCell(((int)currentPos.x) +1,(int)currentPos.y));
}
//left
if(!map.collisionLayer.getCell(((int)currentPos.x) -1,(int)currentPos.y).getTile().getProperties().containsKey("Solid"))
{
openList.add(map.collisionLayer.getCell(((int)currentPos.x) -1,(int)currentPos.y));
}
//top
if(!map.collisionLayer.getCell(((int)currentPos.x),((int)currentPos.y)-1).getTile().getProperties().containsKey("Solid"))
{
openList.add(map.collisionLayer.getCell(((int)currentPos.x),((int)currentPos.y)-1));
}
//bottom
if(!map.collisionLayer.getCell(((int)currentPos.x) +1,((int)currentPos.y)+1).getTile().getProperties().containsKey("Solid"))
{
openList.add(map.collisionLayer.getCell(((int)currentPos.x) +1,((int)currentPos.y)+1));
}
for(int i = 0; i < openList.size() ; i++)
{
}
}
}
I’m on the right way? someone has code examples of pathfinding with tiled and libgdx? any tips? ty