Support tokens of multiple sizes

This commit is contained in:
Manuel Vögele
2022-02-15 17:35:09 +01:00
parent 833aced2be
commit 09e8ca79b3
+11 -9
View File
@@ -8,7 +8,7 @@ import * as GridlessPathfinding from "../wasm/gridless_pathfinding.js"
let cachedNodes = undefined; let cachedNodes = undefined;
let use5105 = false; let use5105 = false;
let gridlessPathfinder = undefined; let gridlessPathfinders = new Map();
export function isPathfindingEnabled() { export function isPathfindingEnabled() {
if (this.user !== game.user) if (this.user !== game.user)
@@ -20,12 +20,14 @@ export function isPathfindingEnabled() {
export function findPath(from, to, token, previousWaypoints) { export function findPath(from, to, token, previousWaypoints) {
if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) { if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) {
// TODO Store multiple pathfinders for different token sizes
let tokenSize = Math.max(token.data.width, token.data.height) * canvas.dimensions.size; let tokenSize = Math.max(token.data.width, token.data.height) * canvas.dimensions.size;
if (!gridlessPathfinder) let pathfinder = gridlessPathfinders.get(tokenSize);
gridlessPathfinder = GridlessPathfinding.initialize(canvas.walls.placeables, tokenSize); if (!pathfinder) {
paintGridlessPathfindingDebug(gridlessPathfinder); pathfinder = GridlessPathfinding.initialize(canvas.walls.placeables, tokenSize);
return GridlessPathfinding.findPath(gridlessPathfinder, from, to); gridlessPathfinders.set(tokenSize, pathfinder);
}
paintGridlessPathfindingDebug(pathfinder);
return GridlessPathfinding.findPath(pathfinder, from, to);
} }
else { else {
const lastNode = calculatePath(from, to, token, previousWaypoints); const lastNode = calculatePath(from, to, token, previousWaypoints);
@@ -49,10 +51,10 @@ export function findPath(from, to, token, previousWaypoints) {
export function wipePathfindingCache() { export function wipePathfindingCache() {
cachedNodes = undefined; cachedNodes = undefined;
if (gridlessPathfinder) { for (const pathfinder of gridlessPathfinders.values()) {
GridlessPathfinding.free(gridlessPathfinder); GridlessPathfinding.free(pathfinder);
gridlessPathfinder = undefined;
} }
gridlessPathfinders.clear();
if (debugGraphics) if (debugGraphics)
debugGraphics.removeChildren().forEach(c => c.destroy()); debugGraphics.removeChildren().forEach(c => c.destroy());
} }