-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support 8 directions AStar pathfinding, closes #1213
Co-authored-by: Jean-René Lavoie <>
- Loading branch information
1 parent
6c00330
commit 641b614
Showing
7 changed files
with
310 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
fxgl-entity/src/main/java/com/almasb/fxgl/pathfinding/heuristic/Heuristic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB ([email protected]). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.pathfinding.heuristic; | ||
|
||
import com.almasb.fxgl.core.collection.grid.Cell; | ||
|
||
/** | ||
* @author Jean-René Lavoie ([email protected]) | ||
*/ | ||
public abstract class Heuristic<T extends Cell> { | ||
|
||
public static final int DEFAULT_WEIGHT = 10; | ||
|
||
private final int weight; | ||
|
||
public Heuristic() { | ||
this(DEFAULT_WEIGHT); | ||
} | ||
|
||
public Heuristic(int weight) { | ||
this.weight = weight; | ||
} | ||
|
||
public abstract int getCost(int x, int y, T target); | ||
|
||
public int getWeight() { | ||
return weight; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
fxgl-entity/src/main/java/com/almasb/fxgl/pathfinding/heuristic/ManhattanDistance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB ([email protected]). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.pathfinding.heuristic; | ||
|
||
import com.almasb.fxgl.core.collection.grid.Cell; | ||
|
||
/** | ||
* @author Jean-René Lavoie ([email protected]) | ||
*/ | ||
public class ManhattanDistance<T extends Cell> extends Heuristic<T> { | ||
|
||
public ManhattanDistance() { | ||
super(); | ||
} | ||
|
||
public ManhattanDistance(int weight) { | ||
super(weight); | ||
} | ||
|
||
@Override | ||
public int getCost(int x, int y, T target) { | ||
return (Math.abs(target.getX() - x) + Math.abs(target.getY() - y)) * getWeight(); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
fxgl-entity/src/main/java/com/almasb/fxgl/pathfinding/heuristic/OctileDistance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB ([email protected]). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.pathfinding.heuristic; | ||
|
||
import com.almasb.fxgl.core.collection.grid.Cell; | ||
|
||
/** | ||
* @author Jean-René Lavoie ([email protected]) | ||
*/ | ||
public class OctileDistance<T extends Cell> extends Heuristic<T> { | ||
|
||
private static final int DIAGONAL_WEIGHT = (int)(Math.sqrt(2) * 10.0); | ||
private static final int DIAGONAL_FACTOR = DIAGONAL_WEIGHT - 10; | ||
|
||
public OctileDistance() { | ||
super(DIAGONAL_WEIGHT); | ||
} | ||
|
||
public OctileDistance(int weight) { | ||
super(weight); | ||
} | ||
|
||
@Override | ||
public int getCost(int x, int y, T target) { | ||
int dx = Math.abs(x - target.getX()); | ||
int dy = Math.abs(y - target.getY()); | ||
|
||
if(dx == dy) { | ||
return (dx + dy) * 10; | ||
} | ||
if(dx < dy) { | ||
return DIAGONAL_FACTOR * dx + 10 * dy; | ||
} | ||
return DIAGONAL_FACTOR * dy + 10 * dx; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.