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.
 
 
 

161 lines
3.7 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('onGetStats', this.onGetStats.bind(this));
  21. setInterval(this.update.bind(this), 100);
  22. },
  23. onGetSpells: function (spells) {
  24. this.el.empty();
  25. this.spells = spells;
  26. for (let i = 0; i < spells.length; i++) {
  27. let icon = spells[i].icon;
  28. let x = -(icon[0] * 64);
  29. let y = -(icon[1] * 64);
  30. let hotkey = (spells[i].id == 0) ? 'space' : spells[i].id;
  31. let html = templateSpell
  32. .replace('$HOTKEY$', hotkey);
  33. let el = $(html)
  34. .appendTo(this.el);
  35. el
  36. .on('mouseover', this.onShowTooltip.bind(this, el, spells[i]))
  37. .on('mouseleave', this.onHideTooltip.bind(this, el));
  38. let spritesheet = spells[i].spritesheet || '../../../images/abilityIcons.png';
  39. el
  40. .find('.icon').css({
  41. background: 'url("' + spritesheet + '") ' + x + 'px ' + y + 'px'
  42. })
  43. .next().html(hotkey);
  44. this.onGetSpellCooldowns({
  45. spell: spells[i].id,
  46. cd: spells[i].cd * 350 //HACK - we don't actually know how long a tick is
  47. });
  48. }
  49. },
  50. onShowTooltip: function (el, spell) {
  51. let pos = el.offset();
  52. pos = {
  53. x: pos.left + 56,
  54. y: pos.top + el.height() + 16
  55. };
  56. let cd = ~~((spell.cdMax * 350) / 1000);
  57. let values = Object.keys(spell.values).filter(function (v) {
  58. return ((v != 'damage') && (v != 'healing'));
  59. }).map(function (v) {
  60. return v + ': ' + spell.values[v];
  61. }).join('<br />');
  62. let manaCost = spell.manaCost;
  63. if (spell.manaReserve)
  64. manaCost = ~~(spell.manaReserve.percentage * 100) + '% reserved';
  65. let tooltip = templateTooltip
  66. .replace('$NAME$', spell.name)
  67. .replace('$DESCRIPTION$', spell.description)
  68. .replace('$MANA$', manaCost)
  69. .replace('$CD$', cd + 's')
  70. .replace('$VALUES$', values)
  71. .replace('$ELEMENT$', spell.element ? 'element: ' + spell.element : '');
  72. if (spell.range) {
  73. tooltip = tooltip
  74. .replace('$RANGE$', spell.range);
  75. } else {
  76. tooltip = tooltip
  77. .replace('range', 'range hidden');
  78. }
  79. events.emit('onShowTooltip', tooltip, el[0], pos, 200, false, true, this.el.css('z-index'));
  80. },
  81. onHideTooltip: function (el) {
  82. events.emit('onHideTooltip', el[0]);
  83. },
  84. onGetSpellCooldowns: function (options) {
  85. let spell = this.spells.find(function (s) {
  86. return (s.id == options.spell);
  87. });
  88. spell.ttl = options.cd;
  89. spell.ttlStart = +new Date();
  90. },
  91. onGetStats: function (stats) {
  92. let mana = stats.mana;
  93. let spells = this.spells;
  94. if (!spells)
  95. return;
  96. for (let i = 0; i < spells.length; i++) {
  97. let spell = spells[i];
  98. let el = this.el.children('div').eq(i).find('.hotkey');
  99. el.removeClass('no-mana');
  100. if (spell.manaCost > mana)
  101. el.addClass('no-mana');
  102. }
  103. },
  104. update: function () {
  105. let spells = this.spells;
  106. if (!spells)
  107. return;
  108. let time = +new Date();
  109. for (let i = 0; i < spells.length; i++) {
  110. let spell = spells[i];
  111. if (!spell.ttl) {
  112. this.el.children('div').eq(i).find('.cooldown').css({
  113. width: '0%'
  114. });
  115. continue;
  116. }
  117. let elapsed = time - spell.ttlStart;
  118. let width = 1 - (elapsed / spell.ttl);
  119. if (width <= 0) {
  120. delete spell.ttl;
  121. width = 0;
  122. }
  123. width = Math.ceil((width * 100) / 4) * 4;
  124. this.el.children('div').eq(i).find('.cooldown').css({
  125. width: width + '%'
  126. });
  127. }
  128. }
  129. };
  130. });