Skip to content

Commit

Permalink
Call the canvas measure to get distance for diagonals on grid
Browse files Browse the repository at this point in the history
So that 5-5-5 and 5-10-5 work correctly.
  • Loading branch information
caewok committed Jul 25, 2021
1 parent c42e9ec commit 3e3cd01
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions scripts/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ function projectSquareGrid(A, B) {

function projectSouth(A, B, height, distance) {
if(height === undefined) height = A.z - B.z;

if(distance === undefined) distance = window.libRuler.RulerUtilities.calculateDistance({x: A.x, y: A.y},
{x: B.x, y: B.y});
if(distance === undefined) distance = gridDistance(A, B);

// set A pointing south; B pointing west
const projected_A = {x: A.x, y: A.y + height};
Expand All @@ -138,19 +136,18 @@ function projectSouth(A, B, height, distance) {

// if dnd5e 5-5-5 or 5-10-5, snap to nearest grid point
// origin should be fine if elevation is in increments. Otherwise, may need to be snapped. Leave for now.
if((canvas.grid.diagonalRule === "555" || canvas.grid.diagonalRule === "5105" || game.system.id === "pf2e")) {
log(`Snapping ${projected_B.x}, ${projected_B.y}`);
[projected_B.x, projected_B.y] = canvas.grid.getCenter(projected_B.x, projected_B.y);
log(`Snapped to ${projected_B.x}, ${projected_B.y}`);
}

// if((canvas.grid.diagonalRule === "555" || canvas.grid.diagonalRule === "5105" || game.system.id === "pf2e")) {
// log(`Snapping ${projected_B.x}, ${projected_B.y}`);
// [projected_B.x, projected_B.y] = canvas.grid.getCenter(projected_B.x, projected_B.y);
// log(`Snapped to ${projected_B.x}, ${projected_B.y}`);
// }
//
return [projected_A, projected_B];
}

function projectEast(A, B, height, distance) {
if(height === undefined) height = A.z - B.z;
if(distance === undefined) distance = window.libRuler.RulerUtilities.calculateDistance({x: A.x, y: A.y},
{x: B.x, y: B.y});
if(distance === undefined) distance = gridDistance(A, B);

// set A pointing east; B pointing south
const projected_A = {x: A.x + height, y: A.y};
Expand All @@ -160,15 +157,29 @@ function projectEast(A, B, height, distance) {

// if dnd5e 5-5-5 or 5-10-5, snap to nearest grid point
// origin should be fine if elevation is in increments. Otherwise, may need to be snapped. Leave for now.
if((canvas.grid.diagonalRule === "555" || canvas.grid.diagonalRule === "5105" || game.system.id === "pf2e")) {
log(`Snapping ${projected_B.x}, ${projected_B.y}`);
[projected_B.x, projected_B.y] = canvas.grid.getCenter(projected_B.x, projected_B.y);
log(`Snapped to ${projected_B.x}, ${projected_B.y}`);
}
// if((canvas.grid.diagonalRule === "555" || canvas.grid.diagonalRule === "5105" || game.system.id === "pf2e")) {
// log(`Snapping ${projected_B.x}, ${projected_B.y}`);
// [projected_B.x, projected_B.y] = canvas.grid.getCenter(projected_B.x, projected_B.y);
// log(`Snapped to ${projected_B.x}, ${projected_B.y}`);
// }

return [projected_A, projected_B];
}

function gridDistance(A, B) {
const use_grid = canvas.grid.diagonalRule === "555" ||
canvas.grid.diagonalRule === "5105" ||
game.system.id === "pf2e";

if(use_grid) {
const distance_segments = [{ray: new Ray(A, B)}];
return canvas.grid.measureDistances(distance_segments, { gridSpaces: true })[0];
}

return window.libRuler.RulerUtilities.calculateDistance({x: A.x, y: A.y},
{x: B.x, y: B.y})
}


/**
* Calculate a new point by projecting the elevated point back onto the 2-D surface
Expand Down

0 comments on commit 3e3cd01

Please sign in to comment.