Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

291 righe
5.6 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, this.target));
  114. },
  115. build: function(destroy) {
  116. client.request({
  117. cpn: 'player',
  118. method: 'performAction',
  119. data: {
  120. instanceModule: 'customMap',
  121. method: 'customize',
  122. data: {
  123. tile: 189,
  124. direction: this.obj.keyboardMover.direction,
  125. destroy: destroy
  126. }
  127. },
  128. callback: renderer.onGetMapCustomization.bind(renderer)
  129. });
  130. },
  131. onKeyDown: function(key) {
  132. if (key == 'b') {
  133. this.build();
  134. return;
  135. }
  136. else if (key == 'n') {
  137. this.build(true);
  138. return;
  139. }
  140. if (key == 'shift') {
  141. this.shiftDown = true;
  142. return;
  143. } else if (key == 'tab') {
  144. this.tabTarget();
  145. return;
  146. }
  147. var spell = this.getSpell(key);
  148. if (!spell)
  149. return;
  150. var oldTarget = null;
  151. if (this.shiftDown) {
  152. oldTarget = this.target;
  153. this.target = this.obj;
  154. }
  155. if ((!spell.targetGround) && (!this.target))
  156. return;
  157. var hoverTile = this.obj.mouseMover.hoverTile;
  158. var target = spell.targetGround ? hoverTile : this.target.id;
  159. if (this.shiftDown)
  160. this.target = oldTarget;
  161. client.request({
  162. cpn: 'player',
  163. method: 'queueAction',
  164. data: {
  165. action: 'spell',
  166. priority: true,
  167. spell: key - 1,
  168. auto: spell.auto,
  169. target: target,
  170. self: this.shiftDown
  171. }
  172. });
  173. },
  174. onKeyUp: function(key) {
  175. if (key == 'shift') {
  176. this.shiftDown = false;
  177. return;
  178. }
  179. },
  180. onDeath: function() {
  181. this.target = null;
  182. this.targetSprite.visible = false;
  183. },
  184. update: function() {
  185. if ((this.target) && (this.target.destroyed)) {
  186. this.target = null;
  187. this.targetSprite.visible = false;
  188. }
  189. if ((this.target) && (this.target.nonSelectable)) {
  190. this.target = null;
  191. this.targetSprite.visible = false;
  192. }
  193. if (this.reticleCd > 0)
  194. this.reticleCd--;
  195. else {
  196. this.reticleCd = this.reticleCdMax;
  197. this.reticleState++;
  198. if (this.reticleState == 4)
  199. this.reticleState = 0;
  200. }
  201. if (!this.target)
  202. return;
  203. renderer.setSprite({
  204. sprite: this.targetSprite,
  205. cell: this.reticleState,
  206. sheetName: 'ui'
  207. });
  208. this.targetSprite.x = this.target.x * scale;
  209. this.targetSprite.y = this.target.y * scale;
  210. },
  211. destroy: function() {
  212. if (this.targetSprite) {
  213. renderer.destroyObject({
  214. layerName: 'effects',
  215. sprite: this.targetSprite
  216. });
  217. }
  218. },
  219. render: function() {
  220. if (this.reticleCd > 0)
  221. this.reticleCd--;
  222. else {
  223. this.reticleCd = this.reticleCdMax;
  224. this.reticleState++;
  225. if (this.reticleState == 4)
  226. this.reticleState = 0;
  227. }
  228. if (!this.target)
  229. return;
  230. renderer.setSprite({
  231. sprite: this.targetSprite,
  232. cell: this.reticleState,
  233. sheetName: 'ui'
  234. });
  235. this.targetSprite.x = this.target.x * scale;
  236. this.targetSprite.y = this.target.y * scale;
  237. }
  238. };
  239. });