Skip to content

Commit

Permalink
Merge branch 'feature/speed-colors' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Jun 1, 2024
2 parents f30d109 + 26f8446 commit d473387
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Fix pathfinding.
Change the keybind for teleport to "F" (fast-forward) to avoid collision with arrow-key usage on the canvas. Switch to requiring "F" to be held when the user triggers the move to get teleport, to avoid weirdness with the drag still being active when using a separate trigger key.
Catch error if waypoint is not added in `_addWaypoint`.
Correct error when sending ruler data from one user to another.
Move Maximum speed category to `CONFIG.elevationruler.SPEED.CATEGORIES`. Should now be possible to define specific colors per user in the CONFIG, so long as category names are same.

# 0.9.1
Fix errors when using the ruler on gridless scenes.
Expand Down
33 changes: 16 additions & 17 deletions scripts/Ruler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* globals
canvas,
Color,
CONFIG,
CONST,
foundry,
Expand All @@ -15,7 +16,7 @@ export const PATCHES = {};
PATCHES.BASIC = {};
PATCHES.SPEED_HIGHLIGHTING = {};

import { SPEED, MODULE_ID, MaximumSpeedCategory } from "./const.js";
import { SPEED, MODULE_ID } from "./const.js";
import { Settings } from "./settings.js";
import { Ray3d } from "./geometry/3d/Ray3d.js";
import { Point3d } from "./geometry/3d/Point3d.js";
Expand Down Expand Up @@ -100,6 +101,7 @@ function _getMeasurementData(wrapper) {
B: s.ray.B
};
newObj.label = Boolean(s.label);
newObj.speed ??= newObj.speed.name;
return newObj;
});

Expand Down Expand Up @@ -131,8 +133,7 @@ function update(wrapper, data) {
// Reconstruct segments.
if ( myData._segments ) this.segments = myData._segments.map(s => {
s.ray = new Ray3d(s.ray.A, s.ray.B);
if ( s.speed ) s.speed.color = s.speed.name === "Maximum"
? MaximumSpeedCategory.color : Color.from(s.speed.color);
s.speed ??= SPEED.CATEGORIES.find(s => s.name === s.speed);
return s;
});

Expand Down Expand Up @@ -405,20 +406,17 @@ function _computeTokenSpeed() {
let segment;

// Debugging
// if ( this.segments[0].moveDistance > 25 ) log(`${this.segments[0].moveDistance}`);
// if ( this.segments[0].moveDistance > 30 ) log(`${this.segments[0].moveDistance}`);
// if ( this.segments[0].moveDistance > 50 ) log(`${this.segments[0].moveDistance}`);
if ( this.segments[0].moveDistance > 60 ) log(`${this.segments[0].moveDistance}`);
if ( CONFIG[MODULE_ID].debug ) {
if ( this.segments[0].moveDistance > 25 ) log(`${this.segments[0].moveDistance}`);
if ( this.segments[0].moveDistance > 30 ) log(`${this.segments[0].moveDistance}`);
if ( this.segments[0].moveDistance > 50 ) log(`${this.segments[0].moveDistance}`);
if ( this.segments[0].moveDistance > 60 ) log(`${this.segments[0].moveDistance}`);
}

// Progress through each speed attribute in turn.
const categoryIter = [...SPEED.CATEGORIES, MaximumSpeedCategory].values();
const maxDistFn = (token, speedCategory, tokenSpeed) => {
if ( speedCategory.name === "Maximum" ) return Number.POSITIVE_INFINITY;
return SPEED.maximumCategoryDistance(token, speedCategory, tokenSpeed);
};

const categoryIter = [...SPEED.CATEGORIES].values();
let speedCategory = categoryIter.next().value;
let maxDistance = maxDistFn(token, speedCategory, tokenSpeed);
let maxDistance = SPEED.maximumCategoryDistance(token, speedCategory, tokenSpeed);

// Determine which speed category we are starting with
// Add in already moved combat distance and determine the starting category
Expand All @@ -432,10 +430,11 @@ function _computeTokenSpeed() {

while ( (segment = this.segments[s]) ) {
// Skip speed categories that do not provide a distance larger than the last.
while ( speedCategory.name !== "Maximum" && maxDistance <= minDistance ) {
while ( speedCategory && maxDistance <= minDistance ) {
speedCategory = categoryIter.next().value;
maxDistance = maxDistFn(token, speedCategory, tokenSpeed);
maxDistance = SPEED.maximumCategoryDistance(token, speedCategory, tokenSpeed);
}
if ( !speedCategory ) speedCategory = SPEED.CATEGORIES.at(-1);

segment.speed = speedCategory;
let newPrevDiagonal = _measureSegment(segment, token, numPrevDiagonal);
Expand Down Expand Up @@ -697,7 +696,7 @@ function decrementElevation() {
* Move the token and stop the ruler measurement
* @returns {boolean} False if the movement did not occur
*/
async function teleport(context) {
async function teleport(_context) {
if ( this._state !== this.constructor.STATES.MEASURING ) return false;
if ( !this._canMove(this.token) ) return false;

Expand Down
24 changes: 7 additions & 17 deletions scripts/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ const DashSpeedCategory = {
multiplier: 2
};

const MaximumSpeedCategory = {
Name: "Maximum",
color: Color.from(0xff0000),
multiplier: Number.POSITIVE_INFINITY
}

export const SPEED = {
/**
* Object of strings indicating where on the actor to locate the given attribute.
Expand All @@ -110,13 +116,7 @@ export const SPEED = {
* in the first category is the next category considered.
* @type {SpeedCategory[]}
*/
CATEGORIES: [WalkSpeedCategory, DashSpeedCategory],

/**
* Color to use once all SpeedCategory distances have been exceeded.
* @type {Color}
*/
MAXIMUM_COLOR: Color.from(0xff0000),
CATEGORIES: [WalkSpeedCategory, DashSpeedCategory, MaximumSpeedCategory],

// Use Font Awesome font unicode instead of basic unicode for displaying terrain symbol.

Expand All @@ -134,14 +134,6 @@ export const SPEED = {
terrainSymbol: "🥾"
};

export const MaximumSpeedCategory = {
name: "Maximum",
multiplier: Number.POSITIVE_INFINITY
};

Object.defineProperty(MaximumSpeedCategory, "color", {
get: () => SPEED.MAXIMUM_COLOR
});

/**
* Given a token, get the maximum distance the token can travel for a given type.
Expand Down Expand Up @@ -179,8 +171,6 @@ Hooks.once("init", function() {
DashSpeedCategory.multiplier = defaultDashMultiplier();
});


/* eslint-disable no-multi-spaces */
export function defaultHPAttribute() {
switch ( game.system.id ) {
case "dnd5e": return "actor.system.attributes.hp.value";
Expand Down
2 changes: 2 additions & 0 deletions scripts/segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ export async function _animateSegment(token, segment, destination) {
log(`Updating ${token.name} destination from ({${token.document.x},${token.document.y}) to (${destination.x},${destination.y}) for segment (${segment.ray.A.x},${segment.ray.A.y})|(${segment.ray.B.x},${segment.ray.B.y})`);

// If the segment is teleporting and the segment destination is not a waypoint or ruler destination, skip.
// Doesn't work because _animateMovement stops the movement if the token does not make it to the
// next waypoint.
// if ( segment.teleport
// && !(segment.B.x === this.destination.x && segment.B.y === this.destination.y )
// && !this.waypoints.some(w => segment.B.x === w.x && segment.B.y === w.y) ) return;
Expand Down

0 comments on commit d473387

Please sign in to comment.