org.xguzm.pathfinding.grid.GridCell Java Examples

The following examples show how to use org.xguzm.pathfinding.grid.GridCell. 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: UtilTest.java    From pathfinding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBacktrace() {
	
	ArrayList<GridCell> path = new ArrayList<GridCell>();
	
	path.add(new GridCell());
	for (int i = 1; i < 5; i++){
		GridCell cell = new GridCell(i, i);
		cell.setParent(path.get(i - 1));
		path.add(cell);
	}
	
	List<GridCell> backwardPath = Util.backtrace(path.get(path.size() - 1));
	
	//since initial cell is excluded, removed from original path
	path.remove(0);
	
	assertEquals("Reversed path size does not match" , path.size(), backwardPath.size() );
	for(int i = 0 ; i < path.size(); i++){
		assertSame("Reversed path node is changed", path.get(i), backwardPath.get(i));
	}
}
 
Example #2
Source File: BattleScreen.java    From Norii with Apache License 2.0 6 votes vote down vote up
private List<GridCell> calculateSpellPath(final Entity unit, final Ability ability, final ArrayList<TiledMapPosition> positions) {
	List<GridCell> spellPath = null;

	switch (ability.getLineOfSight()) {
	case CIRCLE:
		spellPath = currentMap.getPathfinder().getLineOfSightWithinCircle(unit.getCurrentPosition().getTileX(), unit.getCurrentPosition().getTileY(),
			ability.getSpellData().getRange(), positions);
		break;
	case CROSS:
		// TODO
		break;
	case LINE:
		spellPath = currentMap.getPathfinder().getLineOfSightWithinLine(unit.getCurrentPosition().getTileX(), unit.getCurrentPosition().getTileY(),
			ability.getSpellData().getRange(), unit.getEntityAnimation().getCurrentDirection(), positions);
		break;
	default:
		break;
	}
	return spellPath;
}
 
Example #3
Source File: MyNavTmxMapLoader.java    From Norii with Apache License 2.0 6 votes vote down vote up
private GridCell[][] createGridCells(TiledMap map, Element element, int width, int height) {
	int[] ids = getTileIds(element, width, height);
	TiledMapTileSets tilesets = map.getTileSets();
	GridCell[][] nodes = new GridCell[width][height];
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			int id = ids[y * width + x];
			TiledMapTile tile = tilesets.getTile(id & ~MASK_CLEAR);
			
			GridCell cell = new GridCell(x, height - 1 - y, false);
			if (tile != null) {
				MapProperties tileProp = tile.getProperties();
				
				String walkableProp = tileProp.get(navigationProperty, navigationClosedValue, String.class);
				cell.setWalkable( !walkableProp.equals(navigationClosedValue) );
			}
			nodes[cell.getX()][cell.getY()] = cell;
		}
	}
	return nodes;
}
 
Example #4
Source File: MyPathFinder.java    From Norii with Apache License 2.0 6 votes vote down vote up
private boolean checkIfInLine(final GridCell center, final GridCell gridcell, final int range, final Direction direction) {
	if (isCloseEnough(center, gridcell, range)) {
		final List<GridCell> path = aStarGridFinder.findPath(center.x, center.y, gridcell.x, gridcell.y, navGrid);
		if ((path != null) && (path.size() <= range) && (!path.isEmpty())) {
			switch (direction) {
			case UP:
				return (center.x == gridcell.x) && (center.y <= gridcell.y);
			case DOWN:
				return (center.x == gridcell.x) && (center.y >= gridcell.y);
			case LEFT:
				return (center.x >= gridcell.x) && (center.y == gridcell.y);
			case RIGHT:
				return (center.x <= gridcell.x) && (center.y == gridcell.y);
			default:
				return false;
			}
		}
		return false;
	}
	return false;
}
 
Example #5
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 #6
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public List<GridCell> getCellsWithinLine(final int x, final int y, final int range, final Direction direction) {
	final List<GridCell> cells = new ArrayList<GridCell>();
	final GridCell center = navGrid.getCell(x, y);

	for (final GridCell[] gridcells : navGrid.getNodes()) {
		for (final GridCell gridcell : gridcells) {
			if (checkIfInLine(center, gridcell, range, direction)) {
				cells.add(gridcell);
			}
		}
	}
	return cells;
}
 
