If Terran Ruler is enabled, store the length of the traveled path so changes to difficult terrain aren't reflected in the tokens movement history
This commit is contained in:
+18
-5
@@ -14,10 +14,23 @@ export function highlightMeasurementTerrainRuler(ray, startDistance, tokenShape=
|
||||
}
|
||||
}
|
||||
|
||||
export function measureDistances(segments, token, shape, gridSpaces=true) {
|
||||
export function measureDistances(segments, token, shape, gridSpaces=true, options={}) {
|
||||
const opts = duplicate(options)
|
||||
opts.gridSpaces = gridSpaces;
|
||||
const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS;
|
||||
if (terrainRulerAvailable)
|
||||
return terrainRuler.measureDistances(segments, {costFunction: (x, y) => getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), {x, y})});
|
||||
else
|
||||
return canvas.grid.measureDistances(segments, { gridSpaces });
|
||||
if (terrainRulerAvailable) {
|
||||
const firstNewSegmentIndex = segments.findIndex(segment => !segment.ray.dragRulerVisitedSpaces);
|
||||
const previousSegments = segments.slice(0, firstNewSegmentIndex);
|
||||
const newSegments = segments.slice(firstNewSegmentIndex);
|
||||
const distances = previousSegments.map(segment => segment.ray.dragRulerVisitedSpaces[segment.ray.dragRulerVisitedSpaces.length - 1].distance);
|
||||
previousSegments.forEach(segment => segment.ray.terrainRulerVisitedSpaces = duplicate(segment.ray.dragRulerVisitedSpaces));
|
||||
opts.costFunction = (x, y) => getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), {x, y});
|
||||
if (previousSegments.length > 0)
|
||||
opts.terrainRulerInitialState = previousSegments[previousSegments.length - 1];
|
||||
return distances.concat(terrainRuler.measureDistances(newSegments, opts));
|
||||
}
|
||||
else {
|
||||
return canvas.grid.measureDistances(segments, options);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -136,6 +136,8 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
|
||||
const centeredRay = new Ray(centeredOrigin, centeredDest)
|
||||
ray.isPrevious = Boolean(origin.isPrevious);
|
||||
centeredRay.isPrevious = ray.isPrevious;
|
||||
ray.dragRulerVisitedSpaces = origin.dragRulerVisitedSpaces;
|
||||
centeredRay.dragRulerVisitedSpaces = ray.dragRulerVisitedSpaces;
|
||||
if (ray.distance < 10) {
|
||||
if (label) label.visible = false;
|
||||
continue;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import {measureDistances} from "./compatibility.js";
|
||||
import {getTokenShape} from "./util.js";
|
||||
|
||||
function initTrackingFlag(combatant) {
|
||||
const initialFlag = {passedWaypoints: [], trackedRound: 0};
|
||||
let dragRulerFlag = combatant.flags?.dragRuler;
|
||||
@@ -37,11 +40,19 @@ export async function trackRays(token, rays) {
|
||||
}
|
||||
|
||||
// Add the passed waypoints to the combatant
|
||||
const waypoints = combatant.flags.dragRuler.passedWaypoints;
|
||||
const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS;
|
||||
const dragRulerFlags = combatant.flags.dragRuler;
|
||||
const waypoints = dragRulerFlags.passedWaypoints;
|
||||
for (const ray of rays) {
|
||||
// Ignore rays that have the same start and end coordinates
|
||||
if (ray.A.x !== ray.B.x || ray.A.y !== ray.B.y)
|
||||
if (ray.A.x !== ray.B.x || ray.A.y !== ray.B.y) {
|
||||
if (terrainRulerAvailable) {
|
||||
measureDistances([{ray}], token, getTokenShape(token), true, {terrainRulerInitialState: dragRulerFlags.rulerState});
|
||||
ray.A.dragRulerVisitedSpaces = ray.terrainRulerVisitedSpaces;
|
||||
dragRulerFlags.rulerState = ray.terrainRulerFinalState;
|
||||
}
|
||||
waypoints.push(ray.A);
|
||||
}
|
||||
}
|
||||
await combat.updateEmbeddedEntity("Combatant", {_id: combatant._id, flags: combatant.flags}, {diff: false});
|
||||
}
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ export class DragRulerRuler extends Ruler {
|
||||
}
|
||||
|
||||
dragRulerAddWaypointHistory(waypoints) {
|
||||
waypoints = waypoints.map(waypoint => {return {x: waypoint.x, y: waypoint.y, isPrevious: true}});
|
||||
waypoints.forEach(waypoint => waypoint.isPrevious = true);
|
||||
this.waypoints = this.waypoints.concat(waypoints);
|
||||
for (const waypoint of waypoints) {
|
||||
this.labels.addChild(new PreciseText("", CONFIG.canvasTextStyle));
|
||||
|
||||
Reference in New Issue
Block a user