Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"name": "Indicator Distance",
"hint": "Set the multiplier for the indicator's distance. Math: Largest Size × (Multiplier / 2)."
},
"fasterAnimations": {
"name": "Speed Up Animations on Movement",
"hint": "Speeds up mirroring animations whenever the token is moved."
},
"combatOnly": {
"name": "Hide Indicator While Out Of Combat",
"hint": "Hides the indicator when no combat is active."
Expand Down
82 changes: 82 additions & 0 deletions src/module/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export class AboutFace {
this.indicatorDrawingType = game.settings.get("about-face", "indicatorDrawingType");
this.indicatorSize = game.settings.get("about-face", "sprite-type");
this._tokenRotation = false;
this.fasterAnimations = game.settings.get("about-face", "fasterAnimations");
this.pendingFlips = new Map();
this.flipTimeout = null;
}

get tokenRotation() {
Expand Down Expand Up @@ -115,6 +118,59 @@ export class AboutFace {
);
}
}

// ------------------------------------------------------------
// Delayed flip handling for multiple tokens
// ------------------------------------------------------------
async applyDelayedFlip(tokenDocument, mirrorKey, mirrorVal, duration) {
const tokenId = tokenDocument.id;
const now = Date.now();
this.pendingFlips.set(tokenId, {
mirrorKey,
mirrorVal,
timestamp: now,
tokenDocument,
duration
});
if (this.flipTimeout) {
clearTimeout(this.flipTimeout);
}
this.flipTimeout = setTimeout(() => {
this.applyPendingFlips();
}, 50);
}

async applyPendingFlips() {
const now = Date.now();
for (const [tokenId, flipData] of this.pendingFlips.entries()) {
if (now - flipData.timestamp > 500) {
this.pendingFlips.delete(tokenId);
continue;
}
const tokenDocument = flipData.tokenDocument;
const token = tokenDocument?.object;
if (!token || !token.visible) {
this.pendingFlips.delete(tokenId);
continue;
}
try {
const source = tokenDocument.toObject();
const updates = {};
updates[`texture.${flipData.mirrorKey}`] = source.texture[flipData.mirrorKey] * -1;
await tokenDocument.update(updates, {
animate: flipData.duration > 0,
animation: {
duration: flipData.duration,
linkToMovement: false
}
});
} catch(error) {
console.error(`About Face | Error applying flip for token ${tokenId}:`, error);
}
this.pendingFlips.delete(tokenId);
}
this.flipTimeout = null;
}
}

// HOOKS
Expand Down Expand Up @@ -211,6 +267,32 @@ export function onPreUpdateToken(tokenDocument, updates, options, userId) {
const source = tokenDocument.toObject();
updates[`texture.${mirrorKey}`] = source.texture[mirrorKey] * -1;
}

if (game.aboutFace.fasterAnimations) {
const distance = Math.hypot(position.x, position.y);
const action = tokenDocument.movementAction || "walk";
const actionConfig = CONFIG.Token.movement?.actions?.[action];
if (actionConfig?.teleport) return 0;

const { speedMultiplier = 1 } = tokenDocument;
const { movementSpeed } = actionConfig.getAnimationOptions(tokenDocument);

const gridSize = canvas.grid.size || 100;
const speedPxPerSec = movementSpeed * gridSize * speedMultiplier;

const duration = Math.clamp((distance / speedPxPerSec) * 1000, 125, 500);

if (canvas.tokens?.controlled?.length) {
delete updates[`texture.${mirrorKey}`];
game.aboutFace.applyDelayedFlip(tokenDocument, mirrorKey, mirrorVal, duration);
} else if (duration > 0) {
options.animation ||= {};
options.animation.duration = duration;
options.animation.linkToMovement = false;
} else {
options.animate = false;
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/module/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ export function registerSettings() {
type: Boolean,
});

game.settings.register(MODULE_ID, "fasterAnimations", {
name: "about-face.options.fasterAnimations.name",
hint: "about-face.options.fasterAnimations.hint",
scope: "world",
config: true,
default: true,
type: Boolean,
onChange: (value) => {
game.aboutFace.fasterAnimations = value;
},
});

game.settings.register(MODULE_ID, "flip-or-rotate", {
name: "about-face.options.flip-or-rotate.name",
hint: "about-face.options.flip-or-rotate.hint",
Expand Down