Last active
November 16, 2019 12:48
-
-
Save Akjosch/904142e460c094874ebccfe4ef3f48f3 to your computer and use it in GitHub Desktop.
Mod-carrying Stat value for SugarCube
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/* Compatibility version for older browsers, created with Babel */ | |
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | |
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | |
var Stat = | |
/*#__PURE__*/ | |
function () { | |
/** | |
* @param {Stat|object=} other - The Stat value to clone/copy. | |
*/ | |
function Stat(other) { | |
_classCallCheck(this, Stat); | |
if (other instanceof Stat) { | |
/** @type {number} */ | |
this._base = other._base; | |
/** @type {Object.<string, {add: number, mult: (number|undefined)}>} */ | |
this._mods = clone(other._mods); | |
/** @type {number} */ | |
this._current = other._current; | |
/** @type {{add: number, mult: number}} */ | |
this._totalMods = clone(other._totalMods); | |
} else if (_typeof(other) === "object") { | |
/** @type {number} */ | |
this._base = Number(other._base); | |
if (!Number.isFinite(this._base)) { | |
this._base = 0; | |
} | |
/** @type {Object.<string, {add: number, mult: (number|undefined)}>} */ | |
this._mods = _typeof(other._mods) === "object" ? clone(other._mods) : {}; | |
/** @type {number} */ | |
this._current = undefined; | |
/** @type {{add: number, mult: number}} */ | |
this._totalMods = undefined; | |
} else { | |
/** @type {number} */ | |
this._base = 0; | |
/** @type {Object.<string, {add: number, mult: (number|undefined)}>} */ | |
this._mods = {}; | |
/** @type {number} */ | |
this._current = undefined; | |
/** @type {{add: number, mult: number}} */ | |
this._totalMods = undefined; | |
} | |
} | |
_createClass(Stat, [{ | |
key: "clearCache", | |
value: function clearCache() { | |
this._current = undefined; | |
} | |
/** | |
* @param {string} id | |
* @param {number|{add: number, mult: (number|undefined)}} mod | |
*/ | |
}, { | |
key: "addMod", | |
value: function addMod(id, mod) { | |
id = String(id); | |
if (Number.isFinite(mod)) { | |
// @ts-ignore | |
mod = { | |
add: mod | |
}; | |
} | |
if (_typeof(mod) === "object") { | |
this._mods[id] = mod; | |
this.clearCache(); | |
} | |
} | |
/** | |
* @param {String} id | |
*/ | |
}, { | |
key: "removeMod", | |
value: function removeMod(id) { | |
delete this._mods[String(id)]; | |
this.clearCache(); | |
} | |
}, { | |
key: "clearMods", | |
value: function clearMods() { | |
this._mods = {}; | |
this.clearCache(); | |
} | |
/** | |
* @returns {number} | |
*/ | |
}, { | |
key: "_calcTotalMods", | |
value: function _calcTotalMods() { | |
/* 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._totalMods = { | |
add: add, | |
mult: mult | |
}; | |
} | |
/** | |
* @returns {number} | |
*/ | |
}, { | |
key: "toString", | |
/** | |
* @returns {string} | |
*/ | |
value: function toString() { | |
this._calcTotalMods(); | |
return this.current.toFixed(2) + " [" + this.base.toFixed(2) + " x " + (this._totalMods.mult + 1).toFixed(2) + (this._totalMods.add >= 0 ? " + " + this._totalMods.add.toFixed(2) : " - " + (-this._totalMods.add).toFixed(2)) + "]"; | |
} | |
/** | |
* @returns {Stat} | |
*/ | |
}, { | |
key: "clone", | |
value: function clone() { | |
return new Stat(this); | |
} | |
/** | |
* @returns {string|[string, any]} | |
*/ | |
}, { | |
key: "toJSON", | |
value: function toJSON() { | |
return JSON.reviveWrapper('new setup.Stat($ReviveData$)', Object.assign({}, clone(this))); | |
} | |
}, { | |
key: "base", | |
get: function get() { | |
return this._base; | |
}, | |
set: function set(newBase) { | |
newBase = Number(newBase); | |
if (!Number.isFinite(newBase)) { | |
newBase = 0; | |
} | |
if (this._base !== newBase) { | |
this._base = newBase; | |
this.clearCache(); | |
} | |
} | |
}, { | |
key: "current", | |
get: function get() { | |
if (this._current === undefined) { | |
this._calcTotalMods(); | |
this._current = this.base * (this._totalMods.mult + 1) + this._totalMods.add; | |
} | |
return this._current; | |
} | |
}]); | |
return Stat; | |
}(); | |
setup.Stat = Stat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
class Stat { | |
/** | |
* @param {Stat|object=} other - The Stat value to clone/copy. | |
*/ | |
constructor(other) { | |
if(other instanceof Stat) { | |
/** @type {number} */ | |
this._base = other._base; | |
/** @type {Object.<string, {add: number, mult: (number|undefined)}>} */ | |
this._mods = clone(other._mods); | |
/** @type {number} */ | |
this._current = other._current; | |
/** @type {{add: number, mult: number}} */ | |
this._totalMods = clone(other._totalMods); | |
} else if (typeof other === "object") { | |
/** @type {number} */ | |
this._base = Number(other._base); | |
if(!Number.isFinite(this._base)) { this._base = 0; } | |
/** @type {Object.<string, {add: number, mult: (number|undefined)}>} */ | |
this._mods = (typeof other._mods === "object" ? clone(other._mods) : {}); | |
/** @type {number} */ | |
this._current = undefined; | |
/** @type {{add: number, mult: number}} */ | |
this._totalMods = undefined; | |
} else { | |
/** @type {number} */ | |
this._base = 0; | |
/** @type {Object.<string, {add: number, mult: (number|undefined)}>} */ | |
this._mods = {}; | |
/** @type {number} */ | |
this._current = undefined; | |
/** @type {{add: number, mult: number}} */ | |
this._totalMods = undefined; | |
} | |
} | |
clearCache() { | |
this._current = undefined; | |
} | |
/** | |
* @param {string} id | |
* @param {number|{add: number, mult: (number|undefined)}} mod | |
*/ | |
addMod(id, mod) { | |
id = String(id); | |
if(Number.isFinite(mod)) { | |
// @ts-ignore | |
mod = { add: mod }; | |
} | |
if(typeof mod === "object") { | |
this._mods[id] = mod; | |
this.clearCache(); | |
} | |
} | |
/** | |
* @param {String} id | |
*/ | |
removeMod(id) { | |
delete this._mods[String(id)]; | |
this.clearCache(); | |
} | |
clearMods() { | |
this._mods = {}; | |
this.clearCache(); | |
} | |
/** | |
* @returns {number} | |
*/ | |
get base() { | |
return this._base; | |
} | |
set base(newBase) { | |
newBase = Number(newBase); | |
if(!Number.isFinite(newBase)) { | |
newBase = 0; | |
} | |
if(this._base !== newBase) { | |
this._base = newBase; | |
this.clearCache(); | |
} | |
} | |
_calcTotalMods() { | |
/* 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._totalMods = {add: add, mult: mult}; | |
} | |
/** | |
* @returns {number} | |
*/ | |
get current() { | |
if(this._current === undefined) { | |
this._calcTotalMods(); | |
this._current = this.base * (this._totalMods.mult + 1) + this._totalMods.add; | |
} | |
return this._current; | |
} | |
/** | |
* @returns {string} | |
*/ | |
toString() { | |
this._calcTotalMods(); | |
return this.current.toFixed(2) | |
+ " [" + this.base.toFixed(2) | |
+ " x " + (this._totalMods.mult + 1).toFixed(2) | |
+ (this._totalMods.add >= 0 ? " + " + this._totalMods.add.toFixed(2) : " - " + (-this._totalMods.add).toFixed(2)) | |
+ "]"; | |
} | |
/** | |
* @returns {Stat} | |
*/ | |
clone() { | |
return new Stat(this); | |
} | |
/** | |
* @returns {string|[string, any]} | |
*/ | |
toJSON() { | |
return JSON.reviveWrapper('new setup.Stat($ReviveData$)', Object.assign({}, clone(this))); | |
} | |
} | |
setup.Stat = Stat; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment