diff --git a/module/models/Actor/Player.mjs b/module/models/Actor/Player.mjs index 244c887..5c31503 100644 --- a/module/models/Actor/Player.mjs +++ b/module/models/Actor/Player.mjs @@ -1,4 +1,5 @@ import DOTDUNGEON from "../../config.mjs"; +import { DiceField } from "../fields/DiceField.mjs"; function diceChoiceField() { return new foundry.data.fields.StringField({ @@ -38,7 +39,7 @@ export class PlayerData extends foundry.abstract.TypeDataModel { integer: true, }), stats: new fields.SchemaField({ - build: diceChoiceField(), + build: new DiceField(), meta: diceChoiceField(), presence: diceChoiceField(), hands: diceChoiceField(), diff --git a/module/models/fields/DiceField.mjs b/module/models/fields/DiceField.mjs new file mode 100644 index 0000000..294e1a9 --- /dev/null +++ b/module/models/fields/DiceField.mjs @@ -0,0 +1,73 @@ +import { statDice } from "../../config.mjs"; + +/** + * A subclass of StringField that allows ActiveEffects to integrate with dice + * values and increase/decrease the value step-wise according to the dice ladder. + */ +export class DiceField extends foundry.data.fields.DataField { + static get _defaults() { + return foundry.utils.mergeObject(super._defaults, { + trim: true, + blank: true, + initial: ``, + choices: [``, ...statDice], + }); + }; + + constructor(options = {}, context = {}) { + super(options, context); + + // v- because for some reason Foundry doesn't respect the _defaults getter + this.blank = true; + console.log(this.choices) + }; + + applyChange(...args) { + console.log(`DiceField.applyChange`, args); + return super.applyChange(...args) + } + + _cast(value) { + return String(value); + } + + _castChangeDelta(delta) { + console.log(`DiceField._castChangeDelta(${delta})`) + return parseInt(delta) ?? 0; + }; + + /** @inheritdoc */ + _applyChangeAdd(value, delta, model, change) { + console.warn(`Cannot apply Add ActiveEffects to DiceFields. Not changing value.`); + return value; + }; + + _applyChangeMultiply(value, delta, model, change) { + console.warn(`Cannot apply Multiply ActiveEffects to DiceFields. Not changing value.`); + return value; + }; + + _applyChangeOverride(value, delta, model, change) { + return delta; + }; + + _applyChangeUpgrade(value, delta, model, change) { + console.log(`.dungeon | Pre: value=${value}; delta=${delta}`); + if (value === "") return value; + const dieIndex = statDice.findIndex(value); + const newIndex = Math.min(Math.max(0, dieIndex - delta), statDice.length - 1); + value = statDice[newIndex]; + console.log(`.dungeon | Post: value=${value}; delta=${delta}`); + return value; + }; + + _applyChangeDowngrade(value, delta, model, change) { + console.log(`.dungeon | Pre: value=${value}; delta=${delta}`); + if (value === "") return value; + const dieIndex = statDice.findIndex(value); + const newIndex = Math.min(Math.max(0, dieIndex + delta), statDice.length - 1); + value = statDice[newIndex]; + console.log(`.dungeon | Post: value=${value}; delta=${delta}`); + return value + }; +}; diff --git a/system.json b/system.json index 5d3387c..01f77c4 100644 --- a/system.json +++ b/system.json @@ -9,7 +9,7 @@ "compatibility": { "minimum": 11, "verified": 11, - "maximum": 11 + "maximum": 12 }, "authors": [ {