Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Stat() {
- this._base = 0;
- this._mods = {};
- this._current = undefined;
- if(typeof Stat.prototype.clearCache !== "function") {
- Stat.prototype.clearCache = function() {
- this._current = undefined;
- };
- Stat.prototype.addMod = function(id, mod) {
- id = String(id);
- if(Number.isFinite(mod)) {
- mod = { add: mod };
- }
- if(typeof mod === "object") {
- this._mods[id] = mod;
- this.clearCache();
- }
- };
- Stat.prototype.removeMod = function(id) {
- delete this._mods[String(id)];
- this.clearCache();
- };
- Stat.prototype.clearMods = function() {
- this._mods = {};
- this.clearCache();
- };
- Object.defineProperty(Stat.prototype, "base", {
- get: function() {
- return this._base;
- },
- set: function(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();
- }
- }
- });
- Object.defineProperty(Stat.prototype, "current", {
- get: function() {
- 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;
- }
- });
- Stat.prototype.toString = function() {
- 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 */
- Stat.prototype.clone = function() {
- return Stat.create(this);
- };
- Stat.prototype.toJSON = function() {
- return JSON.reviveWrapper('setup.Stat.create($ReviveData$)', Object.assign({}, clone(this)));
- };
- }
- };
- Stat.create = function(vals) {
- vals = vals || {};
- var result = new Stat();
- result._base = vals._base;
- result._mods = clone(vals._mods);
- result._current = vals._current;
- return result;
- };
- setup.Stat = Stat;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement