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.
 
 
 

340 lines
7.5 KiB

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