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.
 
 
 

129 lines
2.3 KiB

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