Skip to content

Commit

Permalink
Track advantage modes in new number field class
Browse files Browse the repository at this point in the history
  • Loading branch information
krbz999 committed Dec 6, 2024
1 parent 56f8995 commit 400b3f8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion module/data/fields/_module.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./activities-field.mjs";
export {default as AdvancementField} from "./advancement-field.mjs";
export {default as AdvancementDataField} from "./advancement-data-field.mjs";
export {default as AdvancementField} from "./advancement-field.mjs";
export {default as AdvantageModeField} from "./advantage-mode-field.mjs";
export {default as FormulaField} from "./formula-field.mjs";
export {default as IdentifierField} from "./identifier-field.mjs";
export {default as LocalDocumentField} from "./local-document-field.mjs";
Expand Down
59 changes: 59 additions & 0 deletions module/data/fields/advantage-mode-field.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Subclass of NumberField that tracks the number of changes made to a roll mode.
*/
export default class AdvantageModeField extends foundry.data.fields.NumberField {
/** @inheritDoc */
static get _defaults() {
return foundry.utils.mergeObject(super._defaults, {
choices: [-1, 0, 1],
initial: 0,
label: "DND5E.AdvantageMode"
});
}

/* -------------------------------------------- */

/**
* Number of advantage modifications.
* @type {number}
*/
#advantage;

/* -------------------------------------------- */

/**
* Number of disadvantage modifications.
* @type {number}
*/
#disadvantage;

/* -------------------------------------------- */

/** @inheritDoc */
initialize(value, model, options={}) {
this.#advantage = Number(value === 1);
this.#disadvantage = Number(value === -1);
return value;
}

/* -------------------------------------------- */
/* Active Effect Integration */
/* -------------------------------------------- */

/** @override */
applyChange(value, model, change) {
const delta = this._castChangeDelta(change.value);
if (change.mode === CONST.ACTIVE_EFFECT_MODES.CUSTOM) {
return this._applyChangeCustom(value, delta, model, change);
}
switch (delta) {
case 1:
this.#advantage++;
break;
case -1:
this.#disadvantage++;
break;
}
return Math.sign(this.#advantage) - Math.sign(this.#disadvantage);
}
}
4 changes: 3 additions & 1 deletion module/data/shared/roll-config-field.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import AdvantageModeField from "../fields/advantage-mode-field.mjs";

const { StringField, NumberField, SchemaField } = foundry.data.fields;

/**
Expand All @@ -20,7 +22,7 @@ export default class RollConfigField extends foundry.data.fields.SchemaField {
roll: new SchemaField({
min: new NumberField({...opts, label: "DND5E.ROLL.Range.Minimum"}),
max: new NumberField({...opts, label: "DND5E.ROLL.Range.Maximum"}),
mode: new NumberField({choices: [-1, 0, 1], initial: 0, label: "DND5E.AdvantageMode"}),
mode: new AdvantageModeField(),
...roll
}),
...fields
Expand Down

0 comments on commit 400b3f8

Please sign in to comment.