diff --git a/scripts/logic.js b/scripts/logic.js index 56c8058..a6a7fad 100644 --- a/scripts/logic.js +++ b/scripts/logic.js @@ -1,5 +1,21 @@ import { IndicatorMode, MODULE_ID } from "./settings.js"; +function getDirection(token) { + const facingDirection = token.getFlag(MODULE_ID, "facingDirection") || game.settings.get(MODULE_ID, "facing-direction"); + const directions = { + right: 0, + left: 180, + down: 270, + up: 90, + }; + return directions[facingDirection]; +} + +function getFlipOrRotation(token) { + const tokenFlipOrRotate = token.getFlag(MODULE_ID, "flipOrRotate") || "global"; + return tokenFlipOrRotate != "global" ? tokenFlipOrRotate : game.settings.get(MODULE_ID, "flip-or-rotate"); +} + export function drawAboutFaceIndicator(wrapped, ...args) { if (!canvas.scene.getFlag(MODULE_ID, "sceneEnabled")) { wrapped(...args); @@ -9,20 +25,9 @@ export function drawAboutFaceIndicator(wrapped, ...args) { let dir = this.data.flags["about-face"]?.direction; if (dir === undefined || dir === null) { dir = 90; - const flipOrRotate = game.settings.get(MODULE_ID, "flip-or-rotate"); + const flipOrRotate = getFlipOrRotation(this); if (flipOrRotate !== "rotate") { - const facingDirection = game.settings.get(MODULE_ID, "facing-direction"); - switch (facingDirection) { - case "right": - dir = 0; - break; - case "left": - dir = 180; - break; - case "down": - dir = 270; - break; - } + dir = getDirection(this); } } const indicatorSize = [1, 1.5][game.settings.get(MODULE_ID, "sprite-type")]; @@ -99,13 +104,20 @@ export function onPreCreateToken(document, data, options, userId) { export function onPreUpdateToken(token, updates) { if (!canvas.scene.getFlag(MODULE_ID, "sceneEnabled")) return; - const tokenFlipOrRotate = token.getFlag(MODULE_ID, "flipOrRotate") || "global"; - const flipOrRotate = tokenFlipOrRotate != "global" ? tokenFlipOrRotate : game.settings.get(MODULE_ID, "flip-or-rotate"); - if (flipOrRotate == "rotate" && "rotation" in updates) { + const flipOrRotate = getFlipOrRotation(token); + if ("rotation" in updates) { var dir = updates.rotation + 90; //store the direction in the token data if (!updates.flags) updates.flags = {}; updates.flags[MODULE_ID] = { direction: dir }; + if (flipOrRotate != "rotate") { + if (flipOrRotate == "flip-h" && [180, 360].includes(dir)) { + updates.mirrorX = !token.data.mirrorX; + } else if (flipOrRotate == "flip-v" && [90, 270].includes(dir)) { + updates.mirrorY = !token.data.mirrorY; + } + return; + } } else if ("x" in updates || "y" in updates) { //get previews and new positions const prevPos = { x: token.data.x, y: token.data.y }; @@ -117,11 +129,13 @@ export function onPreUpdateToken(token, updates) { //store the direction in the token data if (!updates.flags) updates.flags = {}; updates.flags[MODULE_ID] = { direction: dir }; - if (flipOrRotate) { - if (flipOrRotate == "flip-h") diffY = 0; - else if (flipOrRotate == "flip-v") diffX = 0; - dir = (Math.atan2(diffY, diffX) * 180) / Math.PI; - if (!(diffY || diffX)) return; + if (flipOrRotate != "rotate") { + if (flipOrRotate == "flip-h" && diffX != 0) { + updates.mirrorX = !token.data.mirrorX; + } else if (flipOrRotate == "flip-v" && diffY != 0) { + updates.mirrorY = !token.data.mirrorY; + } + return; } } else return; //update the rotation of the token