Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.Stat = class Stat {
- constructor (base) {
- this._base = base;
- this._mods = {};
- this._current = undefined;
- }
- clearCache () {
- this._current = undefined;
- }
- addMod (id, mod) {
- id = String(id);
- if(Number.isFinite(mod)) {
- mod = { add: mod };
- }
- if(typeof mod === "object") {
- this._mods[id] = mod;
- this.clearCache();
- }
- }
- removeMod (id) {
- delete this._mods[String(id)];
- this.clearCache();
- }
- clearMods () {
- this._mods = {};
- this.clearCache();
- }
- get base () {
- return this._base;
- }
- set base (newBase) {
- newBase = Number(newBase);
- /* alternative: newBase = setup.roll(newBase) for "3d6" as inputs */
- if(!Number.isFinite(newBase)) {
- newBase = 0;
- }
- if (this._base !== newBase) {
- this._base = newBase;
- this.clearCache();
- }
- }
- get current () {
- if (this._current === undefined) {
- /* gather multipliers */
- var mult = Object.values(this._mods)
- .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
- .reduce(function(sum, add) { return sum + add; }, 0);
- var add = Object.values(this._mods)
- .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
- .reduce(function(sum, add) { return sum + add; }, 0);
- this._current = this.base * (mult + 1) + add;
- }
- return this._current;
- }
- toString () {
- var mult = Object.values(this._mods)
- .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
- .reduce(function(sum, add) { return sum + add; }, 0);
- var add = Object.values(this._mods)
- .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
- .reduce(function(sum, add) { return sum + add; }, 0);
- return this.current.toFixed(2)
- + " [" + this.base.toFixed(2)
- + " x " + (mult + 1).toFixed(2)
- + (add >= 0 ? " + " + add.toFixed(2) : " - " + (-add).toFixed(2))
- + "]";
- }
- /* SugarCube support */
- clone () {
- return Stat.create(this);
- }
- toJSON () {
- return JSON.reviveWrapper('setup.Stat.create($ReviveData$)', Object.assign({}, clone(this)));
- }
- };
- Stat.prototype.create = function(vals) {
- vals = vals || {};
- var result = new Stat();
- result._base = vals._base;
- result._mods = clone(vals._mods);
- result._current = vals._current;
- return result;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement