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 9494135..38b55be 100644 --- a/src/module/logic.js +++ b/src/module/logic.js @@ -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() { @@ -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 @@ -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; + } + } } } 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",