Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Stat() {
  2.         this._base = 0;
  3.         this._mods = {};
  4.         this._current = undefined;
  5.  
  6.         if(typeof Stat.prototype.clearCache !== "function") {
  7.             Stat.prototype.clearCache = function() {
  8.                 this._current = undefined;
  9.             };
  10.             Stat.prototype.addMod = function(id, mod) {
  11.                 id = String(id);
  12.                 if(Number.isFinite(mod)) {
  13.                     mod = { add: mod };
  14.                 }
  15.                 if(typeof mod === "object") {
  16.                     this._mods[id] = mod;
  17.                     this.clearCache();
  18.                 }
  19.             };
  20.             Stat.prototype.removeMod = function(id) {
  21.                 delete this._mods[String(id)];
  22.                 this.clearCache();
  23.             };
  24.             Stat.prototype.clearMods = function() {
  25.                 this._mods = {};
  26.                 this.clearCache();
  27.             };
  28.             Object.defineProperty(Stat.prototype, "base", {
  29.                 get: function() {
  30.                     return this._base;
  31.                 },
  32.                 set: function(newBase) {
  33.                     newBase = Number(newBase);
  34.                     /* alternative: newBase = setup.roll(newBase) for "3d6" as inputs */
  35.                     if(!Number.isFinite(newBase)) {
  36.                         newBase = 0;
  37.                     }
  38.                     if(this._base !== newBase) {
  39.                         this._base = newBase;
  40.                         this.clearCache();
  41.                     }
  42.                 }
  43.             });
  44.             Object.defineProperty(Stat.prototype, "current", {
  45.                 get: function() {
  46.                     if(this._current === undefined) {
  47.                         /* gather multipliers */
  48.                         var mult = Object.values(this._mods)
  49.                             .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
  50.                             .reduce(function(sum, add) { return sum + add; }, 0);
  51.                         var add = Object.values(this._mods)
  52.                             .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
  53.                             .reduce(function(sum, add) { return sum + add; }, 0);
  54.                         this._current = this.base * (mult + 1) + add;
  55.                     }
  56.                     return this._current;
  57.                 }
  58.             });
  59.             Stat.prototype.toString = function() {
  60.                 var mult = Object.values(this._mods)
  61.                     .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
  62.                     .reduce(function(sum, add) { return sum + add; }, 0);
  63.                 var add = Object.values(this._mods)
  64.                     .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
  65.                     .reduce(function(sum, add) { return sum + add; }, 0);
  66.                 return this.current.toFixed(2)
  67.                     + " [" + this.base.toFixed(2)
  68.                     + " x " + (mult + 1).toFixed(2)
  69.                     + (add >= 0 ? " + " + add.toFixed(2) : " - " + (-add).toFixed(2))
  70.                     + "]";
  71.             };
  72.             /* SugarCube support */
  73.             Stat.prototype.clone = function() {
  74.                 return Stat.create(this);
  75.             };
  76.             Stat.prototype.toJSON = function() {
  77.                 return JSON.reviveWrapper('setup.Stat.create($ReviveData$)', Object.assign({}, clone(this)));
  78.             };
  79.         }
  80. };
  81. Stat.create = function(vals) {
  82.     vals = vals || {};
  83.     var result = new Stat();
  84.     result._base = vals._base;
  85.     result._mods = clone(vals._mods);
  86.     result._current = vals._current;
  87.     return result;
  88. };
  89.  
  90. setup.Stat = Stat;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement