Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Feb 21, 2024
1 parent 2105ceb commit 4a08055
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package com.almasb.fxgl.core.collection.grid;

import com.almasb.fxgl.core.math.FXGLMath;
import static com.almasb.fxgl.core.collection.grid.NeighborFilteringOption.*;
import static com.almasb.fxgl.core.collection.grid.NeighborDirection.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand Down Expand Up @@ -123,35 +123,35 @@ public final List<T> getCells() {
}

/**
* Note: returned cells are in the grid (i.e. bounds are checked). (defaulted to 4 directions)
* Note: returned cells are in the grid (i.e. bounds are checked).
* The order is left, up, right, down.
*
* @return a new list of neighboring cells to given (x, y)
* @return a new list of neighboring cells to given (x, y) in 4 directions
*/
public final List<T> getNeighbors(int x, int y) {
return getNeighbors(x, y, FOUR_DIRECTIONS);
}

/**
* Note: returned cells are in the grid (i.e. bounds are checked).
* NeighborFilteringOption allow filtering based on desired # of directions
* The order is left, up, right, down. + "Optionally" up-left, up-right, down-left, down-right
* The order is left, up, right, down for 4 directions
* + (optionally) up-left, up-right, down-right, down-left for 8 directions.
*
* @return a new list of neighboring cells to given (x, y)
* @return a new list of neighboring cells to given (x, y) in desired # of directions
*/
public final List<T> getNeighbors(int x, int y, NeighborFilteringOption neighborFilteringOption) {
public final List<T> getNeighbors(int x, int y, NeighborDirection neighborDirection) {
List<T> result = new ArrayList<>();
getLeft(x, y).ifPresent(result::add);
getUp(x, y).ifPresent(result::add);
getRight(x, y).ifPresent(result::add);
getDown(x, y).ifPresent(result::add);

// Include "Corner" neighbors when eight directions
if (neighborFilteringOption == EIGHT_DIRECTIONS) {
if (neighborDirection == EIGHT_DIRECTIONS) {
getUpLeft(x, y).ifPresent(result::add);
getUpRight(x, y).ifPresent(result::add);
getDownLeft(x, y).ifPresent(result::add);
getDownRight(x, y).ifPresent(result::add);
getDownLeft(x, y).ifPresent(result::add);
}

return result;
Expand Down Expand Up @@ -237,7 +237,6 @@ public final Optional<T> getDownLeft(int x, int y) {
return getOptional(x - 1, y + 1);
}


/**
* @param x pixel coord x
* @param y pixel coord y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

package com.almasb.fxgl.core.collection.grid;


/**
* Define Movement Directions
* Defines neighboring directions.
*
* @author Jean-Rene Lavoie ([email protected])
*/
public enum NeighborFilteringOption {

FOUR_DIRECTIONS, EIGHT_DIRECTIONS;
public enum NeighborDirection {
FOUR_DIRECTIONS, EIGHT_DIRECTIONS
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testGetNeighbors() {

@Test
public void testGetNeighborsEightDirections() {
assertThat(grid.getNeighbors( 3,3, NeighborFilteringOption.EIGHT_DIRECTIONS), containsInAnyOrder(
assertThat(grid.getNeighbors( 3,3, NeighborDirection.EIGHT_DIRECTIONS), containsInAnyOrder(
grid.get(2, 2), grid.get(2, 3), grid.get(2, 4),
grid.get(3, 2), grid.get(3, 4), // Doesn't contain 3, 3 (self)
grid.get(4, 2), grid.get(4, 3), grid.get(4, 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package com.almasb.fxgl.pathfinding;

import com.almasb.fxgl.core.collection.grid.Cell;
import com.almasb.fxgl.core.collection.grid.NeighborFilteringOption;
import com.almasb.fxgl.core.collection.grid.NeighborDirection;

import java.util.List;

Expand All @@ -28,7 +28,7 @@ public interface Pathfinder<T extends Cell> {
*
* @return a list of cells from source (excl.) to target (incl.)
*/
List<T> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborFilteringOption neighborFilteringOption);
List<T> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborDirection neighborDirection);

/**
* Empty list is returned if no path exists.
Expand All @@ -42,6 +42,6 @@ public interface Pathfinder<T extends Cell> {
*
* @return a list of cells from source (excl.) to target (incl.) while ignoring busyCells
*/
List<T> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborFilteringOption neighborFilteringOption, List<T> busyCells);
List<T> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborDirection neighborDirection, List<T> busyCells);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package com.almasb.fxgl.pathfinding.astar;

import com.almasb.fxgl.core.collection.grid.Cell;
import com.almasb.fxgl.core.collection.grid.NeighborFilteringOption;
import static com.almasb.fxgl.core.collection.grid.NeighborFilteringOption.*;
import com.almasb.fxgl.core.collection.grid.NeighborDirection;
import static com.almasb.fxgl.core.collection.grid.NeighborDirection.*;
import com.almasb.fxgl.pathfinding.CellState;
import com.almasb.fxgl.pathfinding.Pathfinder;
import com.almasb.fxgl.pathfinding.heuristic.Heuristic;
Expand Down Expand Up @@ -62,8 +62,8 @@ public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targe
}

@Override
public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborFilteringOption neighborFilteringOption) {
return findPath(grid.getData(), grid.get(sourceX, sourceY), grid.get(targetX, targetY), neighborFilteringOption);
public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborDirection neighborDirection) {
return findPath(grid.getData(), grid.get(sourceX, sourceY), grid.get(targetX, targetY), neighborDirection);
}

@Override
Expand All @@ -72,8 +72,8 @@ public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targe
}

@Override
public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborFilteringOption neighborFilteringOption, List<AStarCell> busyCells) {
return findPath(grid.getData(), grid.get(sourceX, sourceY), grid.get(targetX, targetY), neighborFilteringOption, busyCells.toArray(new AStarCell[0]));
public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targetY, NeighborDirection neighborDirection, List<AStarCell> busyCells) {
return findPath(grid.getData(), grid.get(sourceX, sourceY), grid.get(targetX, targetY), neighborDirection, busyCells.toArray(new AStarCell[0]));
}

/**
Expand All @@ -87,7 +87,7 @@ public List<AStarCell> findPath(int sourceX, int sourceY, int targetX, int targe
* @return path as list of nodes from start (excl) to target (incl) or empty list if no path found
*/
public List<AStarCell> findPath(AStarCell[][] grid, AStarCell start, AStarCell target, AStarCell... busyNodes) {
return findPath(grid, start, target, NeighborFilteringOption.FOUR_DIRECTIONS, busyNodes);
return findPath(grid, start, target, NeighborDirection.FOUR_DIRECTIONS, busyNodes);
}

/**
Expand All @@ -100,7 +100,7 @@ public List<AStarCell> findPath(AStarCell[][] grid, AStarCell start, AStarCell t
* @param busyNodes busy "unwalkable" nodes
* @return path as list of nodes from start (excl) to target (incl) or empty list if no path found
*/
public List<AStarCell> findPath(AStarCell[][] grid, AStarCell start, AStarCell target, NeighborFilteringOption neighborFilteringOption, AStarCell... busyNodes) {
public List<AStarCell> findPath(AStarCell[][] grid, AStarCell start, AStarCell target, NeighborDirection neighborDirection, AStarCell... busyNodes) {
if (start == target || target.getState() == CellState.NOT_WALKABLE)
return Collections.emptyList();

Expand All @@ -114,7 +114,7 @@ public List<AStarCell> findPath(AStarCell[][] grid, AStarCell start, AStarCell t
}
}

Heuristic<AStarCell> heuristic = (neighborFilteringOption == FOUR_DIRECTIONS) ? defaultHeuristic : diagonalHeuristic;
Heuristic<AStarCell> heuristic = (neighborDirection == FOUR_DIRECTIONS) ? defaultHeuristic : diagonalHeuristic;

// reset grid cells data
for (int y = 0; y < grid[0].length; y++) {
Expand All @@ -133,7 +133,7 @@ public List<AStarCell> findPath(AStarCell[][] grid, AStarCell start, AStarCell t
boolean found = false;

while (!found && !closed.contains(target)) {
for (AStarCell neighbor : getValidNeighbors(current, neighborFilteringOption, busyNodes)) {
for (AStarCell neighbor : getValidNeighbors(current, neighborDirection, busyNodes)) {
if (neighbor == target) {
target.setParent(current);
found = true;
Expand Down Expand Up @@ -208,8 +208,8 @@ private List<AStarCell> buildPath(AStarCell start, AStarCell target) {
* @param busyNodes nodes which are busy, i.e. walkable but have a temporary obstacle
* @return neighbors of the node
*/
private List<AStarCell> getValidNeighbors(AStarCell node, NeighborFilteringOption neighborFilteringOption, AStarCell... busyNodes) {
var result = grid.getNeighbors(node.getX(), node.getY(), neighborFilteringOption);
private List<AStarCell> getValidNeighbors(AStarCell node, NeighborDirection neighborDirection, AStarCell... busyNodes) {
var result = grid.getNeighbors(node.getX(), node.getY(), neighborDirection);
result.removeAll(Arrays.asList(busyNodes));
result.removeIf(cell -> !cell.isWalkable());
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.almasb.fxgl.pathfinding.astar

import com.almasb.fxgl.core.collection.grid.NeighborFilteringOption
import com.almasb.fxgl.core.collection.grid.NeighborDirection
import com.almasb.fxgl.pathfinding.CellState
import com.almasb.fxgl.pathfinding.heuristic.ManhattanDistance
import com.almasb.fxgl.pathfinding.heuristic.OctileDistance
Expand Down Expand Up @@ -53,7 +53,7 @@ class AStarPathfinderTest {
5, 0)

// Now test Diagonal Path Finding
path = pathfinder.findPath(3, 0, 5, 0, NeighborFilteringOption.EIGHT_DIRECTIONS)
path = pathfinder.findPath(3, 0, 5, 0, NeighborDirection.EIGHT_DIRECTIONS)
assertPathEquals(path,
3, 1,
3, 2,
Expand Down Expand Up @@ -95,7 +95,7 @@ class AStarPathfinderTest {
5, 0)

// Now test Diagonal Path Finding
path = pathfinderHeuristics.findPath(3, 0, 5, 0, NeighborFilteringOption.EIGHT_DIRECTIONS)
path = pathfinderHeuristics.findPath(3, 0, 5, 0, NeighborDirection.EIGHT_DIRECTIONS)
assertPathEquals(path,
3, 1,
3, 2,
Expand Down Expand Up @@ -144,7 +144,7 @@ class AStarPathfinderTest {


// Perform Diagonal Testing
path = pathfinder.findPath(1, 1, 4, 5, NeighborFilteringOption.EIGHT_DIRECTIONS, ArrayList())
path = pathfinder.findPath(1, 1, 4, 5, NeighborDirection.EIGHT_DIRECTIONS, ArrayList())

assertThat(path.size, `is`(4))

Expand All @@ -153,7 +153,7 @@ class AStarPathfinderTest {
assertThat(last.x, `is`(4))
assertThat(last.y, `is`(5))

pathWithBusyCell = pathfinder.findPath(1, 1, 4, 5, NeighborFilteringOption.EIGHT_DIRECTIONS, listOf(grid[3, 4]))
pathWithBusyCell = pathfinder.findPath(1, 1, 4, 5, NeighborDirection.EIGHT_DIRECTIONS, listOf(grid[3, 4]))

assertThat(pathWithBusyCell.size, `is`(6))

Expand Down Expand Up @@ -191,7 +191,7 @@ class AStarPathfinderTest {


// Perform Diagonal Testing
path = pathfinderHeuristics.findPath(1, 1, 4, 5, NeighborFilteringOption.EIGHT_DIRECTIONS, ArrayList())
path = pathfinderHeuristics.findPath(1, 1, 4, 5, NeighborDirection.EIGHT_DIRECTIONS, ArrayList())

assertThat(path.size, `is`(6))

Expand All @@ -200,7 +200,7 @@ class AStarPathfinderTest {
assertThat(last.x, `is`(4))
assertThat(last.y, `is`(5))

pathWithBusyCell = pathfinderHeuristics.findPath(1, 1, 4, 5, NeighborFilteringOption.EIGHT_DIRECTIONS, listOf(grid[3, 4]))
pathWithBusyCell = pathfinderHeuristics.findPath(1, 1, 4, 5, NeighborDirection.EIGHT_DIRECTIONS, listOf(grid[3, 4]))

assertThat(pathWithBusyCell.size, `is`(8))

Expand Down

0 comments on commit 4a08055

Please sign in to comment.