diff --git a/CHANGELOG.md b/CHANGELOG.md index 1854291..b70cc5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [1.1.4](https://github.com/ChasarooniZ/PF2e-Reactive-Token-Ring/compare/1.1.3...1.1.4) - Only on Target +- Tokens only flash when targetted now +- Minor Code Cleanup ## [1.1.3](https://github.com/ChasarooniZ/PF2e-Reactive-Token-Ring/compare/1.1.2...1.1.3) - Dragonbane - Support for Dragonbane - Drakar och Demoner (@xdy) diff --git a/scripts/module.js b/scripts/module.js index c976479..9c6cf62 100644 --- a/scripts/module.js +++ b/scripts/module.js @@ -6,7 +6,7 @@ import { isHealing } from "./systemCompatability.js"; // Define color constants const COLORS = { GREEN: "#ADFF2F", - RED: "#ff0000", + RED: "#FF0000", PURPLE: "#9370DB", WHITE: "#FFFFFF", DEEPSKYBLUE: "#00BFFF", @@ -15,37 +15,27 @@ const COLORS = { }; // Initialize module settings -Hooks.once("init", function () { - registerSettings(); -}); +Hooks.once("init", registerSettings); // Set up main functionality when Foundry VTT is ready -Hooks.once("ready", async function () { +Hooks.once("ready", async () => { // Handle actor updates Hooks.on("preUpdateActor", async (actor, update, status, _userID) => { - if (status.diff) { - const isHeal = isHealing(actor, update, status); - const token = canvas.tokens.placeables.find( - (t) => t.actor.id === actor.id - ); - - if (isHeal !== undefined) { - const color = isHeal ? COLORS.GREEN : COLORS.RED; - const situation = isHeal ? "heal" : "damage"; - flashColor( - token, - color, - getAnimationChanges(situation, { actor, status }) - ); - } - } + if (!status.diff) return; + const isHeal = isHealing(actor, update, status); + if (isHeal !== undefined) return; + const token = canvas.tokens.placeables.find((t) => t.actor.id === actor.id); + const color = isHeal ? COLORS.GREEN : COLORS.RED; + const situation = isHeal ? "heal" : "damage"; + flashColor(token, color, getAnimationChanges(situation, { actor, status })); }); // Handle token targeting - Hooks.on("targetToken", async (user, token) => { + Hooks.on("targetToken", async (user, token, isTargetting) => { if ( - user.id === game.user.id || - game.settings.get(MODULE_ID, "target.share-flash") + isTargetting && + (user.id === game.user.id || + game.settings.get(MODULE_ID, "target.share-flash")) ) { const color = game.settings.get(MODULE_ID, "target.player-color") ? user.color.css @@ -98,7 +88,7 @@ async function flashColor(token, color, animationOverride = {}) { * @param {Object} data - Additional data for the animation * @returns {Object} Animation changes */ -function getAnimationChanges(situation, data) { +function getAnimationChanges(situation, { actor, status }) { const baseDuration = game.settings.get(MODULE_ID, "duration"); const result = {}; @@ -108,7 +98,7 @@ function getAnimationChanges(situation, data) { ) { if (game.settings.get(MODULE_ID, "damage-heal.scale-on-%-hp")) { const percentHealth = Math.abs( - data.status.damageTaken / data.actor.system.attributes.hp.max + status.damageTaken / actor.system.attributes.hp.max ); result.duration = getDurationMultiplier(percentHealth) * baseDuration; } @@ -123,11 +113,9 @@ function getAnimationChanges(situation, data) { * @returns {number} A value between 1 and 4 to scale duration */ function getDurationMultiplier(percentHealth) { - return ( - 1 + - ((Math.min(Math.max(0.1, percentHealth), 0.5) - 0.1) * (4 - 1)) / - (0.5 - 0.1) - ); + const clampedHealth = Math.min(Math.max(0.1, percentHealth), 0.5); + const scaledHealth = ((clampedHealth - 0.1) * 3) / 0.4; + return 1 + scaledHealth; } // Token ring effects (borrowed from DnD 5e system) diff --git a/scripts/systemCompatability.js b/scripts/systemCompatability.js index 2ccba4e..e3c049e 100644 --- a/scripts/systemCompatability.js +++ b/scripts/systemCompatability.js @@ -14,10 +14,13 @@ export function isHealing(actor, update, status) { if (!keys.statusDamagePath) { const actorHP = foundry.utils.getProperty(actor, keys.hpPath); return updateHP > actorHP === keys.zeroIsBad; + } else { + const damageTaken = foundry.utils.getProperty( + status, + keys.statusDamagePath + ); + return damageTaken > 0 !== keys.zeroIsBad; } - - const damageTaken = foundry.utils.getProperty(status, keys.statusDamagePath); - return damageTaken > 0 !== keys.zeroIsBad; } /**