Skip to content

Commit

Permalink
Remove getCostForStep SpedProvider function in favor or the Enhance…
Browse files Browse the repository at this point in the history
…d Terrain Layer API
  • Loading branch information
manuelVo committed Oct 13, 2022
1 parent ff9b187 commit 46c5043
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
20 changes: 6 additions & 14 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,6 @@ export function getUnreachableColorFromSpeedProvider() {
}
}

export function getCostFromSpeedProvider(token, area, options) {
try {
if (currentSpeedProvider instanceof Function) {
return SpeedProvider.prototype.getCostForStep.call(undefined, token, area, options);
}
return currentSpeedProvider.getCostForStep(token, area, options);
} catch (e) {
console.error(e);
return 1;
}
}

export function getColorForDistanceAndToken(distance, token, ranges = null) {
if (!ranges) {
ranges = getRangesFromSpeedProvider(token);
Expand Down Expand Up @@ -159,8 +147,12 @@ export function getMovedDistanceFromToken(token) {
}

export function buildCostFunction(token, shape) {
return (x, y, costOptions = {}) =>
getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), costOptions);
return (x, y, costOptions = {}) => {
costOptions.token = token;
const area = getAreaFromPositionAndShape({x, y}, shape);
const costs = area.map(space => canvas.terrain.cost({x: space.x, y: space.y, costOptions}));
return costs.reduce((max, current) => Math.max(max, current));
};
}

export function registerModule(moduleId, speedProvider) {
Expand Down
21 changes: 0 additions & 21 deletions src/speed_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,6 @@ export class SpeedProvider {
return 0xff0000;
}

/**
* Returns the cost for a token to step into the specificed area.
* The area indicates the whole area that the token will occupy (for tokens larger than 1x1) the array will more than one entry.
* The return value should be an integer indicating a multiplicator by that the cost of that step should be increased.
* (1 is regular cost, 2 costs double, 3 costs triple, ...)
*
* Parameters:
* - options: An object used to configure Enhanced Terrain Layer's cost calculation. Ex: If options.ignoreGrid is set to true, then Euclidean measurement can be forced on a gridded map.
*
* This function is only called if the Enhanced Terrain Layer and Terrain Ruler modules are enabled.
*
* Implementing this method is optional and only needs to be done if you want to provide a custom cost function (for example to allow tokens to ignore difficult terrain)
*/
getCostForStep(token, area, options = {}) {
// Lookup the cost for each square occupied by the token
options.token = token;
const costs = area.map(space => terrainRuler.getCost(space.x, space.y, options));
// Return the maximum of the costs
return costs.reduce((max, current) => Math.max(max, current));
}

/**
* Returns a boolean indicating whether this token will use a Ruler or not.
* If this is returns `false` for a token Drag Ruler will be disabled for that token. Dragging a token for which this function
Expand Down

0 comments on commit 46c5043

Please sign in to comment.