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.
 
 
 

300 lines
5.9 KiB

  1. define([
  2. 'js/system/client',
  3. 'js/renderer',
  4. 'js/system/events'
  5. ], function(
  6. client,
  7. renderer,
  8. events
  9. ) {
  10. var scale = 40;
  11. var 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. tarpSprite: 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. var 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. if (this.spells.length - 1 >= s.id)
  67. this.spells.splice(s.id, 0, s);
  68. else
  69. this.spells.push(s);
  70. }, this);
  71. events.emit('onGetSpells', this.spells);
  72. }
  73. },
  74. getSpell: function(number) {
  75. var spellNumber = -1;
  76. if (number == 1) {
  77. spellNumber = 0;
  78. } else if (number == 2)
  79. spellNumber = 1;
  80. else if (number == 3)
  81. spellNumber = 2;
  82. if (spellNumber == -1)
  83. return;
  84. var spell = this.spells[spellNumber];
  85. if (!spell)
  86. return null;
  87. return spell;
  88. },
  89. onMobHover: function(target) {
  90. this.hoverTarget = target;
  91. },
  92. onMouseDown: function(e, target) {
  93. this.target = target || this.hoverTarget;
  94. if (this.target) {
  95. this.targetSprite.x = this.target.x * scale;
  96. this.targetSprite.y = this.target.y * scale;
  97. this.targetSprite.visible = true;
  98. } else {
  99. client.request({
  100. cpn: 'player',
  101. method: 'queueAction',
  102. data: {
  103. action: 'spell',
  104. priority: true,
  105. target: null
  106. }
  107. });
  108. this.targetSprite.visible = false;
  109. }
  110. events.emit('onSetTarget', this.target, e);
  111. },
  112. tabTarget: function() {
  113. this.onMouseDown(null, objects.getClosest(window.player.x, window.player.y, 10, false, this.target));
  114. },
  115. shiftTabTarget: function() {
  116. this.onMouseDown(null, objects.getClosest(window.player.x, window.player.y, 10, true, this.target));
  117. },
  118. build: function(destroy) {
  119. client.request({
  120. cpn: 'player',
  121. method: 'performAction',
  122. data: {
  123. instanceModule: 'customMap',
  124. method: 'customize',
  125. data: {
  126. tile: 189,
  127. direction: this.obj.keyboardMover.direction,
  128. destroy: destroy
  129. }
  130. },
  131. callback: renderer.onGetMapCustomization.bind(renderer)
  132. });
  133. },
  134. onKeyDown: function(key) {
  135. if (key == 'b') {
  136. this.build();
  137. return;
  138. }
  139. else if (key == 'n') {
  140. this.build(true);
  141. return;
  142. }
  143. if (key == 'shift') {
  144. this.shiftDown = true;
  145. return;
  146. } else if (key == 'tab') {
  147. if (this.shiftDown) {
  148. this.shiftTabTarget();
  149. } else {
  150. this.tabTarget();
  151. }
  152. return;
  153. }
  154. var spell = this.getSpell(key);
  155. if (!spell)
  156. return;
  157. var oldTarget = null;
  158. if (this.shiftDown) {
  159. oldTarget = this.target;
  160. this.target = this.obj;
  161. }
  162. if ((!spell.targetGround) && (!this.target))
  163. return;
  164. var hoverTile = this.obj.mouseMover.hoverTile;
  165. var target = spell.targetGround ? hoverTile : this.target.id;
  166. if (this.shiftDown)
  167. this.target = oldTarget;
  168. client.request({
  169. cpn: 'player',
  170. method: 'queueAction',
  171. data: {
  172. action: 'spell',
  173. priority: true,
  174. spell: key - 1,
  175. auto: spell.auto,
  176. target: target,
  177. self: this.shiftDown
  178. }
  179. });
  180. },
  181. onKeyUp: function(key) {
  182. if (key == 'shift') {
  183. this.shiftDown = false;
  184. return;
  185. }
  186. },
  187. onDeath: function() {
  188. this.target = null;
  189. this.targetSprite.visible = false;
  190. },
  191. update: function() {
  192. if ((this.target) && (this.target.destroyed)) {
  193. this.target = null;
  194. this.targetSprite.visible = false;
  195. }
  196. if ((this.target) && (this.target.nonSelectable)) {
  197. this.target = null;
  198. this.targetSprite.visible = false;
  199. }
  200. if (this.reticleCd > 0)
  201. this.reticleCd--;
  202. else {
  203. this.reticleCd = this.reticleCdMax;
  204. this.reticleState++;
  205. if (this.reticleState == 4)
  206. this.reticleState = 0;
  207. }
  208. if (!this.target)
  209. return;
  210. renderer.setSprite({
  211. sprite: this.targetSprite,
  212. cell: this.reticleState,
  213. sheetName: 'ui'
  214. });
  215. this.targetSprite.x = this.target.x * scale;
  216. this.targetSprite.y = this.target.y * scale;
  217. },
  218. destroy: function() {
  219. if (this.targetSprite) {
  220. renderer.destroyObject({
  221. layerName: 'effects',
  222. sprite: this.targetSprite
  223. });
  224. }
  225. },
  226. render: function() {
  227. if (this.reticleCd > 0)
  228. this.reticleCd--;
  229. else {
  230. this.reticleCd = this.reticleCdMax;
  231. this.reticleState++;
  232. if (this.reticleState == 4)
  233. this.reticleState = 0;
  234. }
  235. if (!this.target)
  236. return;
  237. renderer.setSprite({
  238. sprite: this.targetSprite,
  239. cell: this.reticleState,
  240. sheetName: 'ui'
  241. });
  242. this.targetSprite.x = this.target.x * scale;
  243. this.targetSprite.y = this.target.y * scale;
  244. }
  245. };
  246. });