Example #7
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public TiledMapPosition getClosestMoveSpotNextToUnit(Entity mover, Entity target) {
	final int startX = mover.getCurrentPosition().getTileX();
	final int startY = mover.getCurrentPosition().getTileY();
	final int endX = target.getCurrentPosition().getTileX();
	final int endY = target.getCurrentPosition().getTileY();
	final List<GridCell> path = aStarGridFinder.findPath(startX, startY, endX, endY, navGrid);
	for (final GridCell cell : path) {
		if (isNextTo(cell, target.getCurrentPosition())) {
			return new TiledMapPosition().setPositionFromTiles(cell.x, cell.y);
		}
	}
	return null;
}
 
Example #8
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 #9
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public boolean lineOfSight(Entity caster, TiledMapPosition targetPos, Entity[] sortedUnits) {
	final ArrayList<TiledMapPosition> positionsUnits = (ArrayList<TiledMapPosition>) Utility.collectPositionsUnits(sortedUnits);
	final GridCell unitCell = navGrid.getCell(caster.getCurrentPosition().getTileX(), caster.getCurrentPosition().getTileY());
	final GridCell targetCell = navGrid.getCell(targetPos.getTileX(), targetPos.getTileY());

	return lineOfSight(unitCell, targetCell, positionsUnits);
}
 
Example #10
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public List<GridCell> getLineOfSightWithinLine(final int x, final int y, final int range, final Direction direction, final ArrayList<TiledMapPosition> positionsUnits) {
	final List<GridCell> cells = new ArrayList<GridCell>();
	final GridCell center = navGrid.getCell(x, y);

	for (final GridCell[] gridcells : navGrid.getNodes()) {
		for (final GridCell gridcell : gridcells) {
			if (checkIfInLine(center, gridcell, range, direction) && lineOfSight(center, gridcell, positionsUnits)) {
				cells.add(gridcell);
			}
		}
	}
	return cells;
}
 
Example #11
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public List<GridCell> getLineOfSightWithinCircle(final int x, final int y, final int range, final ArrayList<TiledMapPosition> positionsUnits) {
	final List<GridCell> cells = new ArrayList<GridCell>();
	final GridCell center = navGrid.getCell(x, y);

	for (final GridCell[] gridcells : navGrid.getNodes()) {
		for (final GridCell gridcell : gridcells) {
			if (isCloseEnough(center, gridcell, range) && pathExists(center, gridcell, range) && lineOfSight(center, gridcell, positionsUnits)) {
				cells.add(gridcell);
			}
		}
	}
	return cells;
}
 
Example #12
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public List<GridCell> getCellsWithinCircle(final int x, final int y, final int range) {
	final List<GridCell> cells = new ArrayList<GridCell>();
	final GridCell center = navGrid.getCell(x, y);

	for (final GridCell[] gridcells : navGrid.getNodes()) {
		for (final GridCell gridcell : gridcells) {
			if (isCloseEnough(center, gridcell, range) && pathExists(center, gridcell, range)) {
				cells.add(gridcell);
			}
		}
	}
	return cells;
}
 
Example #13
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public boolean lineOfSight(Entity unit, final GridCell to, Entity[] sortedUnits) {
	final ArrayList<TiledMapPosition> positionsUnits = (ArrayList<TiledMapPosition>) Utility.collectPositionsUnits(sortedUnits);
	final GridCell unitCell = navGrid.getCell(unit.getCurrentPosition().getTileX(), unit.getCurrentPosition().getTileY());
	final GridCell targetCell = navGrid.getCell(to.getX(), to.getY());

	return lineOfSight(unitCell, targetCell, positionsUnits);
}
 
Example #14
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 #15
Source File: MyNavTmxMapLoader.java    From Norii with Apache License 2.0 5 votes vote down vote up
private void loadNavigationLayer(TiledMap map, Element element, String layerName){
	int width = element.getIntAttribute("width", 0);
	int height = element.getIntAttribute("height", 0);
	
	GridCell[][] nodes = createGridCells(map, element, width, height);
	MyNavigationTiledMapLayer layer = createNavigationTiledMapLayer(layerName, nodes);
	loadProperties(element, layer);
	
	map.getLayers().add(layer);
}
 
