mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-12 03:31:07 +01:00
Iss4 - create a way to attach attach items to armor and weapons (#310)
* add basic drag drop window * add better field * make effects copy onto actor on attachment * make items from inventory draggable * working drop from inventory * remove duplication issue * add attachment only flag and logic * add weapons to attachables * remove debug logs * try to make it drier * remove unecessary try catch * remove extra configs * remove superfluous comments * remove spurious defenses * make drier * remove unecessary code * deduplicate and simplify * its a desert * standardize to be more similar to class item code * fix bug of duplicate effects being created * fix localization string * fix bug of item equiping and un equiping * remove this since were not going to be using attachmentonly * update attachment tab with comments * remove attachment only logic in favor of just transfer * change flags * change armor and weapon to be attachableItem * change armor and weapon to be attachableItem * change weapon to use mixin * add mixin to armor * move everything to mixin sheet * refactor code for review comments * cleanup and somehow git is ignoring some changes * see if this picks up the changes now * Import/Export updates --------- Co-authored-by: psitacus <walther.johnson@ucalgary.ca> Co-authored-by: WBHarry <williambjrklund@gmail.com>
This commit is contained in:
parent
812a5e8dd7
commit
687500f191
18 changed files with 357 additions and 18 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import DHAncestry from './ancestry.mjs';
|
||||
import DHArmor from './armor.mjs';
|
||||
import DHAttachableItem from './attachableItem.mjs';
|
||||
import DHClass from './class.mjs';
|
||||
import DHCommunity from './community.mjs';
|
||||
import DHConsumable from './consumable.mjs';
|
||||
|
|
@ -13,6 +14,7 @@ import DHBeastform from './beastform.mjs';
|
|||
export {
|
||||
DHAncestry,
|
||||
DHArmor,
|
||||
DHAttachableItem,
|
||||
DHClass,
|
||||
DHCommunity,
|
||||
DHConsumable,
|
||||
|
|
@ -27,6 +29,7 @@ export {
|
|||
export const config = {
|
||||
ancestry: DHAncestry,
|
||||
armor: DHArmor,
|
||||
attachableItem: DHAttachableItem,
|
||||
class: DHClass,
|
||||
community: DHCommunity,
|
||||
consumable: DHConsumable,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import BaseDataItem from './base.mjs';
|
||||
import AttachableItem from './attachableItem.mjs';
|
||||
import ActionField from '../fields/actionField.mjs';
|
||||
import { armorFeatures } from '../../config/itemConfig.mjs';
|
||||
import { actionsTypes } from '../action/_module.mjs';
|
||||
|
||||
export default class DHArmor extends BaseDataItem {
|
||||
export default class DHArmor extends AttachableItem {
|
||||
/** @inheritDoc */
|
||||
static get metadata() {
|
||||
return foundry.utils.mergeObject(super.metadata, {
|
||||
|
|
|
|||
152
module/data/item/attachableItem.mjs
Normal file
152
module/data/item/attachableItem.mjs
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
import BaseDataItem from './base.mjs';
|
||||
|
||||
export default class AttachableItem extends BaseDataItem {
|
||||
static defineSchema() {
|
||||
const fields = foundry.data.fields;
|
||||
return {
|
||||
...super.defineSchema(),
|
||||
attached: new fields.ArrayField(new fields.DocumentUUIDField({ type: "Item", nullable: true }))
|
||||
};
|
||||
}
|
||||
|
||||
async _preUpdate(changes, options, user) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
async #handleAttachmentEffectsOnEquipChange(newEquippedStatus) {
|
||||
const actor = this.parent.parent?.type === 'character' ? this.parent.parent : this.parent.parent?.parent;
|
||||
const parentType = this.parent.type;
|
||||
|
||||
if (!actor || !this.attached?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (newEquippedStatus) {
|
||||
// Item is being equipped - add attachment effects
|
||||
for (const attachedUuid of this.attached) {
|
||||
const attachedItem = await fromUuid(attachedUuid);
|
||||
if (attachedItem && attachedItem.effects.size > 0) {
|
||||
await this.#copyAttachmentEffectsToActor({
|
||||
attachedItem,
|
||||
attachedUuid,
|
||||
parentType
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Item is being unequipped - remove attachment effects
|
||||
await this.#removeAllAttachmentEffects(parentType);
|
||||
}
|
||||
}
|
||||
|
||||
async #copyAttachmentEffectsToActor({ attachedItem, attachedUuid, parentType }) {
|
||||
const actor = this.parent.parent;
|
||||
if (!actor || !attachedItem.effects.size > 0 || !this.equipped) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const effectsToCreate = [];
|
||||
for (const effect of attachedItem.effects) {
|
||||
const effectData = effect.toObject();
|
||||
effectData.origin = `${this.parent.uuid}:${attachedUuid}`;
|
||||
|
||||
const attachmentSource = {
|
||||
itemUuid: attachedUuid,
|
||||
originalEffectId: effect.id
|
||||
};
|
||||
attachmentSource[`${parentType}Uuid`] = this.parent.uuid;
|
||||
|
||||
effectData.flags = {
|
||||
...effectData.flags,
|
||||
[CONFIG.DH.id]: {
|
||||
...effectData.flags?.[CONFIG.DH.id],
|
||||
[CONFIG.DH.FLAGS.itemAttachmentSource]: attachmentSource
|
||||
}
|
||||
};
|
||||
effectsToCreate.push(effectData);
|
||||
}
|
||||
|
||||
if (effectsToCreate.length > 0) {
|
||||
return await actor.createEmbeddedDocuments('ActiveEffect', effectsToCreate);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
async #removeAllAttachmentEffects(parentType) {
|
||||
const actor = this.parent.parent;
|
||||
if (!actor) return;
|
||||
|
||||
const parentUuidProperty = `${parentType}Uuid`;
|
||||
const effectsToRemove = actor.effects.filter(effect => {
|
||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
||||
return attachmentSource && attachmentSource[parentUuidProperty] === this.parent.uuid;
|
||||
});
|
||||
|
||||
if (effectsToRemove.length > 0) {
|
||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Public method for adding an attachment
|
||||
*/
|
||||
async addAttachment(droppedItem) {
|
||||
const newUUID = droppedItem.uuid;
|
||||
|
||||
if (this.attached.includes(newUUID)) {
|
||||
ui.notifications.warn(`${droppedItem.name} is already attached to this ${this.parent.type}.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedAttached = [...this.attached, newUUID];
|
||||
await this.parent.update({
|
||||
'system.attached': updatedAttached
|
||||
});
|
||||
|
||||
// Copy effects if equipped
|
||||
if (this.equipped && droppedItem.effects.size > 0) {
|
||||
await this.#copyAttachmentEffectsToActor({
|
||||
attachedItem: droppedItem,
|
||||
attachedUuid: newUUID,
|
||||
parentType: this.parent.type
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Public method for removing an attachment
|
||||
*/
|
||||
async removeAttachment(attachedUuid) {
|
||||
await this.parent.update({
|
||||
'system.attached': this.attached.filter(uuid => uuid !== attachedUuid)
|
||||
});
|
||||
|
||||
// Remove effects
|
||||
await this.#removeAttachmentEffects(attachedUuid);
|
||||
}
|
||||
|
||||
async #removeAttachmentEffects(attachedUuid) {
|
||||
const actor = this.parent.parent;
|
||||
if (!actor) return;
|
||||
|
||||
const parentType = this.parent.type;
|
||||
const parentUuidProperty = `${parentType}Uuid`;
|
||||
const effectsToRemove = actor.effects.filter(effect => {
|
||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
||||
return attachmentSource &&
|
||||
attachmentSource[parentUuidProperty] === this.parent.uuid &&
|
||||
attachmentSource.itemUuid === attachedUuid;
|
||||
});
|
||||
|
||||
if (effectsToRemove.length > 0) {
|
||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
import BaseDataItem from './base.mjs';
|
||||
import AttachableItem from './attachableItem.mjs';
|
||||
import { actionsTypes } from '../action/_module.mjs';
|
||||
import ActionField from '../fields/actionField.mjs';
|
||||
|
||||
export default class DHWeapon extends BaseDataItem {
|
||||
export default class DHWeapon extends AttachableItem {
|
||||
/** @inheritDoc */
|
||||
static get metadata() {
|
||||
return foundry.utils.mergeObject(super.metadata, {
|
||||
|
|
@ -37,7 +37,7 @@ export default class DHWeapon extends BaseDataItem {
|
|||
actionIds: new fields.ArrayField(new fields.StringField({ required: true }))
|
||||
})
|
||||
),
|
||||
attack: new ActionField({
|
||||
attack: new ActionField({
|
||||
initial: {
|
||||
name: 'Attack',
|
||||
img: 'icons/skills/melee/blood-slash-foam-red.webp',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue