No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

184 líneas
3.4 KiB

  1. define([
  2. 'js/components/components',
  3. 'js/rendering/renderer',
  4. 'js/system/events',
  5. 'js/config'
  6. ], function (
  7. components,
  8. renderer,
  9. events,
  10. config
  11. ) {
  12. return {
  13. components: [],
  14. offsetX: 0,
  15. offsetY: 0,
  16. eventCallbacks: {},
  17. addComponent: function (type, options) {
  18. let c = this[type];
  19. if (!c || options.new) {
  20. const template = components.getTemplate(type);
  21. if (!template)
  22. return;
  23. c = $.extend(true, {}, template);
  24. c.obj = this;
  25. for (let o in options)
  26. c[o] = options[o];
  27. //Only use component to initialize other components?
  28. if (c.init && c.init(options))
  29. return null;
  30. this[c.type] = c;
  31. this.components.push(c);
  32. return c;
  33. }
  34. if (c.extend)
  35. c.extend(options);
  36. return c;
  37. },
  38. removeComponent: function (type) {
  39. let cpn = this[type];
  40. if (!cpn)
  41. return;
  42. this.components.spliceWhere(c => {
  43. return c === cpn;
  44. });
  45. delete this[type];
  46. },
  47. update: function () {
  48. const oComponents = this.components;
  49. let len = oComponents.length;
  50. for (let i = 0; i < len; i++) {
  51. const c = oComponents[i];
  52. if (c.update)
  53. c.update();
  54. if (c.destroyed) {
  55. if (c.destroy)
  56. c.destroy();
  57. oComponents.splice(i, 1);
  58. i--;
  59. len--;
  60. delete this[c.type];
  61. }
  62. }
  63. },
  64. on: function (eventName, callback) {
  65. let list = this.eventCallbacks[eventName] || (this.eventCallbacks[eventName] = []);
  66. list.push(events.on(eventName, callback));
  67. },
  68. setSpritePosition: function () {
  69. const { sprite, chatter, stats, x, y } = this;
  70. if (!sprite)
  71. return;
  72. renderer.setSpritePosition(this);
  73. ['nameSprite', 'chatSprite'].forEach((s, i) => {
  74. const subSprite = this[s];
  75. if (!subSprite)
  76. return;
  77. let yAdd = scale;
  78. if (i === 1) {
  79. yAdd *= -0.8;
  80. yAdd -= (chatter.msg.split('\r\n').length - 1) * scale * 0.8;
  81. }
  82. subSprite.x = (x * scale) + (scale / 2) - (subSprite.width / 2);
  83. subSprite.y = (y * scale) + yAdd;
  84. });
  85. if (stats)
  86. stats.updateHpSprite();
  87. },
  88. updateVisibility: function () {
  89. const { x, y, hidden, isVisible } = this;
  90. const vis = (
  91. !hidden &&
  92. (
  93. this.self ||
  94. (
  95. renderer.sprites[x] &&
  96. renderer.sprites[x][y] &&
  97. renderer.sprites[x][y].length > 0 &&
  98. !renderer.isHidden(x, y)
  99. )
  100. )
  101. );
  102. if (vis === isVisible)
  103. return;
  104. this.isVisible = vis;
  105. this.setVisible(vis);
  106. },
  107. setVisible: function (visible) {
  108. if (this.sprite)
  109. this.sprite.visible = visible;
  110. if (this.nameSprite)
  111. this.nameSprite.visible = (visible && config.showNames);
  112. if (!visible && this.stats && this.stats.hpSprite && this.stats.hpSprite.visible) {
  113. this.stats.hpSprite.visible = false;
  114. this.stats.hpSpriteInner.visible = false;
  115. }
  116. this.components.forEach(c => {
  117. if (c.setVisible)
  118. c.setVisible(visible);
  119. });
  120. },
  121. destroy: function () {
  122. if (this.sprite)
  123. renderer.destroyObject(this);
  124. if (this.nameSprite) {
  125. renderer.destroyObject({
  126. layerName: 'effects',
  127. sprite: this.nameSprite
  128. });
  129. }
  130. const oComponents = this.components;
  131. const cLen = oComponents.length;
  132. for (let i = 0; i < cLen; i++) {
  133. const c = oComponents[i];
  134. if (c.destroy)
  135. c.destroy();
  136. }
  137. this.destroyed = true;
  138. this.offEvents();
  139. },
  140. offEvents: function () {
  141. if (this.pather)
  142. this.pather.resetPath();
  143. for (let e in this.eventCallbacks)
  144. this.eventCallbacks[e].forEach(c => events.off(e, c));
  145. }
  146. };
  147. });