org.xguzm.pathfinding.grid.NavigationGrid Java Examples

The following examples show how to use org.xguzm.pathfinding.grid.NavigationGrid. 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: AStarFinderTest.java    From pathfinding with Apache License 2.0 6 votes vote down vote up
@Test
public void autoAssignXYMapTest(){
	System.out.println("\nRunning AStarFinderTest.autoAssignXYMapTest");
	NavigationGrid<GridCell> grid = NavGraphFactory.getAutoAssignedGridCellMap();
	GridCell c = grid.getCell(3, 1);
	
	assertTrue("GridCell at Grid(3,2) didn't have it's x and y auto assigned correctly", c.x == 3 && c.y == 1);
	
	GridCell start = grid.getCell(2, 0), end = grid.getCell(4, 7);
	
	//test orthogonal movement only
	opt.allowDiagonal = false;
	
	List<GridCell> path = finder.findPath(start,  end,  grid);
	assertNotNull(String.format("No path found from %s to %s for orthogonal movement", start, end), path);
}
 
Example #2
Source File: NavGraphFactory.java    From pathfinding with Apache License 2.0 5 votes vote down vote up
public static NavigationGrid<GridCell> getGridCellMap() {
	GridCell[][] cells = new GridCell[navCells[0].length][navCells.length];


	for (int y = navCells.length - 1; y >= 0; y--) 
		for (int x = 0; x < navCells[0].length; x++){
			int invY = navCells.length - 1 - y;
			GridCell cell = new GridCell(x, invY, navCells[y][x] > 0);
			cells[x][invY] = cell;
		}

	return new NavigationGrid<GridCell>(cells, false);

}
 
Example #3
Source File: NavGraphFactory.java    From pathfinding with Apache License 2.0 5 votes vote down vote up
public static NavigationGrid<GridCell> getAutoAssignedGridCellMap() {
	GridCell[][] cells = new GridCell[navCells[0].length][navCells.length];


	for (int y = navCells.length - 1; y >= 0; y--) 
		for (int x = 0; x < navCells[0].length; x++){
			int invY = navCells.length - 1 - y;
			GridCell cell = new GridCell(navCells[y][x] > 0);
			cells[x][invY] = cell;
		}

	return new NavigationGrid<GridCell>(cells, true);

}
 
Example #4
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 #5
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 #6
Source File: MyNavigationTiledMapLayer.java    From Norii with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public MyNavigationTiledMapLayer(final GridCell[][] nodes) {
	navGrid = new NavigationGrid<GridCell>(nodes);
}
 
Example #7
Source File: NavigationTiledMapLayer.java    From pathfinding with Apache License 2.0 4 votes vote down vote up
public NavigationTiledMapLayer(GridCell[][] nodes){
	navGrid = new NavigationGrid<GridCell>(nodes);
}