From eca04de225cf1fb43459c48cd34619312fb595e9 Mon Sep 17 00:00:00 2001 From: p4vl0 Date: Sun, 23 Aug 2026 12:49:56 +0300 Subject: [PATCH 1/3] Update logic.js added new logic to support new v13-v14 flip speed --- src/module/logic.js | 116 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/src/module/logic.js b/src/module/logic.js index 9494135..4c500ab 100644 --- a/src/module/logic.js +++ b/src/module/logic.js @@ -1,5 +1,42 @@ import { IndicatorMode, MODULE_ID } from "./settings.js"; +// ============================================================ +// Helper: compute flip animation duration +// ============================================================ +function getFlipDuration(distance, tokenDocument) { + if (distance <= 0) return 0; + + const action = tokenDocument.movementAction || "walk"; + const actionConfig = CONFIG.Token.movement?.actions?.[action]; + + if (actionConfig?.teleport) { + return 0; + } + + let movementSpeed = tokenDocument.movementSpeed; + if (!movementSpeed || movementSpeed <= 0) { + movementSpeed = CONFIG.Token.movement.defaultSpeed || 1; + } + const speedMultiplier = tokenDocument.speedMultiplier ?? 1; + + const actionSpeedFactors = { + crawl: 0.5, + swim: 0.5, + climb: 0.5, + }; + const actionFactor = actionSpeedFactors[action] ?? 1; + + const gridSize = canvas.grid.size || 100; + const speedPxPerSec = movementSpeed * gridSize * speedMultiplier * actionFactor; + + let durationMs = (distance / speedPxPerSec) * 1000; + const minDuration = 180; + const maxDuration = 2000; + durationMs = Math.max(minDuration, Math.min(durationMs, maxDuration)); + + return durationMs; +} + export class AboutFace { constructor() { this.combatOnly = game.settings.get("about-face", "combatOnly"); @@ -9,6 +46,9 @@ export class AboutFace { this.indicatorDrawingType = game.settings.get("about-face", "indicatorDrawingType"); this.indicatorSize = game.settings.get("about-face", "sprite-type"); this._tokenRotation = false; + // Delayed flips for multiple tokens + this.pendingFlips = new Map(); + this.flipTimeout = null; } get tokenRotation() { @@ -115,6 +155,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 @@ -210,6 +303,27 @@ export function onPreUpdateToken(tokenDocument, updates, options, userId) { if ((texture[mirrorKey] < 0 && !mirrorVal) || (texture[mirrorKey] > 0 && mirrorVal)) { const source = tokenDocument.toObject(); updates[`texture.${mirrorKey}`] = source.texture[mirrorKey] * -1; + + // --- NEW: compute duration and handle animation --- + const distance = Math.hypot(position.x || 0, position.y || 0); + const duration = getFlipDuration(distance, tokenDocument); + const controlledCount = canvas.tokens?.controlled?.length || 0; + + if (controlledCount > 1) { + delete updates[`texture.${mirrorKey}`]; + game.aboutFace.applyDelayedFlip(tokenDocument, mirrorKey, mirrorVal, duration); + } else { + if (duration > 0) { + options.animation = options.animation || {}; + options.animation.duration = duration; + if (game.version && parseFloat(game.version) >= 13) { + options.animation.linkToMovement = false; + } + } else { + options.animate = false; + } + } + // --- END NEW --- } } } @@ -281,4 +395,4 @@ function getMirror(tokenDocument, position = {}) { if (y > 0) return [mirrorY, false]; } return []; -} +} \ No newline at end of file From a79d529dc90a925aa746a675b5a18fd2261a3f46 Mon Sep 17 00:00:00 2001 From: Matheus Clemente Date: Mon, 24 Aug 2026 18:59:17 -0300 Subject: [PATCH 2/3] linting --- src/module/logic.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/module/logic.js b/src/module/logic.js index 4c500ab..4ded4f1 100644 --- a/src/module/logic.js +++ b/src/module/logic.js @@ -201,7 +201,7 @@ export class AboutFace { linkToMovement: false } }); - } catch (error) { + } catch(error) { console.error(`About Face | Error applying flip for token ${tokenId}:`, error); } this.pendingFlips.delete(tokenId); @@ -312,16 +312,14 @@ export function onPreUpdateToken(tokenDocument, updates, options, userId) { if (controlledCount > 1) { delete updates[`texture.${mirrorKey}`]; game.aboutFace.applyDelayedFlip(tokenDocument, mirrorKey, mirrorVal, duration); - } else { - if (duration > 0) { - options.animation = options.animation || {}; - options.animation.duration = duration; - if (game.version && parseFloat(game.version) >= 13) { - options.animation.linkToMovement = false; - } - } else { - options.animate = false; + } else if (duration > 0) { + options.animation = options.animation || {}; + options.animation.duration = duration; + if (game.version && parseFloat(game.version) >= 13) { + options.animation.linkToMovement = false; } + } else { + options.animate = false; } // --- END NEW --- } @@ -395,4 +393,4 @@ function getMirror(tokenDocument, position = {}) { if (y > 0) return [mirrorY, false]; } return []; -} \ No newline at end of file +} From 94c0b8d53fd08c5ab38918cb3d86b085eb6c6284 Mon Sep 17 00:00:00 2001 From: Matheus Clemente Date: Sat, 10 Oct 2026 04:25:10 -0300 Subject: [PATCH 3/3] Refactor --- src/lang/en.json | 4 +++ src/module/logic.js | 66 ++++++++++++------------------------------ src/module/settings.js | 12 ++++++++ 3 files changed, 34 insertions(+), 48 deletions(-) diff --git a/src/lang/en.json b/src/lang/en.json index f4bc49f..3f01b97 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -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." diff --git a/src/module/logic.js b/src/module/logic.js index 4ded4f1..38b55be 100644 --- a/src/module/logic.js +++ b/src/module/logic.js @@ -1,42 +1,5 @@ import { IndicatorMode, MODULE_ID } from "./settings.js"; -// ============================================================ -// Helper: compute flip animation duration -// ============================================================ -function getFlipDuration(distance, tokenDocument) { - if (distance <= 0) return 0; - - const action = tokenDocument.movementAction || "walk"; - const actionConfig = CONFIG.Token.movement?.actions?.[action]; - - if (actionConfig?.teleport) { - return 0; - } - - let movementSpeed = tokenDocument.movementSpeed; - if (!movementSpeed || movementSpeed <= 0) { - movementSpeed = CONFIG.Token.movement.defaultSpeed || 1; - } - const speedMultiplier = tokenDocument.speedMultiplier ?? 1; - - const actionSpeedFactors = { - crawl: 0.5, - swim: 0.5, - climb: 0.5, - }; - const actionFactor = actionSpeedFactors[action] ?? 1; - - const gridSize = canvas.grid.size || 100; - const speedPxPerSec = movementSpeed * gridSize * speedMultiplier * actionFactor; - - let durationMs = (distance / speedPxPerSec) * 1000; - const minDuration = 180; - const maxDuration = 2000; - durationMs = Math.max(minDuration, Math.min(durationMs, maxDuration)); - - return durationMs; -} - export class AboutFace { constructor() { this.combatOnly = game.settings.get("about-face", "combatOnly"); @@ -46,7 +9,7 @@ export class AboutFace { this.indicatorDrawingType = game.settings.get("about-face", "indicatorDrawingType"); this.indicatorSize = game.settings.get("about-face", "sprite-type"); this._tokenRotation = false; - // Delayed flips for multiple tokens + this.fasterAnimations = game.settings.get("about-face", "fasterAnimations"); this.pendingFlips = new Map(); this.flipTimeout = null; } @@ -303,25 +266,32 @@ export function onPreUpdateToken(tokenDocument, updates, options, userId) { if ((texture[mirrorKey] < 0 && !mirrorVal) || (texture[mirrorKey] > 0 && mirrorVal)) { const source = tokenDocument.toObject(); updates[`texture.${mirrorKey}`] = source.texture[mirrorKey] * -1; + } - // --- NEW: compute duration and handle animation --- - const distance = Math.hypot(position.x || 0, position.y || 0); - const duration = getFlipDuration(distance, tokenDocument); - const controlledCount = canvas.tokens?.controlled?.length || 0; + 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; - if (controlledCount > 1) { + 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 || {}; + options.animation ||= {}; options.animation.duration = duration; - if (game.version && parseFloat(game.version) >= 13) { - options.animation.linkToMovement = false; - } + options.animation.linkToMovement = false; } else { options.animate = false; } - // --- END NEW --- } } } diff --git a/src/module/settings.js b/src/module/settings.js index a4ec74e..79550a3 100644 --- a/src/module/settings.js +++ b/src/module/settings.js @@ -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",