Fixed Weapon/Armor features. Fixed Feature actions

This commit is contained in:
WBHarry 2025-07-23 00:37:25 +02:00
parent 5243260b4d
commit 920ab3fd76
10 changed files with 84 additions and 63 deletions

View file

@ -1,3 +1,4 @@
export { ActionCollection } from './actionField.mjs';
export { default as FormulaField } from './formulaField.mjs';
export { default as ForeignDocumentUUIDField } from './foreignDocumentUUIDField.mjs';
export { default as ForeignDocumentUUIDArrayField } from './foreignDocumentUUIDArrayField.mjs';

View file

@ -1,7 +1,6 @@
import AttachableItem from './attachableItem.mjs';
import { ActionField } from '../fields/actionField.mjs';
import { ActionsField } from '../fields/actionField.mjs';
import { armorFeatures } from '../../config/itemConfig.mjs';
import { actionsTypes } from '../action/_module.mjs';
export default class DHArmor extends AttachableItem {
/** @inheritDoc */
@ -40,7 +39,7 @@ export default class DHArmor extends AttachableItem {
major: new fields.NumberField({ integer: true, initial: 0 }),
severe: new fields.NumberField({ integer: true, initial: 0 })
}),
actions: new fields.ArrayField(new ActionField())
actions: new ActionsField()
};
}
@ -65,7 +64,10 @@ export default class DHArmor extends AttachableItem {
actionIds.push(...feature.actionIds);
}
await this.parent.deleteEmbeddedDocuments('ActiveEffect', effectIds);
changes.system.actions = this.actions.filter(x => !actionIds.includes(x._id));
changes.system.actions = actionIds.reduce((acc, id) => {
acc[`-=${id}`] = null;
return acc;
}, {});
for (var feature of added) {
const featureData = armorFeatures[feature.value];
@ -79,17 +81,38 @@ export default class DHArmor extends AttachableItem {
]);
feature.effectIds = embeddedItems.map(x => x.id);
}
const newActions = {};
if (featureData.actions?.length > 0) {
const newActions = featureData.actions.map(action => {
const cls = actionsTypes[action.type];
return new cls(
{ ...action, _id: foundry.utils.randomID(), name: game.i18n.localize(action.name) },
for (let action of featureData.actions) {
const embeddedEffects = await this.parent.createEmbeddedDocuments(
'ActiveEffect',
(action.effects ?? []).map(effect => ({
...effect,
transfer: false,
name: game.i18n.localize(effect.name),
description: game.i18n.localize(effect.description)
}))
);
const cls = game.system.api.models.actions.actionsTypes[action.type];
const actionId = foundry.utils.randomID();
newActions[actionId] = new cls(
{
...cls.getSourceConfig(this),
...action,
_id: actionId,
name: game.i18n.localize(action.name),
description: game.i18n.localize(action.description),
effects: embeddedEffects.map(x => ({ _id: x.id }))
},
{ parent: this }
);
});
changes.system.actions = [...this.actions, ...newActions];
feature.actionIds = newActions.map(x => x._id);
}
}
changes.system.actions = newActions;
feature.actionIds = Object.keys(newActions);
}
}
}

View file

@ -1,5 +1,5 @@
import BaseDataItem from './base.mjs';
import { ActionField } from '../fields/actionField.mjs';
import { ActionField, ActionsField } from '../fields/actionField.mjs';
export default class DHFeature extends BaseDataItem {
/** @inheritDoc */
@ -25,7 +25,7 @@ export default class DHFeature extends BaseDataItem {
subType: new fields.StringField({ choices: CONFIG.DH.ITEM.featureSubTypes, nullable: true, initial: null }),
originId: new fields.StringField({ nullable: true, initial: null }),
identifier: new fields.StringField(),
actions: new fields.ArrayField(new ActionField())
actions: new ActionsField()
};
}

View file

@ -1,5 +1,4 @@
import AttachableItem from './attachableItem.mjs';
import { actionsTypes } from '../action/_module.mjs';
import { ActionsField, ActionField } from '../fields/actionField.mjs';
export default class DHWeapon extends AttachableItem {
@ -66,7 +65,6 @@ export default class DHWeapon extends AttachableItem {
}
}),
actions: new ActionsField()
// actions: new fields.ArrayField(new ActionField())
};
}
@ -96,7 +94,10 @@ export default class DHWeapon extends AttachableItem {
}
await this.parent.deleteEmbeddedDocuments('ActiveEffect', removedEffectsUpdate);
changes.system.actions = this.actions.filter(x => !removedActionsUpdate.includes(x._id));
changes.system.actions = removedActionsUpdate.reduce((acc, id) => {
acc[`-=${id}`] = null;
return acc;
}, {});
for (let weaponFeature of added) {
const featureData = CONFIG.DH.ITEM.weaponFeatures[weaponFeature.value];
@ -111,7 +112,7 @@ export default class DHWeapon extends AttachableItem {
weaponFeature.effectIds = embeddedItems.map(x => x.id);
}
const newActions = [];
const newActions = {};
if (featureData.actions?.length > 0) {
for (let action of featureData.actions) {
const embeddedEffects = await this.parent.createEmbeddedDocuments(
@ -123,24 +124,25 @@ export default class DHWeapon extends AttachableItem {
description: game.i18n.localize(effect.description)
}))
);
const cls = actionsTypes[action.type];
newActions.push(
new cls(
{
...action,
_id: foundry.utils.randomID(),
name: game.i18n.localize(action.name),
description: game.i18n.localize(action.description),
effects: embeddedEffects.map(x => ({ _id: x.id }))
},
{ parent: this }
)
const cls = game.system.api.models.actions.actionsTypes[action.type];
const actionId = foundry.utils.randomID();
newActions[actionId] = new cls(
{
...cls.getSourceConfig(this),
...action,
_id: actionId,
name: game.i18n.localize(action.name),
description: game.i18n.localize(action.description),
effects: embeddedEffects.map(x => ({ _id: x.id }))
},
{ parent: this }
);
}
}
changes.system.actions = [...this.actions, ...newActions];
weaponFeature.actionIds = newActions.map(x => x._id);
changes.system.actions = newActions;
weaponFeature.actionIds = Object.keys(newActions);
}
}
}