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.
 
 
 

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