Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

254 lignes
5.4 KiB

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