Advertisement
Guest User

Another RPG Engine stat mod problem

a guest
Sep 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. window.Stat = class Stat {
  2. // Designed by Akjosch
  3.  
  4. constructor (base) {
  5. this._base = base;
  6. this._mods = {};
  7. this._current = undefined;
  8. }
  9.  
  10. clearCache () {
  11. this._current = undefined;
  12. }
  13.  
  14. addMod (id, mod, equipment) {
  15. console.log("addMod called");
  16. id = String(id);
  17. if (!this._mods[id]) {
  18. this._mods[id] = [];
  19. }
  20. if(Number.isFinite(mod)) {
  21. mod = { add: mod };
  22. if (equipment === true) {
  23. mod.equipment = true;
  24. }
  25. }
  26. if(typeof mod === "object") {
  27. this._mods[id].push(mod);
  28. this.clearCache();
  29. return this._mods[id].indexOf(mod);
  30. }
  31. }
  32.  
  33. removeMod (id, index) {
  34. console.log("removeMod called");
  35. if (Number.isFinite(index)) {
  36. console.log("index detected");
  37. this._mods[String(id)].deleteAt(index);
  38. } else {
  39. delete this._mods[String(id)];
  40. }
  41. this.clearCache();
  42. }
  43.  
  44. clearMods () {
  45. this._mods = {};
  46. this.clearCache();
  47. }
  48.  
  49. get base () {
  50. return this._base;
  51. }
  52.  
  53. set base (newBase) {
  54. newBase = Number(newBase);
  55. /* alternative: newBase = setup.roll(newBase) for "3d6" as inputs */
  56. if(!Number.isFinite(newBase)) {
  57. newBase = 0;
  58. }
  59. if (this._base !== newBase) {
  60. this._base = newBase;
  61. this.clearCache();
  62. }
  63. }
  64.  
  65. get current () {
  66. if (this._current === undefined) {
  67. /* gather multipliers */
  68. var mult = Object.values(this._mods)
  69. .reduce(function(bigSum, entry) { return bigSum + entry
  70. .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
  71. .reduce(function(sum, add) { return sum + add; }, 0);
  72. }, 0);
  73. /* gather sums */
  74. var add = Object.values(this._mods)
  75. .reduce(function(bigSum, entry) { return bigSum + entry
  76. .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
  77. .reduce(function(sum, add) { return sum + add; }, 0);
  78. }, 0);
  79. this._current = this.base * (mult + 1) + add;
  80. }
  81. return this._current;
  82. }
  83.  
  84. get bonus () {
  85. return this.current - this.base;
  86. }
  87.  
  88. get equipBonus () {
  89. var mult = Object.values(this._mods)
  90. .reduce(function(bigSum, entry) { return bigSum + entry
  91. .map(function(m) { return m.equipment === true ? m : {}; })
  92. .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
  93. .reduce(function(sum, add) { return sum + add; }, 0);
  94. }, 0);
  95. var add = Object.values(this._mods)
  96. .reduce(function(bigSum, entry) { return bigSum + entry
  97. .map(function(m) { return m.equipment === true ? m : {}; })
  98. .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
  99. .reduce(function(sum, add) { return sum + add; }, 0);
  100. }, 0);
  101. return this.base * (mult + 1) + add - this.base;
  102. }
  103.  
  104. toString () {
  105. var mult = Object.values(this._mods)
  106. .map(function(m) { return Number.isFinite(m.mult) ? m.mult : 0; })
  107. .reduce(function(sum, add) { return sum + add; }, 0);
  108. var add = Object.values(this._mods)
  109. .map(function(m) { return Number.isFinite(m.add) ? m.add : 0; })
  110. .reduce(function(sum, add) { return sum + add; }, 0);
  111. return this.current.toFixed(2)
  112. + " [" + this.base.toFixed(2)
  113. + " x " + (mult + 1).toFixed(2)
  114. + (add >= 0 ? " + " + add.toFixed(2) : " - " + (-add).toFixed(2))
  115. + "]";
  116. }
  117.  
  118. /* SugarCube support */
  119. clone () {
  120. return Stat.create(this);
  121. }
  122.  
  123. toJSON () {
  124. return JSON.reviveWrapper('Stat.create($ReviveData$)', Object.assign({}, clone(this)));
  125. }
  126. };
  127.  
  128. Stat.create = function(vals) {
  129. vals = vals || {};
  130. var result = new Stat();
  131. result._base = vals._base;
  132. result._mods = clone(vals._mods);
  133. result._current = vals._current;
  134. return result;
  135. };
  136.  
  137. Map.prototype.addMod = function (key, id, mod, equipment) {
  138. return this.get(key).addMod(id,mod,equipment);
  139. }
  140.  
  141. Map.prototype.removeMod = function (key, id, index) {
  142. this.get(key).removeMod(id, index);
  143. }
  144.  
  145. ----
  146.  
  147. class Effect {
  148. constructor(name,time,power,target){
  149. this.name = name;
  150. (...)
  151.  
  152. switch (name) {
  153. case 'ATK Boost':
  154. this.buff = true;
  155. this.stackable = true;
  156. this.statmod = true;
  157. this.onApply = (puppet) => {
  158. this.id = puppet.stats.addMod("Attack","ATK Boost",this.power);
  159. console.log("Applying ATK Boost. ID = "+this.id);
  160. }
  161. this.onRemove = (puppet) => {
  162. console.log("Removing ATK Boost. ID = "+this.id);
  163. puppet.stats.removeMod("Attack","ATK Boost",this.id);
  164. }
  165. this.info = function () {
  166. return `Attack boosted by ${this.power}.`;
  167. }
  168. this.addText = function (target) {
  169. return `${target} is surging with strength!`;
  170. }
  171. this.removeText = remBuff;
  172. break;
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement