diff --git a/module/data/actor/base.mjs b/module/data/actor/base.mjs index bdb810dd..2b74bf1c 100644 --- a/module/data/actor/base.mjs +++ b/module/data/actor/base.mjs @@ -1,5 +1,5 @@ import DHBaseActorSettings from '../../applications/sheets/api/actor-setting.mjs'; -import { createScrollText, getScrollTextData } from '../../helpers/utils.mjs'; +import { getScrollTextData } from '../../helpers/utils.mjs'; const resistanceField = (resistanceLabel, immunityLabel, reductionLabel) => new foundry.data.fields.SchemaField({ @@ -148,6 +148,6 @@ export default class BaseDataActor extends foundry.abstract.TypeDataModel { _onUpdate(changes, options, userId) { super._onUpdate(changes, options, userId); - createScrollText(this.parent, options.scrollingTextData); + if (options.scrollingTextData) this.parent.queueScrollText(options.scrollingTextData); } } diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 1b257db4..59286a7b 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -221,6 +221,8 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { super._onUpdate(changed, options, userId); updateLinkedItemApps(options, this.parent.sheet); - createScrollText(this.parent?.parent, options.scrollingTextData); + + if (this.parent?.parent && options.scrollingTextData) + this.parent.parent.queueScrollText(options.scrollingTextData); } } diff --git a/module/documents/actor.mjs b/module/documents/actor.mjs index 8e0b1c88..c4cc2207 100644 --- a/module/documents/actor.mjs +++ b/module/documents/actor.mjs @@ -1,10 +1,13 @@ import { emitAsGM, GMUpdateEvent } from '../systemRegistration/socket.mjs'; import { LevelOptionType } from '../data/levelTier.mjs'; import DHFeature from '../data/item/feature.mjs'; -import { damageKeyToNumber } from '../helpers/utils.mjs'; +import { createScrollText, damageKeyToNumber } from '../helpers/utils.mjs'; import DhCompanionLevelUp from '../applications/levelup/companionLevelup.mjs'; export default class DhpActor extends Actor { + #scrollTextQueue = []; + #scrollTextInterval; + /** * Return the first Actor active owner. */ @@ -27,7 +30,7 @@ export default class DhpActor extends Actor { /** @inheritDoc */ static migrateData(source) { - if(source.system?.attack && !source.system.attack.type) source.system.attack.type = "attack"; + if (source.system?.attack && !source.system.attack.type) source.system.attack.type = 'attack'; return super.migrateData(source); } @@ -572,19 +575,15 @@ export default class DhpActor extends Actor { if (armorSlotResult) { const { modifiedDamage, armorSpent, stressSpent } = armorSlotResult; updates.find(u => u.key === 'hitPoints').value = modifiedDamage; - if(armorSpent) { + if (armorSpent) { const armorUpdate = updates.find(u => u.key === 'armor'); - if(armorUpdate) - armorUpdate.value += armorSpent; - else - updates.push({ value: armorSpent, key: 'armor' }); + if (armorUpdate) armorUpdate.value += armorSpent; + else updates.push({ value: armorSpent, key: 'armor' }); } - if(stressSpent) { + if (stressSpent) { const stressUpdate = updates.find(u => u.key === 'stress'); - if(stressUpdate) - stressUpdate.value += stressSpent; - else - updates.push({ value: stressSpent, key: 'stress' }); + if (stressUpdate) stressUpdate.value += stressSpent; + else updates.push({ value: stressSpent, key: 'stress' }); } } } @@ -754,4 +753,23 @@ export default class DhpActor extends Actor { } } } + + queueScrollText(scrollingTextData) { + this.#scrollTextQueue.push(...scrollingTextData.map(data => () => createScrollText(this, data))); + if (!this.#scrollTextInterval) { + const scrollFunc = this.#scrollTextQueue.pop(); + scrollFunc?.(); + + const intervalFunc = () => { + const scrollFunc = this.#scrollTextQueue.pop(); + scrollFunc?.(); + if (this.#scrollTextQueue.length === 0) { + clearInterval(this.#scrollTextInterval); + this.#scrollTextInterval = null; + } + }; + + this.#scrollTextInterval = setInterval(intervalFunc.bind(this), 600); + } + } } diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index 47b6d904..dbf66ff4 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -371,17 +371,15 @@ export function getScrollTextData(resources, resource, key) { return { text, stroke, fill, direction }; } -export function createScrollText(actor, optionsData) { - if (actor && optionsData?.length) { +export function createScrollText(actor, data) { + if (actor) { actor.getActiveTokens().forEach(token => { - optionsData.forEach(data => { - const { text, ...options } = data; - canvas.interface.createScrollingText(token.getCenterPoint(), data.text, { - duration: 2000, - distance: token.h, - jitter: 0, - ...options - }); + const { text, ...options } = data; + canvas.interface.createScrollingText(token.getCenterPoint(), data.text, { + duration: 2000, + distance: token.h, + jitter: 0, + ...options }); }); }