mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-18 16:09:03 +01:00
add weapons to attachables
This commit is contained in:
parent
4e304adde7
commit
f78cf12f6e
3 changed files with 248 additions and 1 deletions
|
|
@ -66,6 +66,7 @@ export default class DHWeapon extends BaseDataItem {
|
|||
}
|
||||
}
|
||||
}),
|
||||
attached: new fields.ArrayField(new fields.DocumentUUIDField({ type: "Item", nullable: true })),
|
||||
actions: new fields.ArrayField(new ActionField())
|
||||
};
|
||||
}
|
||||
|
|
@ -78,6 +79,11 @@ export default class DHWeapon extends BaseDataItem {
|
|||
const allowed = await super._preUpdate(changes, options, user);
|
||||
if (allowed === false) return false;
|
||||
|
||||
// Handle equipped status changes for attachment effects
|
||||
if (changes.system?.equipped !== undefined && changes.system.equipped !== this.equipped) {
|
||||
await this._handleAttachmentEffectsOnEquipChange(changes.system.equipped);
|
||||
}
|
||||
|
||||
if (changes.system?.features) {
|
||||
const removed = this.features.filter(x => !changes.system.features.includes(x));
|
||||
const added = changes.system.features.filter(x => !this.features.includes(x));
|
||||
|
|
@ -116,4 +122,60 @@ export default class DHWeapon extends BaseDataItem {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle adding/removing attachment effects when weapon is equipped/unequipped
|
||||
* @param {boolean} newEquippedStatus - The new equipped status
|
||||
*/
|
||||
async _handleAttachmentEffectsOnEquipChange(newEquippedStatus) {
|
||||
const actor = this.parent.parent;
|
||||
if (!actor || !this.attached?.length) return;
|
||||
|
||||
if (newEquippedStatus) {
|
||||
// Weapon is being equipped - add attachment effects
|
||||
console.log(`Weapon ${this.parent.name} being equipped, adding attachment effects`);
|
||||
|
||||
const effectsToCreate = [];
|
||||
for (const attachedUuid of this.attached) {
|
||||
const attachedItem = await fromUuid(attachedUuid);
|
||||
if (attachedItem && attachedItem.effects.size > 0) {
|
||||
for (const effect of attachedItem.effects) {
|
||||
// Copy ALL effects when item is attached - attachment-only flag only matters for non-attached items
|
||||
const effectData = effect.toObject();
|
||||
effectData.origin = `${this.parent.uuid}:${attachedUuid}`;
|
||||
effectData.flags = {
|
||||
...effectData.flags,
|
||||
daggerheart: {
|
||||
...effectData.flags?.daggerheart,
|
||||
attachmentSource: {
|
||||
weaponUuid: this.parent.uuid,
|
||||
itemUuid: attachedUuid,
|
||||
originalEffectId: effect.id
|
||||
}
|
||||
}
|
||||
};
|
||||
effectsToCreate.push(effectData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (effectsToCreate.length > 0) {
|
||||
await actor.createEmbeddedDocuments('ActiveEffect', effectsToCreate);
|
||||
console.log(`Created ${effectsToCreate.length} attachment effects on actor`);
|
||||
}
|
||||
} else {
|
||||
// Weapon is being unequipped - remove attachment effects
|
||||
console.log(`Weapon ${this.parent.name} being unequipped, removing attachment effects`);
|
||||
|
||||
const effectsToRemove = actor.effects.filter(effect => {
|
||||
const attachmentSource = effect.flags?.daggerheart?.attachmentSource;
|
||||
return attachmentSource && attachmentSource.weaponUuid === this.parent.uuid;
|
||||
});
|
||||
|
||||
if (effectsToRemove.length > 0) {
|
||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
||||
console.log(`Removed ${effectsToRemove.length} attachment effects from actor`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue