diff --git a/module/applications/ui/combatTracker.mjs b/module/applications/ui/combatTracker.mjs index 38e6802c..39abaee8 100644 --- a/module/applications/ui/combatTracker.mjs +++ b/module/applications/ui/combatTracker.mjs @@ -1,6 +1,6 @@ import { AdversaryBPPerEncounter } from '../../config/encounterConfig.mjs'; import { expireActiveEffects } from '../../helpers/utils.mjs'; -import { spotlightToken } from '../../macros/spotlightCombatant.mjs'; +import { clearPreviousSpotlight } from '../../macros/spotlightCombatant.mjs'; export default class DhCombatTracker extends foundry.applications.sidebar.tabs.CombatTracker { static DEFAULT_OPTIONS = { @@ -190,7 +190,7 @@ export default class DhCombatTracker extends foundry.applications.sidebar.tabs.C await combatant.update(update); if (!combatant.token) return; - spotlightToken(combatant.token); + clearPreviousSpotlight(); } async clearTurn() { diff --git a/module/canvas/placeables/token.mjs b/module/canvas/placeables/token.mjs index 2af2bf6f..77d8c3f4 100644 --- a/module/canvas/placeables/token.mjs +++ b/module/canvas/placeables/token.mjs @@ -20,7 +20,9 @@ export default class DhTokenPlaceable extends foundry.canvas.placeables.Token { .get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.SpotlightTracker) .spotlightedTokens.has(this.document.uuid); - const markerActive = markersEnabled && spotlighted; + const turnIsSet = game.combat?.turn !== null; + const isTurn = game.combat?.combatant?.tokenId === this.id; + const markerActive = markersEnabled && turnIsSet ? isTurn : spotlighted; // Activate a Turn Marker if (markerActive) { diff --git a/module/macros/spotlightCombatant.mjs b/module/macros/spotlightCombatant.mjs index 6d4c63fc..43f87d1f 100644 --- a/module/macros/spotlightCombatant.mjs +++ b/module/macros/spotlightCombatant.mjs @@ -3,42 +3,44 @@ * @param {TokenDocument} token - The token to spotlight * @returns {void} */ -const spotlightCombatantMacro = token => { +const spotlightCombatantMacro = async token => { if (!token) return ui.notifications.error(game.i18n.localize('DAGGERHEART.MACROS.Spotlight.errors.noTokenSelected')); - if (game.combat && token.combatant) { - ui.combat.setCombatantSpotlight(token.combatant.id); + const combatantCombat = game.combats.find(combat => + combat.combatants.some(x => x.token && x.token.id === token.document.id) + ); + if (combatantCombat) { + const combatant = combatantCombat.combatants.find(x => x.token.id === token.document.id); + if (!combatantCombat.active) await combatantCombat.activate(); + if (combatantCombat.combatant?.id !== combatant.id) ui.combat.setCombatantSpotlight(combatant.id); } else { - if (game.combat) ui.combat.clearTurn(); - spotlightToken(token.document); + if (game.combat) await ui.combat.clearTurn(); + + const spotlightTracker = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.SpotlightTracker); + const isSpotlighted = spotlightTracker.spotlightedTokens.has(token.document.uuid); + if (!isSpotlighted) await clearPreviousSpotlight(); + + spotlightTracker.updateSource({ + spotlightedTokens: isSpotlighted ? [] : [token.document.uuid] + }); + + await game.settings.set(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.SpotlightTracker, spotlightTracker); + token.renderFlags.set({ refreshTurnMarker: true }); } }; -/** - * Spotlight a token on the canvas. - * @param {TokenDocument} token - The token to spotlight - * @returns {void} - */ -export const spotlightToken = async token => { +export const clearPreviousSpotlight = async () => { const spotlightTracker = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.SpotlightTracker); - let previouslySpotlightedUuid = null; - const isSpotlighted = spotlightTracker.spotlightedTokens.has(token.uuid); - if (!isSpotlighted && spotlightTracker.spotlightedTokens.size > 0) { - previouslySpotlightedUuid = spotlightTracker.spotlightedTokens.first(); - } - - spotlightTracker.updateSource({ - spotlightedTokens: isSpotlighted ? [] : [token.uuid] - }); + const previouslySpotlightedUuid = + spotlightTracker.spotlightedTokens.size > 0 ? spotlightTracker.spotlightedTokens.first() : null; + if (!previouslySpotlightedUuid) return; + spotlightTracker.updateSource({ spotlightedTokens: [] }); await game.settings.set(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.SpotlightTracker, spotlightTracker); - token.object.renderFlags.set({ refreshTurnMarker: true }); - if (previouslySpotlightedUuid) { - const previousToken = await foundry.utils.fromUuid(previouslySpotlightedUuid); - previousToken.object.renderFlags.set({ refreshTurnMarker: true }); - } + const previousToken = await foundry.utils.fromUuid(previouslySpotlightedUuid); + previousToken.object.renderFlags.set({ refreshTurnMarker: true }); }; export default spotlightCombatantMacro;