mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-03-07 14:36:13 +01:00
Added a updateArmorValue function that updates armoreffects according to an auto order
This commit is contained in:
parent
fb9f89fa9d
commit
9dc5830e18
2 changed files with 75 additions and 5 deletions
|
|
@ -458,11 +458,6 @@ export default class DhCharacter extends BaseDataActor {
|
|||
return this.parent.items.find(x => x.type === 'armor' && x.system.equipped);
|
||||
}
|
||||
|
||||
/* TODO: Prep datastructure to be useful when applying automatic armor damage order */
|
||||
get armorEffects() {
|
||||
return Array.from(this.parent.allApplicableEffects());
|
||||
}
|
||||
|
||||
get activeBeastform() {
|
||||
return this.parent.effects.find(x => x.type === 'beastform');
|
||||
}
|
||||
|
|
@ -512,6 +507,53 @@ export default class DhCharacter extends BaseDataActor {
|
|||
}
|
||||
}
|
||||
|
||||
async updateArmorValue(armorChange) {
|
||||
if (armorChange === 0) return;
|
||||
|
||||
const increasing = armorChange >= 0;
|
||||
let remainingChange = Math.abs(armorChange);
|
||||
const armorEffects = Array.from(this.parent.allApplicableEffects()).filter(x => x.type === 'armor');
|
||||
const orderedEffects = game.system.api.data.activeEffects.ArmorEffect.orderEffectsForAutoChange(
|
||||
armorEffects,
|
||||
increasing
|
||||
);
|
||||
|
||||
const embeddedUpdates = [];
|
||||
for (const armorEffect of orderedEffects) {
|
||||
let usedArmorChange = 0;
|
||||
if (increasing) {
|
||||
const remainingArmor = armorEffect.system.armorChange.max - armorEffect.system.armorChange.value;
|
||||
usedArmorChange = Math.min(remainingChange, remainingArmor);
|
||||
remainingChange -= usedArmorChange;
|
||||
} else {
|
||||
const changeChange = Math.min(armorEffect.system.armorChange.value, remainingChange);
|
||||
usedArmorChange -= changeChange;
|
||||
remainingChange -= changeChange;
|
||||
}
|
||||
|
||||
if (!usedArmorChange) continue;
|
||||
else {
|
||||
if (!embeddedUpdates[armorEffect.parent.id])
|
||||
embeddedUpdates[armorEffect.parent.id] = { doc: armorEffect.parent, updates: [] };
|
||||
|
||||
embeddedUpdates[armorEffect.parent.id].updates.push({
|
||||
'_id': armorEffect.id,
|
||||
'system.changes': [
|
||||
{
|
||||
...armorEffect.system.armorChange,
|
||||
value: armorEffect.system.armorChange.value + usedArmorChange
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
if (remainingChange === 0) break;
|
||||
}
|
||||
|
||||
for (const { doc, updates } of Object.values(embeddedUpdates))
|
||||
doc.updateEmbeddedDocuments('ActiveEffect', updates);
|
||||
}
|
||||
|
||||
get sheetLists() {
|
||||
const ancestryFeatures = [],
|
||||
communityFeatures = [],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue