Add support for difficult terrain via Terrain Ruler module (resolves #1)

This commit is contained in:
Manuel Vögele
2021-03-09 10:02:09 +01:00
parent 0a7d8495c6
commit 384de1a1b5
6 changed files with 62 additions and 3 deletions
+11
View File
@@ -1,3 +1,6 @@
import {getPixelsFromGridPosition} from "./foundry_fixes.js"
import {getColorForDistance} from "./main.js"
export function getHexSizeSupportTokenGridCenter(token) {
const tokenCenterOffset = CONFIG.hexSizeSupport.getCenterOffset(token)
const tokenCenter = {x: token.x + tokenCenterOffset.x, y: token.y + tokenCenterOffset.y}
@@ -17,3 +20,11 @@ export function getHexSizeSupportTokenGridCenter(token) {
}
return tokenCenter
}
export function highlightMeasurementTerrainRuler(ray, startDistance) {
for (const space of ray.terrainRulerVisitedSpaces) {
const [x, y] = getPixelsFromGridPosition(space.x, space.y);
const color = getColorForDistance.call(this, startDistance, space.distance)
canvas.grid.highlightPosition(this.name, {x, y, color: color})
}
}
+21
View File
@@ -0,0 +1,21 @@
// Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.grid.getPixelsFromGridPosition to be ordered inconsistently
// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705
export function getPixelsFromGridPosition(xGrid, yGrid) {
if (canvas.grid.isHex) {
return canvas.grid.grid.getPixelsFromGridPosition(yGrid, xGrid)
}
const [x, y] = canvas.grid.grid.getPixelsFromGridPosition(xGrid, yGrid)
if (canvas.grid.type === CONST.GRID_TYPES.SQUARE)
return [y, x]
return [x, y]
}
// Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.grid.getPixelsFromGridPosition to be ordered inconsistently
// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705
export function getGridPositionFromPixels(xPixel, yPixel) {
const [x, y] = canvas.grid.grid.getGridPositionFromPixels(xPixel, yPixel)
if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS)
return [y, x]
return [x, y]
}
+14 -3
View File
@@ -1,4 +1,6 @@
import {highlightMeasurementTerrainRuler} from "./compatibility.js";
import {getColorForDistance} from "./main.js"
import {zip} from "./util.js"
// This is a modified version of Ruler.moveToken from foundry 0.7.9
export async function moveTokens(draggedToken, selectedTokens) {
@@ -127,7 +129,13 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
}
// Compute measured distance
const distances = canvas.grid.measureDistances(centeredSegments, { gridSpaces });
const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS
let distances
if (terrainRulerAvailable)
distances = game.terrainRuler.measureDistances(centeredSegments)
else
distances = canvas.grid.measureDistances(centeredSegments, { gridSpaces });
let totalDistance = 0;
for (let [i, d] of distances.entries()) {
let s = segments[i];
@@ -149,7 +157,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
rulerColor = getColorForDistance.call(this, totalDistance)
else
rulerColor = this.color
for (let s of segments) {
for (const [s, cs] of zip(segments, centeredSegments)) {
const { ray, label, text, last } = s;
// Draw line segment
@@ -166,7 +174,10 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
}
// Highlight grid positions
highlightMeasurementNative.call(this, ray, s.startDistance);
if (terrainRulerAvailable)
highlightMeasurementTerrainRuler.call(this, cs.ray, s.startDistance)
else
highlightMeasurementNative.call(this, ray, s.startDistance);
}
// Draw endpoints
+5
View File
@@ -0,0 +1,5 @@
export function* zip(it1, it2) {
for (let i = 0;i < Math.min(it1.length, it2.length);i++) {
yield [it1[i], it2[i]]
}
}