Add api endpoint to determine the distance that a token has moved already

This commit is contained in:
Manuel Vögele
2021-04-08 10:45:14 +02:00
parent c66ec46aed
commit 35624a37aa
5 changed files with 29 additions and 12 deletions
+12
View File
@@ -1,5 +1,8 @@
import {measureDistances} from "./compatibility.js";
import {getMovementHistory} from "./movement_tracking.js";
import {GenericSpeedProvider, SpeedProvider} from "./speed_provider.js" import {GenericSpeedProvider, SpeedProvider} from "./speed_provider.js"
import {settingsKey} from "./settings.js" import {settingsKey} from "./settings.js"
import {getTokenShape} from "./util.js";
export const availableSpeedProviders = {} export const availableSpeedProviders = {}
export let currentSpeedProvider = undefined export let currentSpeedProvider = undefined
@@ -104,6 +107,15 @@ export function getCostFromSpeedProvider(token, area) {
return currentSpeedProvider.getCostForStep(token, area); return currentSpeedProvider.getCostForStep(token, area);
} }
export function getMovedDistanceFromToken(token) {
const history = getMovementHistory(token);
const segments = Ruler.dragRulerGetRaysFromWaypoints(history, {x: token.x, y: token.y}).map(ray => {return {ray}});
const shape = getTokenShape(token);
const distances = measureDistances(segments, token, shape);
// Sum up the distances
return distances.reduce((acc, val) => acc + val, 0);
}
export function registerModule(moduleId, speedProvider) { export function registerModule(moduleId, speedProvider) {
// Check if a module with the given id exists and is currently enabled // Check if a module with the given id exists and is currently enabled
const module = game.modules.get(moduleId) const module = game.modules.get(moduleId)
+10 -1
View File
@@ -1,5 +1,6 @@
import {getCostFromSpeedProvider} from "./api.js";
import {getColorForDistance} from "./main.js" import {getColorForDistance} from "./main.js"
import {highlightTokenShape} from "./util.js" import {getAreaFromPositionAndShape, highlightTokenShape} from "./util.js";
export function getHexSizeSupportTokenGridCenter(token) { export function getHexSizeSupportTokenGridCenter(token) {
const tokenCenterOffset = CONFIG.hexSizeSupport.getCenterOffset(token) const tokenCenterOffset = CONFIG.hexSizeSupport.getCenterOffset(token)
@@ -12,3 +13,11 @@ export function highlightMeasurementTerrainRuler(ray, startDistance, tokenShape=
highlightTokenShape.call(this, space, tokenShape, color, alpha) highlightTokenShape.call(this, space, tokenShape, color, alpha)
} }
} }
export function measureDistances(segments, token, shape, gridSpaces=true) {
const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS;
if (terrainRulerAvailable)
return game.terrainRuler.measureDistances(segments, {costFunction: (x, y) => getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), {x, y})});
else
return canvas.grid.measureDistances(segments, { gridSpaces });
}
+4 -9
View File
@@ -1,9 +1,8 @@
import { getCostFromSpeedProvider } from "./api.js"; import {highlightMeasurementTerrainRuler, measureDistances} from "./compatibility.js";
import {highlightMeasurementTerrainRuler} from "./compatibility.js";
import {getGridPositionFromPixels} from "./foundry_fixes.js"; import {getGridPositionFromPixels} from "./foundry_fixes.js";
import {getColorForDistance} from "./main.js" import {getColorForDistance} from "./main.js"
import {trackRays} from "./movement_tracking.js" import {trackRays} from "./movement_tracking.js"
import {applyTokenSizeOffset, getAreaFromPositionAndShape, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; import {applyTokenSizeOffset, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js";
// This is a modified version of Ruler.moveToken from foundry 0.7.9 // This is a modified version of Ruler.moveToken from foundry 0.7.9
export async function moveTokens(draggedToken, selectedTokens) { export async function moveTokens(draggedToken, selectedTokens) {
@@ -17,7 +16,7 @@ export async function moveTokens(draggedToken, selectedTokens) {
// Get the movement rays and check collision along each Ray // Get the movement rays and check collision along each Ray
// These rays are center-to-center for the purposes of collision checking // These rays are center-to-center for the purposes of collision checking
const rays = this.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination); const rays = this.constructor.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination);
if (!game.user.isGM) { if (!game.user.isGM) {
const hasCollision = selectedTokens.some(token => { const hasCollision = selectedTokens.some(token => {
const offset = calculateTokenOffset(token, draggedToken) const offset = calculateTokenOffset(token, draggedToken)
@@ -149,11 +148,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
const shape = getTokenShape(this.draggedToken) const shape = getTokenShape(this.draggedToken)
// Compute measured distance // Compute measured distance
let distances const distances = measureDistances(centeredSegments, this.draggedToken, shape, gridSpaces);
if (terrainRulerAvailable)
distances = game.terrainRuler.measureDistances(centeredSegments, {costFunction: (x, y) => getCostFromSpeedProvider(this.draggedToken, getAreaFromPositionAndShape({x, y}, shape), {x, y})});
else
distances = canvas.grid.measureDistances(centeredSegments, { gridSpaces });
let totalDistance = 0; let totalDistance = 0;
for (let [i, d] of distances.entries()) { for (let [i, d] of distances.entries()) {
+2 -1
View File
@@ -1,6 +1,6 @@
"use strict" "use strict"
import {currentSpeedProvider, getRangesFromSpeedProvider, getUnreachableColorFromSpeedProvider, initApi, registerModule, registerSystem} from "./api.js" import {currentSpeedProvider, getMovedDistanceFromToken, getRangesFromSpeedProvider, getUnreachableColorFromSpeedProvider, initApi, registerModule, registerSystem} from "./api.js"
import {getHexSizeSupportTokenGridCenter} from "./compatibility.js" import {getHexSizeSupportTokenGridCenter} from "./compatibility.js"
import {moveTokens, onMouseMove} from "./foundry_imports.js" import {moveTokens, onMouseMove} from "./foundry_imports.js"
import {performMigrations} from "./migration.js" import {performMigrations} from "./migration.js"
@@ -19,6 +19,7 @@ Hooks.once("init", () => {
window.dragRuler = { window.dragRuler = {
getColorForDistance, getColorForDistance,
getMovedDistanceFromToken,
registerModule, registerModule,
registerSystem, registerSystem,
} }
+1 -1
View File
@@ -99,7 +99,7 @@ export class DragRulerRuler extends Ruler {
} }
} }
dragRulerGetRaysFromWaypoints(waypoints, destination) { static dragRulerGetRaysFromWaypoints(waypoints, destination) {
if ( destination ) if ( destination )
waypoints = waypoints.concat([destination]); waypoints = waypoints.concat([destination]);
return waypoints.slice(1).map((wp, i) => { return waypoints.slice(1).map((wp, i) => {