org.xguzm.pathfinding.grid.finders.AStarGridFinder Java Examples

The following examples show how to use org.xguzm.pathfinding.grid.finders.AStarGridFinder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public MyPathFinder(final Map map) {
	linkedMap = map;
	navGrid = linkedMap.getNavLayer().navGrid;
	gridFinderOptions = new GridFinderOptions();
	gridFinderOptions.allowDiagonal = false;
	aStarGridFinder = new AStarGridFinder<GridCell>(GridCell.class, gridFinderOptions);
}
 
Example #2
Source File: TmxMapLoadingTest.java    From pathfinding with Apache License 2.0 5 votes vote down vote up
@Override
public void create() {

    TiledMap map = new NavTmxMapLoader(new ClassPathResolver()).load(tmxFile);
    AStarGridFinder<GridCell> finder = new AStarGridFinder<GridCell>(GridCell.class);

    NavigationTiledMapLayer nav = (NavigationTiledMapLayer)map.getLayers().get("navigation");

    path = finder.findPath(0, 0, 9, 9, nav);
    loaded = true;
}
 
Example #3
Source File: MapHelpers.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Create path for targeting (used for throwing weapons).
 *
 * @param start Starting cell
 * @param end   Where they're throwing to
 */
void createTargetingPath(Vector2 start, Vector2 end) {
  Map map = WorldManager.world.getCurrentMap();

  Vector2 oldTarget;
  GridCell[][] cells = new GridCell[map.width][map.height];

  for (int x = 0; x < map.width; x++) {
    for (int y = 0; y < map.height; y++) {
      boolean canTarget = cellExists(new Vector2(x, y))
          && !getCell(x, y).isWall()
          && !getCell(x, y).isNothing()
          && !getCell(x, y).hidden;

      cells[x][y] = new GridCell(x, y, canTarget);
    }
  }

  NavigationGrid<GridCell> grid = new NavigationGrid<>(cells, false);
  AStarGridFinder<GridCell> finder = new AStarGridFinder<>(GridCell.class);

  PlayerComponent playerDetails = ComponentMappers.player.get(WorldManager.player);

  if (playerDetails.target == null) {
    oldTarget = null;
    playerDetails.target = start.cpy().add(end);
  } else {
    oldTarget = playerDetails.target.cpy();
    playerDetails.target = playerDetails.target.add(end);
  }

  playerDetails.path = finder.findPath(
      (int) start.x, (int) start.y,
      (int) playerDetails.target.x, (int) playerDetails.target.y, grid
  );

  AttributesComponent playerAttributes = ComponentMappers.attributes.get(WorldManager.player);
  int maxDistance = (playerAttributes.strength <= 4 ? 4 : playerAttributes.strength);

  if (playerDetails.path == null || playerDetails.path.size() > maxDistance) {
    playerDetails.target = oldTarget;

    if (playerDetails.target != null) {
      playerDetails.path = finder.findPath(
          (int) start.x, (int) start.y,
          (int) playerDetails.target.x, (int) playerDetails.target.y, grid
      );
    }
  }
}
 
Example #4
Source File: MapHelpers.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Create a path for looking around.
 *
 * @param start Start position
 * @param end   End position
 */
public void createLookingPath(Vector2 start, Vector2 end, boolean careAboutWalls) {
  Map map = WorldManager.world.getCurrentMap();

  Vector2 oldTarget;
  GridCell[][] cells = new GridCell[map.width][map.height];

  for (int x = 0; x < map.width; x++) {
    for (int y = 0; y < map.height; y++) {
      boolean canTarget;

      if (careAboutWalls) {
        canTarget = cellExists(new Vector2(x, y))
            && !getCell(x, y).hidden
            && !getCell(x, y).isWall();
      } else {
        canTarget = cellExists(new Vector2(x, y)) && !getCell(x, y).hidden;
      }

      cells[x][y] = new GridCell(x, y, canTarget);
    }
  }

  NavigationGrid<GridCell> grid = new NavigationGrid<>(cells, false);
  AStarGridFinder<GridCell> finder = new AStarGridFinder<>(GridCell.class);

  PlayerComponent playerDetails = ComponentMappers.player.get(WorldManager.player);

  if (playerDetails.target == null) {
    oldTarget = null;
    playerDetails.target = start.cpy().add(end);
  } else {
    oldTarget = playerDetails.target.cpy();
    playerDetails.target = playerDetails.target.add(end);
  }

  playerDetails.path = finder.findPath(
      (int) start.x, (int) start.y,
      (int) playerDetails.target.x, (int) playerDetails.target.y, grid
  );

  if (playerDetails.path == null) {
    playerDetails.target = oldTarget;

    if (playerDetails.target != null) {
      playerDetails.path = finder.findPath(
          (int) start.x, (int) start.y,
          (int) playerDetails.target.x, (int) playerDetails.target.y, grid
      );
    }
  }
}
 
Example #5
Source File: MyPathFinder.java    From Norii with Apache License 2.0 4 votes vote down vote up
public AStarGridFinder<GridCell> getFinder() {
	return aStarGridFinder;
}