|
| 1 | +const { DocumentUUIDField, NumberField, SetField, StringField } = foundry.data.fields; |
| 2 | + |
| 3 | +/** |
| 4 | + * @import { ApplyActiveEffectRegionBehaviorSystemData } from "./_types.mjs"; |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * The data model for a region behavior that applies active effects to certain tokens. |
| 9 | + * @extends {foundry.data.regionBehaviors.RegionBehaviorType<ApplyActiveEffectRegionBehaviorSystemData>} |
| 10 | + * @mixes ApplyActiveEffectRegionBehaviorSystemData |
| 11 | + */ |
| 12 | +export default class ApplyActiveEffect5eRegionBehaviorType extends foundry.data.regionBehaviors.RegionBehaviorType { |
| 13 | + |
| 14 | + /** @override */ |
| 15 | + static LOCALIZATION_PREFIXES = ["DND5E.REGIONBEHAVIORS.APPLYACTIVEEFFECT"]; |
| 16 | + |
| 17 | + /* ---------------------------------------- */ |
| 18 | + |
| 19 | + /** @override */ |
| 20 | + static defineSchema() { |
| 21 | + const dispositions = { ...foundry.applications.sheets.TokenConfig.TOKEN_DISPOSITIONS }; |
| 22 | + delete dispositions[CONST.TOKEN_DISPOSITIONS.SECRET]; |
| 23 | + return { |
| 24 | + effects: new SetField(new DocumentUUIDField({ type: "ActiveEffect", nullable: false })), |
| 25 | + dispositions: new SetField(new NumberField({ choices: dispositions })), |
| 26 | + sizes: new SetField(new StringField({ choices: () => CONFIG.DND5E.actorSizes })), |
| 27 | + types: new SetField(new StringField({ choices: () => CONFIG.DND5E.creatureTypes })) |
| 28 | + // TODO: Add FiltersField for arbitrary conditions once https://github.com/foundryvtt/dnd5e/issues/6672 is done |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + /* ---------------------------------------- */ |
| 33 | + /* Methods */ |
| 34 | + /* ---------------------------------------- */ |
| 35 | + |
| 36 | + /** |
| 37 | + * Check the conditions to decide if effects should be added to this token. |
| 38 | + * @param {TokenDocument5e} token The token to which to add the effects. |
| 39 | + * @returns {boolean} |
| 40 | + */ |
| 41 | + #evaluateConditions(token) { |
| 42 | + if ( this.dispositions.size && !this.dispositions.has(token.disposition) ) return false; |
| 43 | + if ( this.sizes.size && !this.sizes.has(token.actor.system.traits?.size) ) return false; |
| 44 | + if ( this.types.size && !this.types.has(token.actor.system.details?.type?.value) ) return false; |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + /* ---------------------------------------- */ |
| 49 | + |
| 50 | + /** |
| 51 | + * Apply the Active Effects when the Token enters the Region. |
| 52 | + * @param {RegionTokenEnterEvent} event |
| 53 | + * @this {ApplyActiveEffect5eRegionBehaviorType} |
| 54 | + */ |
| 55 | + static async #onTokenEnter(event) { |
| 56 | + if ( !event.user.isSelf ) return; |
| 57 | + const { token, movement } = event.data; |
| 58 | + const actor = token.actor; |
| 59 | + if ( !actor || !this.#evaluateConditions(token) ) return; |
| 60 | + const resumeMovement = movement ? token.pauseMovement() : undefined; |
| 61 | + const effects = await Promise.all(this.effects.map(fromUuid)); |
| 62 | + const toCreate = []; |
| 63 | + for ( const effect of effects ) { |
| 64 | + const data = effect.toObject(); |
| 65 | + delete data._id; |
| 66 | + if ( effect.compendium ) { |
| 67 | + data._stats.duplicateSource = null; |
| 68 | + data._stats.compendiumSource = effect.uuid; |
| 69 | + } else { |
| 70 | + data._stats.duplicateSource = effect.uuid; |
| 71 | + data._stats.compendiumSource = null; |
| 72 | + } |
| 73 | + data._stats.exportSource = null; |
| 74 | + data.origin = this.parent.uuid; |
| 75 | + toCreate.push(data); |
| 76 | + } |
| 77 | + if ( toCreate.length ) await actor.createEmbeddedDocuments("ActiveEffect", toCreate); |
| 78 | + await resumeMovement?.(); |
| 79 | + } |
| 80 | + |
| 81 | + /* ---------------------------------------- */ |
| 82 | + |
| 83 | + /** |
| 84 | + * Un-apply the Active Effects when the Token exists the Region. |
| 85 | + * @param {RegionTokenExitEvent} event |
| 86 | + * @this {ApplyActiveEffect5eRegionBehaviorType} |
| 87 | + */ |
| 88 | + static async #onTokenExit(event) { |
| 89 | + if ( !event.user.isSelf ) return; |
| 90 | + const { token, movement } = event.data; |
| 91 | + const actor = token.actor; |
| 92 | + if ( !actor ) return; |
| 93 | + const toDelete = []; |
| 94 | + for ( const effect of actor.effects ) { |
| 95 | + if ( effect.origin === this.parent.uuid ) { |
| 96 | + toDelete.push(effect.id); |
| 97 | + } |
| 98 | + } |
| 99 | + if ( !toDelete.length ) return; |
| 100 | + const resumeMovement = movement ? token.pauseMovement() : undefined; |
| 101 | + await actor.deleteEmbeddedDocuments("ActiveEffect", toDelete); |
| 102 | + await resumeMovement?.(); |
| 103 | + } |
| 104 | + |
| 105 | + /* ---------------------------------------- */ |
| 106 | + |
| 107 | + /** @override */ |
| 108 | + static events = { |
| 109 | + [CONST.REGION_EVENTS.TOKEN_ENTER]: this.#onTokenEnter, |
| 110 | + [CONST.REGION_EVENTS.TOKEN_EXIT]: this.#onTokenExit |
| 111 | + }; |
| 112 | +} |
0 commit comments