You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.4 KiB

  1. /* eslint-disable max-lines-per-function */
  2. define([
  3. 'js/system/events',
  4. 'js/rendering/numbers'
  5. ], function (
  6. events,
  7. numbers
  8. ) {
  9. const defaultBuffIcons = {
  10. stunned: [4, 0]
  11. };
  12. const effectBase = {
  13. init: function () {
  14. this.defaultDamageText(false);
  15. if (this.self && defaultBuffIcons[this.type]) {
  16. events.emit('onGetEffectIcon', {
  17. id: this.id,
  18. icon: defaultBuffIcons[this.type]
  19. });
  20. }
  21. },
  22. destroy: function () {
  23. if (!this.obj.destroyed)
  24. this.defaultDamageText(true);
  25. if (this.self && defaultBuffIcons[this.type]) {
  26. events.emit('onRemoveEffectIcon', {
  27. id: this.id
  28. });
  29. }
  30. },
  31. defaultDamageText: function (removing) {
  32. numbers.onGetDamage({
  33. id: this.obj.id,
  34. event: true,
  35. text: (removing ? '-' : '+') + this.type
  36. });
  37. }
  38. };
  39. return {
  40. type: 'effects',
  41. effects: [],
  42. templates: {
  43. },
  44. init: function (blueprint) {
  45. this.effects = this.effects.map(e => this.buildEffect(e));
  46. },
  47. buildEffect: function (data) {
  48. let template = this.templates[data.type] || {};
  49. let effect = $.extend(true, {}, effectBase, template, data);
  50. effect.self = !!this.obj.self;
  51. effect.obj = this.obj;
  52. if (effect.init)
  53. effect.init();
  54. return effect;
  55. },
  56. extend: function (blueprint) {
  57. if (blueprint.addEffects) {
  58. blueprint.addEffects = blueprint.addEffects.map(e => this.buildEffect(e));
  59. this.effects.push.apply(this.effects, blueprint.addEffects || []);
  60. }
  61. if (blueprint.removeEffects) {
  62. blueprint.removeEffects.forEach(removeId => {
  63. let effect = this.effects.find(e => e.id === removeId);
  64. if (!effect)
  65. return;
  66. if (effect.destroy)
  67. effect.destroy();
  68. this.effects.spliceFirstWhere(e => e.id === removeId);
  69. });
  70. }
  71. if (blueprint.extendEffects) {
  72. blueprint.extendEffects.forEach(u => {
  73. let effect = this.effects.find(e => e.id === u.id);
  74. if (!effect)
  75. return;
  76. if (effect.extend)
  77. effect.extend(u.data);
  78. else {
  79. for (let p in u.data)
  80. effect[p] = u.data[p];
  81. }
  82. });
  83. }
  84. },
  85. update: function () {
  86. this.effects.forEach(e => {
  87. if (e.update)
  88. e.update();
  89. });
  90. },
  91. setVisible: function (visible) {
  92. this.effects.forEach(e => {
  93. if (e.setVisible)
  94. e.setVisible(visible);
  95. });
  96. },
  97. destroy: function () {
  98. this.effects.forEach(e => {
  99. if (e.destroy)
  100. e.destroy();
  101. });
  102. }
  103. };
  104. });