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.
 
 
 

344 lignes
7.3 KiB

  1. define([
  2. 'js/objects/objBase',
  3. 'js/system/events',
  4. 'js/rendering/renderer'
  5. ], function(
  6. objBase,
  7. events,
  8. renderer
  9. ) {
  10. var scale = 40;
  11. return {
  12. showNames: false,
  13. objects: [],
  14. dirty: false,
  15. init: function() {
  16. events.on('onKeyDown', this.onKeyDown.bind(this));
  17. events.on('onGetObject', this.onGetObject.bind(this));
  18. events.on('onRezone', this.onRezone.bind(this));
  19. events.on('onChangeHoverTile', this.getLocation.bind(this));
  20. },
  21. getLocation: function(x, y) {
  22. var objects = this.objects;
  23. var oLen = objects.length;
  24. var closest = 999;
  25. var mob = null;
  26. for (var i = 0; i < oLen; i++) {
  27. var o = objects[i];
  28. if ((!o.stats) || (o.nonSelectable))
  29. continue;
  30. var dx = Math.abs(o.x - x);
  31. if ((dx < 3) && (dx < closest)) {
  32. var dy = Math.abs(o.y - y);
  33. if ((dy < 3) && (dy < closest)) {
  34. mob = o;
  35. closest = Math.max(dx, dy);
  36. }
  37. }
  38. }
  39. events.emit('onMobHover', mob);
  40. },
  41. getClosest: function(x, y, maxDistance, fromMob, callback) {
  42. var objects = this.objects;
  43. var oLen = objects.length;
  44. var list = objects.filter(function(o) {
  45. if ((!o.stats) || (o.nonSelectable) || (o == window.player))
  46. return false;
  47. var dx = Math.abs(o.x - x);
  48. if (dx < maxDistance) {
  49. var dy = Math.abs(o.y - y);
  50. if (dy < maxDistance)
  51. return true;
  52. }
  53. });
  54. if (list.length == 0)
  55. return null;
  56. list.sort(function(a, b) {
  57. var aDistance = Math.max(Math.abs(x - a.x), Math.abs(y - a.y));
  58. var bDistance = Math.max(Math.abs(x - b.x), Math.abs(y - b.y));
  59. return (aDistance - bDistance);
  60. });
  61. if (!fromMob)
  62. return list[0];
  63. var fromIndex = list.firstIndex(function(l) {
  64. return (l.id == fromMob.id);
  65. });
  66. return list[(fromIndex + 1) % list.length];
  67. },
  68. onRezone: function(oldZone) {
  69. var objects = this.objects;
  70. var oLen = objects.length
  71. for (var i = 0; i < oLen; i++) {
  72. var o = objects[i];
  73. if (oldZone == null)
  74. o.destroy();
  75. else if ((o.zoneId == oldZone) && (o.player == null))
  76. o.destroy();
  77. }
  78. window.player.offEvents();
  79. },
  80. onGetObject: function(obj) {
  81. this.dirty = true;
  82. //Things like attacks don't have ids
  83. var exists = null;
  84. if (obj.id != null) {
  85. exists = this.objects.find(function(o) {
  86. return ((o.id == obj.id) && (!o.destroyed));
  87. });
  88. }
  89. if (!exists)
  90. exists = this.buildObject(obj);
  91. else {
  92. this.updateObject(exists, obj);
  93. }
  94. },
  95. buildObject: function(template) {
  96. var obj = $.extend(true, {}, objBase);
  97. var components = template.components || [];
  98. delete template.components;
  99. for (var p in template) {
  100. var value = template[p];
  101. var type = typeof(value);
  102. if (type == 'object') {
  103. } else
  104. obj[p] = value;
  105. }
  106. if (obj.sheetName) {
  107. obj.sprite = renderer.buildObject(obj);
  108. if (template.hidden)
  109. obj.sprite.visible = false;
  110. }
  111. components.forEach(function(c) {
  112. //Map ids to objects
  113. var keys = Object.keys(c).filter(function(k) {
  114. return ((k.indexOf('id') == 0) && (k.length > 2));
  115. });
  116. keys.forEach(function(k) {
  117. var value = c[k];
  118. var newKey = k.substr(2, k.length).toLowerCase();
  119. c[newKey] = this.objects.find(function(o) {
  120. return (o.id == value);
  121. });
  122. delete c[k];
  123. }, this);
  124. obj.addComponent(c.type, c);
  125. }, this);
  126. this.objects.push(obj);
  127. if (obj.self) {
  128. events.emit('onGetPlayer', obj);
  129. window.player = obj;
  130. renderer.setPosition({
  131. x: (obj.x - (renderer.width / (scale * 2))) * scale,
  132. y: (obj.y - (renderer.height / (scale * 2))) * scale
  133. }, true);
  134. }
  135. if ((obj.name) && (obj.sprite)) {
  136. obj.nameSprite = renderer.buildText({
  137. layerName: 'effects',
  138. text: obj.name,
  139. x: (obj.x * scale) + (scale / 2),
  140. y: (obj.y * scale) + scale
  141. });
  142. obj.nameSprite.visible = this.showNames;
  143. }
  144. return obj;
  145. },
  146. updateObject: function(obj, template) {
  147. var components = template.components || [];
  148. components.forEach(function(c) {
  149. //Map ids to objects
  150. var keys = Object.keys(c).filter(function(k) {
  151. return ((k.indexOf('id') == 0) && (k.length > 2));
  152. });
  153. keys.forEach(function(k) {
  154. var value = c[k];
  155. var newKey = k.substr(2, k.length).toLowerCase();
  156. c[newKey] = this.objects.find(function(o) {
  157. return (o.id == value);
  158. });
  159. delete c[k];
  160. }, this);
  161. obj.addComponent(c.type, c);
  162. }, this);
  163. delete template.components;
  164. var oldX = obj.x;
  165. var sprite = obj.sprite;
  166. var moved = false;
  167. for (var p in template) {
  168. var value = template[p];
  169. var type = typeof(value);
  170. if (type != 'object')
  171. obj[p] = value;
  172. if ((p == 'x') || (p == 'y'))
  173. moved = true;
  174. if (sprite) {
  175. if (p == 'x') {
  176. if (obj.x < oldX)
  177. obj.flipX = true;
  178. else if (obj.x > oldX)
  179. obj.flipX = false;
  180. }
  181. }
  182. }
  183. if (moved)
  184. obj.setSpritePosition();
  185. if (((template.sheetName) || (template.cell)) && (sprite))
  186. renderer.setSprite(obj);
  187. if (sprite) {
  188. if (template.hidden != null) {
  189. sprite.visible = !template.hidden;
  190. }
  191. }
  192. if ((template.x != 0) || (template.y != 0)) {
  193. if (obj.stats)
  194. obj.stats.updateHpSprite();
  195. }
  196. if ((!obj.sprite) && (template.sheetName))
  197. obj.sprite = renderer.buildObject(obj);
  198. if ((!obj.nameSprite) && (template.name)) {
  199. obj.nameSprite = renderer.buildText({
  200. layerName: 'effects',
  201. text: template.name,
  202. x: (obj.x * scale) + (scale / 2),
  203. y: (obj.y * scale) + scale
  204. });
  205. obj.nameSprite.visible = this.showNames;
  206. }
  207. obj.setSpritePosition();
  208. },
  209. update: function() {
  210. var objects = this.objects;
  211. var len = objects.length;
  212. for (var i = 0; i < len; i++) {
  213. var o = objects[i];
  214. if (o.destroyed) {
  215. o.destroy();
  216. objects.splice(i, 1);
  217. i--;
  218. len--;
  219. continue;
  220. }
  221. o.update();
  222. if (o.dirty)
  223. this.dirty = true;
  224. }
  225. },
  226. render: function() {
  227. canvas.renderMap();
  228. var objects = this.objects;
  229. var len = objects.length;
  230. var showNames = this.showNames;
  231. var ctx = canvas.layers.effects.ctx;
  232. if (showNames) {
  233. ctx.font = '14px bitty';
  234. ctx.strokeStyle = 'rgb(0, 0, 0)';
  235. }
  236. for (var i = 0; i < len; i++) {
  237. var o = objects[i];
  238. o.render();
  239. if (o.stats) {
  240. var yOffset = 12;
  241. if (o.isChampion)
  242. yOffset = 18;
  243. ctx.globalAlpha = 1;
  244. var statValues = o.stats.values;
  245. var hp = statValues.hp;
  246. var hpMax = statValues.hpMax;
  247. if (hp < hpMax) {
  248. ctx.fillStyle = 'rgb(0, 0, 0)';
  249. ctx.fillRect(o.x * scale, (o.y * scale) - yOffset, scale, 4);
  250. var w = (hp / hpMax) * scale;
  251. ctx.fillStyle = '#cf0056';
  252. ctx.fillRect(o.x * scale, (o.y * scale) - yOffset, w, 4);
  253. }
  254. }
  255. if ((!showNames) || (!o.name))
  256. continue;
  257. //OPTIMIZE: Don't wanna set this every time
  258. ctx.globalAlpha = 1;
  259. ctx.fillStyle = 'rgb(255, 255, 255)';
  260. canvas.renderOutlineText('effects', o.name, (o.x * scale) + (scale / 2), ((o.y + 1) * scale) + (scale / 4), true);
  261. }
  262. },
  263. onKeyDown: function(key) {
  264. if (key == 'v') {
  265. this.showNames = !this.showNames;
  266. var showNames = this.showNames;
  267. var objects = this.objects;
  268. var oLen = objects.length;
  269. for (var i = 0; i < oLen; i++) {
  270. var ns = objects[i].nameSprite;
  271. if (!ns)
  272. continue;
  273. ns.visible = showNames;
  274. }
  275. }
  276. }
  277. };
  278. });