Single armor rework start

This commit is contained in:
WBHarry 2026-03-19 13:26:11 +01:00
parent 6e75b81806
commit caea55ade4
7 changed files with 53 additions and 32 deletions

View file

@ -281,7 +281,6 @@ Hooks.on('setup', () => {
...damageThresholds,
'proficiency',
'evasion',
'armorScore',
'scars',
'levelData.level.current'
]

View file

@ -153,6 +153,10 @@ export default class DhActiveEffectConfig extends foundry.applications.sheets.Ac
minLength: 0
});
});
htmlElement
.querySelector('.armor-change-checkbox')
?.addEventListener('change', this.armorChangeToggle.bind(this));
}
async _prepareContext(options) {
@ -190,10 +194,17 @@ export default class DhActiveEffectConfig extends foundry.applications.sheets.Ac
break;
case 'changes':
const fields = this.document.system.schema.fields.changes.element.fields;
const singleTypes = ['armor'];
const { base, ...typedChanges } = context.source.changes.reduce((acc, change, index) => {
const type = CONFIG.DH.GENERAL.baseActiveEffectModes[change.type] ? 'base' : change.type;
if (singleTypes.includes(type)) {
acc[type] = { ...change, index };
} else {
if (!acc[type]) acc[type] = [];
acc[type].push({ ...change, index });
}
return acc;
}, {});
partContext.changes = await Promise.all(
@ -206,14 +217,30 @@ export default class DhActiveEffectConfig extends foundry.applications.sheets.Ac
return partContext;
}
armorChangeToggle(event) {
if (event.target.checked) {
this.addArmorChange();
} else {
this.removeTypedChange(event.target.dataset.index);
}
}
/* Could be generalised if needed later */
static #addTypedChange() {
addArmorChange() {
const submitData = this._processFormData(null, this.form, new FormDataExtended(this.form));
const changes = Object.values(submitData.system?.changes ?? {});
changes.push(game.system.api.data.activeEffects.changeTypes.armor.getInitialValue());
return this.submit({ updateData: { system: { changes } } });
}
removeTypedChange(indexString) {
const submitData = this._processFormData(null, this.form, new FormDataExtended(this.form));
const changes = Object.values(submitData.system.changes);
const index = Number(indexString);
changes.splice(index, 1);
return this.submit({ updateData: { system: { changes } } });
}
_prepareChangeContext(change, fields) {
if (typeof change.value !== 'string') change.value = JSON.stringify(change.value);
const defaultPriority = game.system.api.documents.DhActiveEffect.CHANGE_TYPES[change.type]?.defaultPriority;

View file

@ -89,6 +89,13 @@ export default class BaseEffect extends foundry.data.ActiveEffectTypeDataModel {
return true;
}
get armorData() {
const armorChange = this.changes.find(x => x.type === CONFIG.DH.GENERAL.activeEffectModes.armor.id);
if (!armorChange) return null;
return armorChange.armorData;
}
static getDefaultObject() {
return {
name: 'New Effect',

View file

@ -76,36 +76,27 @@ export default class Armor extends foundry.abstract.DataModel {
/* Helpers */
get armorChange() {
if (this.changes.length !== 1)
throw new Error('Unexpected error. An armor effect should have a changes field of length 1.');
const actor = this.parent.actor?.type === 'character' ? this.parent.actor : null;
const changeData = this.changes[0];
const maxParse = actor ? itemAbleRollParse(changeData.max, actor, this.parent.parent) : null;
get armorData() {
const actor = this.parent.parent?.actor?.type === 'character' ? this.parent.parent.actor : null;
const maxParse = actor ? itemAbleRollParse(this.max, actor, this.parent.parent.parent) : null;
const maxRoll = maxParse ? new Roll(maxParse).evaluateSync() : null;
const maxEvaluated = maxRoll ? (maxRoll.isDeterministic ? maxRoll.total : null) : null;
return {
...changeData,
max: maxEvaluated ?? changeData.max
value: this.value,
max: maxEvaluated ?? this.max
};
}
get armorData() {
return { value: this.armorChange.value, max: this.armorChange.max };
}
async updateArmorMax(newMax) {
const { effect, ...baseChange } = this.armorChange;
const newChanges = [
{
...baseChange,
max: newMax,
value: Math.min(this.armorChange.value, newMax)
}
...this.parent.changes.map(change => ({
...change,
value: change.type === 'armor' ? Math.min(this.parent.value, newMax) : change.value,
typeData: change.type === 'armor' ? { ...change.typeData, max: newMax } : change.typeData
}))
];
await this.parent.update({ 'system.changes': newChanges });
await this.parent.parent.update({ 'system.changes': newChanges });
}
static orderEffectsForAutoChange(armorEffects, increasing) {

View file

@ -35,7 +35,7 @@
.armor-change-container {
header,
ol {
grid-template-columns: 6rem 6rem 12rem 4rem 1rem;
grid-template-columns: 6rem 6rem 12rem 4rem;
}
}
}

View file

@ -15,8 +15,8 @@
</ol>
<fieldset class="armor-change-container">
<legend>{{localize "DAGGERHEART.GENERAL.armor"}} <a data-action="addTypedChange"><i class="fa-solid fa-plus"></i></a></legend>
<legend>{{localize "DAGGERHEART.GENERAL.armor"}} <input type="checkbox" class="armor-change-checkbox" data-index="{{typedChanges.armor.index}}" {{checked typedChanges.armor}} /></legend>
{{#if typedChanges.armor}}
<header>
<div>{{localize "EFFECT.FIELDS.changes.element.value.label"}}</div>
<div>{{localize "DAGGERHEART.GENERAL.max"}}</div>
@ -24,10 +24,8 @@
<div>{{localize "EFFECT.FIELDS.changes.element.priority.label"}}</div>
</header>
<ol class="scrollable">
{{#each typedChanges.armor as |armor|}}
{{> "systems/daggerheart/templates/sheets/activeEffect/typeChanges/armorChange.hbs" armor fields=@root.systemFields.changes.element.fields}}
{{/each}}
{{> "systems/daggerheart/templates/sheets/activeEffect/typeChanges/armorChange.hbs" typedChanges.armor fields=@root.systemFields.changes.element.fields}}
</ol>
{{/if}}
</fieldset>
</section>

View file

@ -7,5 +7,4 @@
{{formInput fields.typeData.types.armor.fields.max name=(concat "system.changes." index ".typeData.max") value=typeData.max data-dtype="Number"}}
{{formInput fields.typeData.types.armor.fields.armorInteraction name=(concat "system.changes." index ".typeData.armorInteraction") value=typeData.armorInteraction localize=true}}
{{formInput fields.priority name=(concat "system.changes." index ".priority") value=priority}}
<a data-action="deleteChange"><i class="fa-solid fa-trash"></i></a>
</li>