Example #16
Source File: WorldRenderer.java    From xibalba with MIT License 5 votes vote down vote up
private void renderHighlights() {
  if (playerDetails.path != null && playerDetails.target != null) {
    for (int i = 0; i < playerDetails.path.size(); i++) {
      GridCell cell = playerDetails.path.get(i);

      shadow.setColor(Color.WHITE);
      shadow.setAlpha(.15f);
      shadow.setPosition(cell.x * Main.SPRITE_WIDTH, cell.y * Main.SPRITE_HEIGHT);

      shadow.draw(batch);
    }
  }
}
 
Example #17
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public boolean lineOfSight(Entity caster, Entity target, Entity[] sortedUnits) {
	final ArrayList<TiledMapPosition> positionsUnits = (ArrayList<TiledMapPosition>) Utility.collectPositionsUnits(sortedUnits);
	final GridCell unitCell = navGrid.getCell(caster.getCurrentPosition().getTileX(), caster.getCurrentPosition().getTileY());
	final GridCell targetCell = navGrid.getCell(target.getCurrentPosition().getTileX(), target.getCurrentPosition().getTileY());

	return lineOfSight(unitCell, targetCell, positionsUnits);
}
 
Example #18
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public List<GridCell> pathTowards(TiledMapPosition start, TiledMapPosition goal, int ap) {
	final List<GridCell> path = aStarGridFinder.findPath(start.getTileX(), start.getTileY(), goal.getTileX(), goal.getTileY(), navGrid);

	if (path == null) {
		return adjustGoal(start, goal, path);
	}

	if (path.size() <= ap) {
		return path;
	}

	return chippedPath(path, ap);
}
 
Example #19
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
private List<GridCell> adjustGoal(TiledMapPosition start, TiledMapPosition goal, List<GridCell> path) {
	while (path == null) {
		goal = tryAdjacentTile(start, goal);
		path = aStarGridFinder.findPath(start.getTileX(), start.getTileY(), goal.getTileX(), goal.getTileY(), navGrid);
	}
	return path;
}
 
Example #20
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
private List<GridCell> chippedPath(List<GridCell> path, int ap) {
	while (path.size() > ap) {
		path.remove(path.size());
	}

	return path;
}
 
Example #21
Source File: MyPathFinder.java    From Norii with Apache License 2.0 5 votes vote down vote up
public List<GridCell> getPathFromUnitToUnit(Entity mover, Entity target) {
	final int startX = mover.getCurrentPosition().getTileX();
	final int startY = mover.getCurrentPosition().getTileY();
	final int endX = target.getCurrentPosition().getTileX();
	final int endY = target.getCurrentPosition().getTileY();
	return removeEndPoint(aStarGridFinder.findPath(startX, startY, endX, endY, navGrid));
}
 
Example #22
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 #23
Source File: NavTmxMapLoader.java    From pathfinding with Apache License 2.0 5 votes vote down vote up
private void loadNavigationLayer(TiledMap map, Element element, String layerName){
	int width = element.getIntAttribute("width", 0);
	int height = element.getIntAttribute("height", 0);
	
	int[] ids = getTileIds(element, width, height);
	TiledMapTileSets tilesets = map.getTileSets();
	GridCell[][] nodes = new GridCell[width][height];
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			int id = ids[y * width + x];
			TiledMapTile tile = tilesets.getTile(id & ~MASK_CLEAR);
			
			GridCell cell = new GridCell(x, height - 1 - y, false);
			if (tile != null) {
				MapProperties tileProp = tile.getProperties();
				
				String walkableProp = tileProp.get(navigationProperty, navigationClosedValue, String.class);
				cell.setWalkable( !walkableProp.equals(navigationClosedValue) );
			}
			nodes[cell.getX()][cell.getY()] = cell;
		}
	}
	
	NavigationTiledMapLayer layer = new NavigationTiledMapLayer(nodes);
	layer.setName(layerName);
	layer.setVisible(false);

	Element properties = element.getChildByName("properties");
	if (properties != null) {
		loadProperties(layer.getProperties(), properties);
	}
	map.getLayers().add(layer);
}
 
