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.

215 lines
4.5 KiB

  1. define([
  2. 'js/system/client',
  3. 'js/rendering/renderer',
  4. 'js/system/events',
  5. 'js/input'
  6. ], function (
  7. client,
  8. renderer,
  9. events,
  10. input
  11. ) {
  12. let objects = null;
  13. require(['js/objects/objects'], function (o) {
  14. objects = o;
  15. });
  16. return {
  17. type: 'spellbook',
  18. hoverTarget: null,
  19. target: null,
  20. targetSprite: null,
  21. reticleState: 0,
  22. reticleCd: 0,
  23. reticleCdMax: 10,
  24. reticleSprite: null,
  25. init: function (blueprint) {
  26. this.targetSprite = renderer.buildObject({
  27. sheetName: 'ui',
  28. layerName: 'effects',
  29. cell: 0,
  30. visible: false
  31. });
  32. this.reticleSprite = renderer.buildObject({
  33. sheetName: 'ui',
  34. layerName: 'effects',
  35. cell: 8,
  36. visible: false
  37. });
  38. events.emit('onGetSpells', this.spells);
  39. this.reticleCd = this.reticleCdMax;
  40. this.obj.on('onDeath', this.onDeath.bind(this));
  41. this.obj.on('onMobHover', this.onMobHover.bind(this));
  42. this.obj.on('mouseDown', this.onMouseDown.bind(this));
  43. this.obj.on('onKeyDown', this.onKeyDown.bind(this));
  44. },
  45. extend: function (blueprint) {
  46. if (blueprint.removeSpells) {
  47. blueprint.removeSpells.forEach(r => this.spells.spliceWhere(s => s.id === r));
  48. events.emit('onGetSpells', this.spells);
  49. }
  50. if (blueprint.getSpells) {
  51. blueprint.getSpells.forEach(function (s) {
  52. let existIndex = this.spells.firstIndex(f => f.id === s.id);
  53. if (existIndex > -1) {
  54. this.spells.splice(existIndex, 1, s);
  55. return;
  56. }
  57. this.spells.push(s);
  58. this.spells.sort((a, b) => a.id - b.id);
  59. }, this);
  60. events.emit('onGetSpells', this.spells);
  61. }
  62. },
  63. getSpell: function (number) {
  64. let spellNumber = (number === ' ') ? 0 : number;
  65. let spell = this.spells.find(s => s.id === spellNumber);
  66. return spell;
  67. },
  68. onMobHover: function (target) {
  69. this.hoverTarget = target;
  70. },
  71. onMouseDown: function (e, target) {
  72. if (!target && this.target && (!this.hoverTarget || this.hoverTarget.id !== this.target.id)) {
  73. client.request({
  74. cpn: 'player',
  75. method: 'queueAction',
  76. data: {
  77. action: 'spell',
  78. priority: true,
  79. target: null
  80. }
  81. });
  82. }
  83. this.target = target || this.hoverTarget;
  84. if (this.target) {
  85. this.targetSprite.x = this.target.x * scale;
  86. this.targetSprite.y = this.target.y * scale;
  87. this.targetSprite.visible = true;
  88. } else
  89. this.targetSprite.visible = false;
  90. events.emit('onSetTarget', this.target, e);
  91. },
  92. tabTarget: function () {
  93. let closest = objects.getClosest(window.player.x, window.player.y, 10, input.isKeyDown('shift'), this.target);
  94. this.target = closest;
  95. this.targetSprite.visible = !!this.target;
  96. events.emit('onSetTarget', this.target, null);
  97. },
  98. onKeyDown: function (key) {
  99. if (key === 'tab') {
  100. this.tabTarget();
  101. return;
  102. } else if (isNaN(key))
  103. return;
  104. let spell = this.getSpell(~~key);
  105. if (!spell)
  106. return;
  107. let isShiftDown = input.isKeyDown('shift');
  108. let oldTarget = null;
  109. if (isShiftDown) {
  110. oldTarget = this.target;
  111. this.target = this.obj;
  112. }
  113. if (!spell.aura && !spell.targetGround && !spell.autoTargetFollower && !this.target)
  114. return;
  115. let hoverTile = (this.obj.mouseMover || this.obj.touchMover).hoverTile;
  116. let target = hoverTile;
  117. if (spell.autoTargetFollower && !this.target)
  118. target = null;
  119. else if (!spell.targetGround && this.target)
  120. target = this.target.id;
  121. if (isShiftDown)
  122. this.target = oldTarget;
  123. if (target === this.obj && spell.noTargetSelf)
  124. return;
  125. client.request({
  126. cpn: 'player',
  127. method: 'queueAction',
  128. data: {
  129. action: 'spell',
  130. priority: input.isKeyDown('ctrl'),
  131. spell: spell.id,
  132. target: target,
  133. self: isShiftDown
  134. }
  135. });
  136. },
  137. onDeath: function () {
  138. this.target = null;
  139. this.targetSprite.visible = false;
  140. },
  141. update: function () {
  142. if (this.reticleCd > 0)
  143. this.reticleCd--;
  144. else {
  145. this.reticleCd = this.reticleCdMax;
  146. this.reticleState++;
  147. if (this.reticleState === 4)
  148. this.reticleState = 0;
  149. }
  150. let target = this.target;
  151. if (!target)
  152. return;
  153. if (this.target.destroyed || this.target.nonSelectable) {
  154. this.target = null;
  155. this.targetSprite.visible = false;
  156. }
  157. this.targetSprite.x = target.x * scale;
  158. this.targetSprite.y = target.y * scale;
  159. renderer.setSprite({
  160. sprite: this.targetSprite,
  161. cell: this.reticleState,
  162. sheetName: 'ui'
  163. });
  164. },
  165. destroy: function () {
  166. if (this.targetSprite) {
  167. renderer.destroyObject({
  168. layerName: 'effects',
  169. sprite: this.targetSprite
  170. });
  171. }
  172. }
  173. };
  174. });