Skip to content

Commit

Permalink
Show remaining AP when measuring from controllable token
Browse files Browse the repository at this point in the history
Closes #65.
  • Loading branch information
kmoschcau committed Nov 27, 2021
1 parent 202d97a commit f8e859c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- ability to create macros from unowned weapons
- ability to create weapon macros from source provided in a script macro
([#60](https://github.com/Wasteland-Ventures-Group/WV-VTT-module/issues/60))
- a remaining AP on movement display to the ruler labels, when measuring from a
controllable token
([#65](https://github.com/Wasteland-Ventures-Group/WV-VTT-module/issues/65))

### Changed

Expand Down
88 changes: 51 additions & 37 deletions src/typescript/foundryOverrides/wvRuler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class WvRuler extends Ruler {
destination: Point,
{ gridSpaces }: { gridSpaces?: boolean } = {}
): Ruler.Segment[] {
return replaceLabels(super.measure(destination, { gridSpaces }));
return this.replaceLabels(super.measure(destination, { gridSpaces }));
}

override async moveToken(): Promise<false | undefined> {
Expand Down Expand Up @@ -80,49 +80,63 @@ export default class WvRuler extends Ruler {

return super.moveToken();
}
}

/**
* Replace the labels and texts of the given Ruler segments with added movement
* information.
*/
export function replaceLabels(segments: Ruler.Segment[]): Ruler.Segment[] {
let totalDistance = 0;
for (const segment of segments) {
const distance = segment.distance;
totalDistance += distance;

const text = getLabel(distance, totalDistance, segment.last);

segment.text = text;
if (segment.label instanceof PreciseText) {
segment.label.text = text;

if (segment.label.style instanceof PIXI.TextStyle)
segment.label.style.align = "left";
/**
* Replace the labels and texts of the given Ruler segments with added movement
* information.
*/
replaceLabels(segments: Ruler.Segment[]): Ruler.Segment[] {
let totalDistance = 0;
for (const segment of segments) {
const distance = segment.distance;
totalDistance += distance;

const text = this.getLabel(distance, totalDistance, segment.last);

segment.text = text;
if (segment.label instanceof PreciseText) {
segment.label.text = text;

if (segment.label.style instanceof PIXI.TextStyle)
segment.label.style.align = "left";
}
}

return segments;
}

return segments;
}
/** Get the label for a segment. */
getLabel(
segmentDistance: number,
totalDistance: number,
isTotal: boolean
): string {
if (!canvas?.scene) throw new Error("There was no canvas or scene!");

/** Get the label for a segment. */
function getLabel(
segmentDistance: number,
totalDistance: number,
isTotal: boolean
): string {
if (!canvas?.scene) throw new Error("There was no canvas or scene!");
const units = canvas.scene.data.gridUnits;
const apUnit = getGame().i18n.localize("wv.ruler.apCostUnit");
const segmentApUse = getApUse(segmentDistance);
const totalApUse = getApUse(totalDistance);

const units = canvas.scene.data.gridUnits;
const apUnit = getGame().i18n.localize("wv.ruler.apCostUnit");
let label = `${Math.round(segmentDistance * 100) / 100} ${units}`;
if (isTotal)
label += ` [${Math.round(totalDistance * 100) / 100} ${units}]`;

let label = `${Math.round(segmentDistance * 100) / 100} ${units}`;
if (isTotal) label += ` [${Math.round(totalDistance * 100) / 100} ${units}]`;
label += "\n";
label += `${segmentApUse} ${apUnit}`;
if (isTotal) label += ` [${totalApUse} ${apUnit}]`;

label += "\n";
label += `${getApUse(segmentDistance)} ${apUnit}`;
if (isTotal) label += ` [${getApUse(totalDistance)} ${apUnit}]`;
const token = this._getMovementToken();
if (token?.actor instanceof WvActor) {
const currentAp = token.actor.actionPoints.value;
const segmentApRemaining = currentAp - segmentApUse;
const totalApRemaining = currentAp - totalApUse;

label += "\n";
label += `${segmentApRemaining} ${apUnit}`;
if (isTotal) label += ` [${totalApRemaining} ${apUnit}]`;
}

return label;
return label;
}
}

0 comments on commit f8e859c

Please sign in to comment.