Example #24
Source File: Entity.java    From Norii with Apache License 2.0 5 votes vote down vote up
private float decideRotation(GridCell oldCell, GridCell cell) {
	if ((oldCell.x == cell.x) && (oldCell.y > cell.y)) {
		return 270.0f;
	} else if ((oldCell.x == cell.x) && (oldCell.y < cell.y)) {
		return 180.0f;
	} else if ((oldCell.x > cell.x) && (oldCell.y == cell.y)) {
		return 270.0f;
	}
	return 90.0f;
}
 
Example #25
Source File: BattleScreen.java    From Norii with Apache License 2.0 5 votes vote down vote up
private void prepareMove(final Entity unit) {
	final List<GridCell> path = currentMap.getPathfinder().getCellsWithinCircle(unit.getCurrentPosition().getTileX(), unit.getCurrentPosition().getTileY(), unit.getAp());
	for (final GridCell cell : path) {
		if (!isUnitOnCell(cell)) {
			final TiledMapPosition positionToPutMoveParticle = new TiledMapPosition().setPositionFromTiles(cell.x, cell.y);
			ParticleMaker.addParticle(ParticleType.MOVE, positionToPutMoveParticle);
			battlemanager.setCurrentBattleState(battlemanager.getMovementBattleState());
		}
	}
}
 
Example #26
Source File: BattleScreen.java    From Norii with Apache License 2.0 5 votes vote down vote up
private void prepareAttack(final Entity unit) {
	final List<GridCell> attackPath = currentMap.getPathfinder().getCellsWithinCircle(unit.getCurrentPosition().getTileX(), unit.getCurrentPosition().getTileY(),
		unit.getEntityData().getAttackRange());
	for (final GridCell cell : attackPath) {
		final TiledMapPosition positionToPutAttackParticle = new TiledMapPosition().setPositionFromTiles(cell.x, cell.y);
		ParticleMaker.addParticle(ParticleType.ATTACK, positionToPutAttackParticle);
		battlemanager.setCurrentBattleState(battlemanager.getAttackBattleState());
	}
}
 
Example #27
Source File: BattleScreen.java    From Norii with Apache License 2.0 5 votes vote down vote up
private void prepareSpell(final Entity unit, final Ability ability) {
	final ArrayList<TiledMapPosition> positions = Utility.collectPositionsUnits(players);
	final List<GridCell> spellPath = calculateSpellPath(unit, ability, positions);

	for (final GridCell cell : spellPath) {
		final TiledMapPosition positionToPutSpellParticle = new TiledMapPosition().setPositionFromTiles(cell.x, cell.y);
		ParticleMaker.addParticle(ParticleType.SPELL, positionToPutSpellParticle);
	}

	battlemanager.setCurrentSpell(ability);
	battlemanager.setCurrentBattleState(battlemanager.getSpellBattleState());
}
 
Example #28
Source File: BattleScreen.java    From Norii with Apache License 2.0 5 votes vote down vote up
private boolean isUnitOnCell(final GridCell cell) {
	final TiledMapPosition cellToTiled = new TiledMapPosition().setPositionFromTiles(cell.x, cell.y);
	for (final Entity entity : allUnits) {
		if (entity.getCurrentPosition().isTileEqualTo(cellToTiled)) {
			return true;
		}
	}
	return false;
}
 
Example #29
Source File: Entity.java    From Norii with Apache License 2.0 5 votes vote down vote up
private SequenceAction createMoveSequence(List<GridCell> path) {
	GridCell oldCell = new GridCell(this.getCurrentPosition().getTileX(), this.getCurrentPosition().getTileY());
	final SequenceAction sequence = Actions.sequence();
	for (final GridCell cell : path) {
		sequence.addAction(Actions.rotateTo(decideRotation(oldCell, cell), 0.1f));
		sequence.addAction(moveTo(cell.x, cell.y, 0.2f));
		sequence.addAction(run(updatePositionAction));
		oldCell = cell;
	}
	return sequence;
}
 
Example #30
Source File: Entity.java    From Norii with Apache License 2.0 5 votes vote down vote up
public void moveAttack(List<GridCell> path, Entity target) {
	final SequenceAction sequence = createMoveSequence(path);
	sequence.addAction(new AttackAction(target));
	sequence.addAction(run(aiFinishTurn));

	this.getEntityactor().addAction(sequence);
	this.setAp(this.getAp() - path.size() - this.getEntityData().getBasicAttackCost());
}