Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

294 рядки
5.9 KiB

  1. define([
  2. 'js/system/client',
  3. 'js/rendering/renderer',
  4. 'js/system/events'
  5. ], function (
  6. client,
  7. renderer,
  8. events
  9. ) {
  10. let objects = null;
  11. require(['js/objects/objects'], function (o) {
  12. objects = o;
  13. });
  14. return {
  15. type: 'spellbook',
  16. hoverTarget: null,
  17. target: null,
  18. selected: null,
  19. reticleState: 0,
  20. reticleCd: 0,
  21. reticleCdMax: 10,
  22. renderRange: null,
  23. reticleSprite: null,
  24. targetSprite: null,
  25. shiftDown: false,
  26. init: function (blueprint) {
  27. this.targetSprite = renderer.buildObject({
  28. sheetName: 'ui',
  29. layerName: 'effects',
  30. cell: this.reticleState
  31. });
  32. this.targetSprite.visible = false;
  33. this.reticleSprite = renderer.buildObject({
  34. sheetName: 'ui',
  35. layerName: 'effects',
  36. cell: 8 + this.reticleState
  37. });
  38. this.reticleSprite.visible = false;
  39. events.emit('onGetSpells', this.spells);
  40. this.reticleCd = this.reticleCdMax;
  41. this.obj.on('onDeath', this.onDeath.bind(this));
  42. this.obj.on('onMobHover', this.onMobHover.bind(this));
  43. this.obj.on('mouseDown', this.onMouseDown.bind(this));
  44. this.obj.on('onKeyDown', this.onKeyDown.bind(this));
  45. this.obj.on('onKeyUp', this.onKeyUp.bind(this));
  46. },
  47. extend: function (blueprint) {
  48. if (blueprint.removeSpells) {
  49. blueprint.removeSpells.forEach(function (spellId) {
  50. this.spells.spliceWhere(function (s) {
  51. return (s.id === spellId);
  52. });
  53. }, this);
  54. events.emit('onGetSpells', this.spells);
  55. }
  56. if (blueprint.getSpells) {
  57. blueprint.getSpells.forEach(function (s) {
  58. let existIndex = this.spells.firstIndex(function (spell) {
  59. return (spell.id === s.id);
  60. });
  61. if (existIndex > -1) {
  62. this.spells.splice(existIndex, 1, s);
  63. return;
  64. }
  65. this.spells.push(s);
  66. this.spells = this.spells.sort(function (a, b) {
  67. return (a.id - b.id);
  68. });
  69. }, this);
  70. events.emit('onGetSpells', this.spells);
  71. }
  72. },
  73. getSpell: function (number) {
  74. let spellNumber = (number === ' ') ? 0 : number;
  75. let spell = this.spells.find(function (s) {
  76. return (s.id === spellNumber);
  77. });
  78. if (!spell)
  79. return null;
  80. return spell;
  81. },
  82. onMobHover: function (target) {
  83. this.hoverTarget = target;
  84. },
  85. onMouseDown: function (e, target) {
  86. this.target = target || this.hoverTarget;
  87. if (this.target) {
  88. this.targetSprite.x = this.target.x * scale;
  89. this.targetSprite.y = this.target.y * scale;
  90. this.targetSprite.visible = true;
  91. } else {
  92. client.request({
  93. cpn: 'player',
  94. method: 'queueAction',
  95. data: {
  96. action: 'spell',
  97. priority: true,
  98. target: null
  99. }
  100. });
  101. this.targetSprite.visible = false;
  102. }
  103. events.emit('onSetTarget', this.target, e);
  104. },
  105. tabTarget: function () {
  106. let closest = objects.getClosest(window.player.x, window.player.y, 10, this.shiftDown, this.target);
  107. this.target = closest;
  108. this.targetSprite.visible = !!this.target;
  109. events.emit('onSetTarget', this.target, null);
  110. },
  111. build: function (destroy) {
  112. client.request({
  113. cpn: 'player',
  114. method: 'performAction',
  115. data: {
  116. instanceModule: 'customMap',
  117. method: 'customize',
  118. data: {
  119. tile: 189,
  120. direction: this.obj.keyboardMover.direction,
  121. destroy: destroy
  122. }
  123. },
  124. callback: renderer.onGetMapCustomization.bind(renderer)
  125. });
  126. },
  127. onKeyDown: function (key) {
  128. if (key === 'b') {
  129. this.build();
  130. return;
  131. } else if (key === 'n') {
  132. this.build(true);
  133. return;
  134. }
  135. if (key === 'shift') {
  136. this.shiftDown = true;
  137. return;
  138. } else if (key === 'tab') {
  139. this.tabTarget();
  140. return;
  141. }
  142. let spell = this.getSpell(~~key);
  143. if (!spell)
  144. return;
  145. let oldTarget = null;
  146. if (this.shiftDown) {
  147. oldTarget = this.target;
  148. this.target = this.obj;
  149. }
  150. if ((!spell.aura) && (!spell.targetGround) && (!spell.autoTargetFollower) && (!this.target))
  151. return;
  152. let hoverTile = this.obj.mouseMover.hoverTile;
  153. let target = hoverTile;
  154. if ((spell.autoTargetFollower) && (!this.target))
  155. target = null;
  156. else if ((!spell.targetGround) && (this.target))
  157. target = this.target.id;
  158. if (this.shiftDown)
  159. this.target = oldTarget;
  160. if ((target === this.obj) && (spell.noTargetSelf))
  161. return;
  162. client.request({
  163. cpn: 'player',
  164. method: 'queueAction',
  165. data: {
  166. action: 'spell',
  167. priority: true,
  168. spell: spell.id,
  169. auto: spell.auto,
  170. target: target,
  171. self: this.shiftDown
  172. }
  173. });
  174. },
  175. onKeyUp: function (key) {
  176. if (key === 'shift') {
  177. this.shiftDown = false;
  178. return;
  179. }
  180. },
  181. onDeath: function () {
  182. this.target = null;
  183. this.targetSprite.visible = false;
  184. },
  185. update: function () {
  186. if ((this.target) && (this.target.destroyed)) {
  187. this.target = null;
  188. this.targetSprite.visible = false;
  189. }
  190. if ((this.target) && (this.target.nonSelectable)) {
  191. this.target = null;
  192. this.targetSprite.visible = false;
  193. }
  194. if (this.reticleCd > 0)
  195. this.reticleCd--;
  196. else {
  197. this.reticleCd = this.reticleCdMax;
  198. this.reticleState++;
  199. if (this.reticleState === 4)
  200. this.reticleState = 0;
  201. }
  202. if (!this.target)
  203. return;
  204. renderer.setSprite({
  205. sprite: this.targetSprite,
  206. cell: this.reticleState,
  207. sheetName: 'ui'
  208. });
  209. this.targetSprite.x = this.target.x * scale;
  210. this.targetSprite.y = this.target.y * scale;
  211. },
  212. destroy: function () {
  213. if (this.targetSprite) {
  214. renderer.destroyObject({
  215. layerName: 'effects',
  216. sprite: this.targetSprite
  217. });
  218. }
  219. },
  220. render: function () {
  221. if (this.reticleCd > 0)
  222. this.reticleCd--;
  223. else {
  224. this.reticleCd = this.reticleCdMax;
  225. this.reticleState++;
  226. if (this.reticleState === 4)
  227. this.reticleState = 0;
  228. }
  229. if (!this.target)
  230. return;
  231. renderer.setSprite({
  232. sprite: this.targetSprite,
  233. cell: this.reticleState,
  234. sheetName: 'ui'
  235. });
  236. this.targetSprite.x = this.target.x * scale;
  237. this.targetSprite.y = this.target.y * scale;
  238. }
  239. };
  240. });