From 5113e375747c460b1c850d28abfe5dc43339f572 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Wed, 25 Feb 2026 00:15:56 +0100 Subject: [PATCH] Initial --- module/data/action/baseAction.mjs | 9 ++++++++ module/data/actor/character.mjs | 2 +- module/data/fields/_module.mjs | 1 + module/data/fields/action/damageField.mjs | 9 +++++++- module/data/fields/collectionField.mjs | 25 ++++++++++++++++++++++ templates/sheets/items/weapon/settings.hbs | 14 ++++++------ 6 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 module/data/fields/collectionField.mjs diff --git a/module/data/action/baseAction.mjs b/module/data/action/baseAction.mjs index f6ffe75f..8c172701 100644 --- a/module/data/action/baseAction.mjs +++ b/module/data/action/baseAction.mjs @@ -376,6 +376,15 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel return tags; } + + static migrateData(source) { + if (source.damage?.parts && Array.isArray(source.damage.parts)) { + source.damage.parts = source.damage.parts.reduce((acc, part) => { + acc[part.applyTo] = part; + return acc; + }, {}); + } + } } export class ResourceUpdateMap extends Map { diff --git a/module/data/actor/character.mjs b/module/data/actor/character.mjs index 10fba63c..1ae6016f 100644 --- a/module/data/actor/character.mjs +++ b/module/data/actor/character.mjs @@ -704,7 +704,7 @@ export default class DhCharacter extends DhCreature { isReversed: true }; - this.attack.damage.parts[0].value.custom.formula = `@prof${this.basicAttackDamageDice}${this.rules.attack.damage.bonus ? ` + ${this.rules.attack.damage.bonus}` : ''}`; + this.attack.damage.parts.hitPoints.value.custom.formula = `@prof${this.basicAttackDamageDice}${this.rules.attack.damage.bonus ? ` + ${this.rules.attack.damage.bonus}` : ''}`; } getRollData() { diff --git a/module/data/fields/_module.mjs b/module/data/fields/_module.mjs index 2a8ba454..816f419a 100644 --- a/module/data/fields/_module.mjs +++ b/module/data/fields/_module.mjs @@ -1,4 +1,5 @@ export { ActionCollection } from './actionField.mjs'; +export { default as CollectionField } from './collectionField.mjs'; export { default as FormulaField } from './formulaField.mjs'; export { default as ForeignDocumentUUIDField } from './foreignDocumentUUIDField.mjs'; export { default as ForeignDocumentUUIDArrayField } from './foreignDocumentUUIDArrayField.mjs'; diff --git a/module/data/fields/action/damageField.mjs b/module/data/fields/action/damageField.mjs index efad726c..4adfb9ec 100644 --- a/module/data/fields/action/damageField.mjs +++ b/module/data/fields/action/damageField.mjs @@ -1,5 +1,6 @@ import FormulaField from '../formulaField.mjs'; import { setsEqual } from '../../../helpers/utils.mjs'; +import CollectionField from '../collectionField.mjs'; const fields = foundry.data.fields; @@ -12,7 +13,7 @@ export default class DamageField extends fields.SchemaField { /** @inheritDoc */ constructor(options, context = {}) { const damageFields = { - parts: new fields.ArrayField(new fields.EmbeddedDataField(DHDamageData)), + parts: new CollectionField(DHDamageData, { collectionClass: DamagePartsCollection }), includeBase: new fields.BooleanField({ initial: false, label: 'DAGGERHEART.ACTIONS.Settings.includeBase.label' @@ -300,3 +301,9 @@ export class DHDamageData extends DHResourceData { }; } } + +class DamagePartsCollection extends foundry.utils.Collection { + get hitPoints() { + return this.get('hitPoints'); + } +} diff --git a/module/data/fields/collectionField.mjs b/module/data/fields/collectionField.mjs new file mode 100644 index 00000000..9291cab8 --- /dev/null +++ b/module/data/fields/collectionField.mjs @@ -0,0 +1,25 @@ +export default class CollectionField extends foundry.data.fields.TypedObjectField { + constructor(model, options = { collectionClass: foundry.utils.Collection }, context = {}) { + super(new foundry.data.fields.EmbeddedDataField(model), options, context); + this.#elementClass = model; + this.#collectionClass = options.collectionClass; + } + + /** + * The collection class + */ + #collectionClass; + + /** + * The collection element class. + */ + #elementClass; + + initialize(value, model, _options = {}) { + console.log(model); + const collection = new this.#collectionClass( + Object.entries(value).map(([key, value]) => [key, new this.#elementClass(value)]) + ); + return collection; + } +} diff --git a/templates/sheets/items/weapon/settings.hbs b/templates/sheets/items/weapon/settings.hbs index f9499221..e67e8dd7 100644 --- a/templates/sheets/items/weapon/settings.hbs +++ b/templates/sheets/items/weapon/settings.hbs @@ -19,24 +19,24 @@
{{#with systemFields.attack.fields.damage.fields.parts.element.fields as | fields | }} - {{#with (lookup ../document.system.attack.damage.parts 0) as | source | }} + {{#with ../document.system.attack.damage.parts.hitPoints as | source | }} {{localize "DAGGERHEART.GENERAL.damage"}} {{localize "DAGGERHEART.ACTIONS.Config.general.customFormula"}} - {{formInput fields.value.fields.custom.fields.enabled value=source.value.custom.enabled name="system.attack.damage.parts.0.value.custom.enabled"}} + {{formInput fields.value.fields.custom.fields.enabled value=source.value.custom.enabled name="system.attack.damage.parts.hitPoints.value.custom.enabled"}} {{#if source.value.custom.enabled}} {{localize "DAGGERHEART.ACTIONS.Config.general.formula"}} - {{formInput fields.value.fields.custom.fields.formula value=source.value.custom.formula name="system.attack.damage.parts.0.value.custom.formula"}} + {{formInput fields.value.fields.custom.fields.formula value=source.value.custom.formula name="system.attack.damage.parts.hitPoints.value.custom.formula"}} {{else}} {{localize "DAGGERHEART.GENERAL.Dice.single"}} - {{formInput fields.value.fields.dice value=source.value.dice name="system.attack.damage.parts.0.value.dice"}} + {{formInput fields.value.fields.dice value=source.value.dice name="system.attack.damage.parts.hitPoints.value.dice"}} {{localize "DAGGERHEART.GENERAL.bonus"}} - {{formInput fields.value.fields.bonus value=source.value.bonus name="system.attack.damage.parts.0.value.bonus" localize=true}} + {{formInput fields.value.fields.bonus value=source.value.bonus name="system.attack.damage.parts.hitPoints.value.bonus" localize=true}} {{/if}} {{localize "DAGGERHEART.GENERAL.type"}} - {{formInput fields.type value=source.type name="system.attack.damage.parts.0.type" localize=true}} + {{formInput fields.type value=source.type name="system.attack.damage.parts.hitPoints.type" localize=true}} {{localize "DAGGERHEART.CONFIG.DamageType.direct.name"}} {{formInput @root.systemFields.attack.fields.damage.fields.direct value=@root.document.system.attack.damage.direct name="system.attack.damage.direct" localize=true}} - + {{/with}} {{/with}}