This commit is contained in:
WBHarry 2026-02-09 19:38:29 +01:00
parent 514e0260eb
commit fb9f89fa9d
8 changed files with 127 additions and 57 deletions

View file

@ -1,3 +1,7 @@
/**
* ArmorEffects are ActiveEffects that have a static changes field of length 1. It includes current and maximum armor.
* When applied to a character, it adds to their currently marked and maximum armor.
*/
export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel {
static defineSchema() {
const fields = foundry.data.fields;
@ -14,7 +18,7 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
}),
phase: new fields.StringField({ required: true, blank: false, initial: 'initial' }),
priority: new fields.NumberField({ integer: true, initial: 20 }),
marked: new fields.NumberField({
value: new fields.NumberField({
required: true,
integer: true,
initial: 0,
@ -28,28 +32,35 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
min: 1,
label: 'DAGGERHEART.GENERAL.max'
})
})
}),
{
initial: [
{
type: CONFIG.DH.GENERAL.activeEffectModes.armor.id,
phase: 'initial',
priority: 20,
value: 0,
max: 1
}
]
}
)
};
}
get armorData() {
if (this.changes.length !== 1) return { value: 0, max: 0 };
return { value: this.changes[0].value, max: this.changes[0].max };
}
/* Type Functions */
async updateArmorMax(newMax) {
if (this.changes.length !== 1) return;
const newChanges = this.changes.map(change => ({
...change,
max: newMax,
marked: Math.min(change.marked, newMax)
}));
await this.parent.update({ 'system.changes': newChanges });
}
/**
* Validate that an {@link EffectChangeData#type} string is well-formed.
* @param {string} type The string to be validated
* @returns {true}
* @throws {Error} An error if the type string is malformed
*/
static #validateType(type) {
if (type !== CONFIG.DH.GENERAL.activeEffectModes.armor.id)
throw new Error('An armor effect must have change.type "armor"');
static applyChangeField(model, change, field) {
return [model, change, field];
return true;
}
static armorChangeEffect = {
@ -81,23 +92,71 @@ export default class ArmorEffect extends foundry.data.ActiveEffectTypeDataModel
render: null
};
/* Helpers */
get armorChange() {
if (this.changes.length !== 1)
throw new Error('Unexpected error. An armor effect should have a changes field of length 1.');
return this.changes[0];
}
get armorData() {
return { value: this.armorChange.value, max: this.armorChange.max };
}
async updateArmorMax(newMax) {
const newChanges = [
{
...this.armorChange,
max: newMax,
value: Math.min(this.armorChange.value, newMax)
}
];
await this.parent.update({ 'system.changes': newChanges });
}
/* Overrides */
prepareBaseData() {
for (const change of this.changes) {
change.key = 'system.armorScore';
change.value = Math.min(change.max - change.marked, change.max);
const armorChange = this.armorChange;
armorChange.key = 'system.armorScore';
}
static getDefaultEffectData() {
return {
type: 'armor',
name: game.i18n.localize('DAGGERHEART.EFFECTS.Armor.newArmorEffect'),
img: 'icons/equipment/chest/breastplate-helmet-metal.webp'
};
}
async _preCreate(data, options, user) {
const allowed = await super._preCreate(data, options, user);
if (allowed === false) return;
await this.updateSource({ ...ArmorEffect.getDefaultEffectData(), data });
}
async _preUpdate(changes, options, user) {
const allowed = await super._preUpdate(changes, options, user);
if (allowed === false) return false;
if (changes.system?.changes) {
const changesChanged = changes.system.changes.length !== this.changes.length;
if (changesChanged) {
ui.notifications.error(
game.i18n.localize('DAGGERHEART.UI.Notifications.cannotAlterArmorEffectChanges')
);
return false;
}
if (
changes.system.changes.length === 1 &&
changes.system.changes[0].type !== CONFIG.DH.GENERAL.activeEffectModes.armor.id
) {
ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.cannotAlterArmorEffectType'));
return false;
}
}
}
/**
* Validate that an {@link EffectChangeData#type} string is well-formed.
* @param {string} type The string to be validated
* @returns {true}
* @throws {Error} An error if the type string is malformed
*/
static #validateType(type) {
if (type !== CONFIG.DH.GENERAL.activeEffectModes.armor.id)
throw new Error('An armor effect must have change.type "armor"');
return true;
}
}