Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

190 rader
4.4 KiB

  1. define([
  2. 'js/system/events',
  3. 'html!ui/templates/spells/template',
  4. 'css!ui/templates/spells/styles',
  5. 'html!ui/templates/spells/templateSpell',
  6. 'html!ui/templates/spells/templateTooltip'
  7. ], function (
  8. events,
  9. template,
  10. styles,
  11. templateSpell,
  12. templateTooltip
  13. ) {
  14. return {
  15. tpl: template,
  16. spells: null,
  17. postRender: function () {
  18. this.onEvent('onGetSpells', this.onGetSpells.bind(this));
  19. this.onEvent('onGetSpellCooldowns', this.onGetSpellCooldowns.bind(this));
  20. this.onEvent('onGetSpellActive', this.onGetSpellActive.bind(this));
  21. this.onEvent('onGetStats', this.onGetStats.bind(this));
  22. setInterval(this.update.bind(this), 100);
  23. },
  24. onGetSpells: function (spells) {
  25. this.el.empty();
  26. this.spells = spells;
  27. for (let i = 0; i < spells.length; i++) {
  28. let spell = spells[i];
  29. let icon = spell.icon;
  30. let x = -(icon[0] * 64);
  31. let y = -(icon[1] * 64);
  32. let hotkey = (spell.id === 0) ? 'space' : spells[i].id;
  33. let html = templateSpell
  34. .replace('$HOTKEY$', hotkey);
  35. let el = $(html)
  36. .appendTo(this.el);
  37. el
  38. .on('click', this.onClickSpell.bind(this, hotkey))
  39. .on('mouseover', this.onShowTooltip.bind(this, el, spell))
  40. .on('mouseleave', this.onHideTooltip.bind(this, el));
  41. let spritesheet = spell.spritesheet || '../../../images/abilityIcons.png';
  42. el
  43. .find('.icon').css({
  44. background: 'url("' + spritesheet + '") ' + x + 'px ' + y + 'px'
  45. })
  46. .next().html(hotkey);
  47. //HACK - we don't actually know how long a tick is
  48. if (spell.cd) {
  49. this.onGetSpellCooldowns({
  50. spell: spell.id,
  51. cd: spell.cd * 350
  52. });
  53. delete spell.cd;
  54. }
  55. }
  56. },
  57. onClickSpell: function (hotkey, e) {
  58. e.preventDefault();
  59. let key = (hotkey === 'space') ? ' ' : hotkey;
  60. window.player.spellbook.onKeyDown(key);
  61. return false;
  62. },
  63. onShowTooltip: function (el, spell) {
  64. if (isMobile)
  65. return false;
  66. let pos = el.offset();
  67. pos = {
  68. x: pos.left + 56,
  69. y: pos.top + el.height() + 16
  70. };
  71. let values = Object.keys(spell.values).filter(function (v) {
  72. return ((v !== 'damage') && (v !== 'healing'));
  73. }).map(function (v) {
  74. return v + ': ' + spell.values[v];
  75. }).join('<br />');
  76. let manaCost = spell.manaCost;
  77. if (spell.manaReserve)
  78. manaCost = ~~(spell.manaReserve.percentage * 100) + '% reserved';
  79. let tooltip = templateTooltip
  80. .replace('$NAME$', spell.name)
  81. .replace('$DESCRIPTION$', spell.description)
  82. .replace('$MANA$', manaCost)
  83. .replace('$CD$', spell.cdMax + ' Ticks')
  84. .replace('$VALUES$', values)
  85. .replace('$ELEMENT$', spell.element ? 'element: ' + spell.element : '');
  86. if (spell.range) {
  87. tooltip = tooltip
  88. .replace('$RANGE$', spell.range);
  89. } else {
  90. tooltip = tooltip
  91. .replace('range', 'range hidden');
  92. }
  93. events.emit('onShowTooltip', tooltip, el[0], pos, 200, false, true, this.el.css('z-index'));
  94. },
  95. onHideTooltip: function (el) {
  96. events.emit('onHideTooltip', el[0]);
  97. },
  98. onGetSpellCooldowns: function (options) {
  99. let spell = this.spells.find(function (s) {
  100. return (s.id === options.spell);
  101. });
  102. spell.ttl = options.cd;
  103. spell.ttlStart = +new Date();
  104. },
  105. onGetSpellActive: function (options) {
  106. let spellIndex = this.spells.findIndex(s => s.id === options.spell);
  107. let el = this.el.children('div')
  108. .eq(spellIndex)
  109. .removeClass('active');
  110. if (options.active)
  111. el.addClass('active');
  112. },
  113. onGetStats: function (stats) {
  114. let mana = stats.mana;
  115. let spells = this.spells;
  116. if (!spells)
  117. return;
  118. for (let i = 0; i < spells.length; i++) {
  119. let spell = spells[i];
  120. let el = this.el.children('div').eq(i).find('.hotkey');
  121. el.removeClass('no-mana');
  122. if (spell.manaCost > mana)
  123. el.addClass('no-mana');
  124. }
  125. },
  126. update: function () {
  127. let spells = this.spells;
  128. if (!spells)
  129. return;
  130. let time = +new Date();
  131. for (let i = 0; i < spells.length; i++) {
  132. let spell = spells[i];
  133. if (!spell.ttl) {
  134. this.el.children('div').eq(i).find('.cooldown').css({
  135. width: '0%'
  136. });
  137. continue;
  138. }
  139. let elapsed = time - spell.ttlStart;
  140. let width = 1 - (elapsed / spell.ttl);
  141. if (width <= 0) {
  142. delete spell.ttl;
  143. width = 0;
  144. }
  145. width = Math.ceil((width * 100) / 4) * 4;
  146. this.el.children('div').eq(i).find('.cooldown').css({
  147. width: width + '%'
  148. });
  149. }
  150. }
  151. };
  152. });