Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

358 linhas
7.4 KiB

  1. define([
  2. 'components/components'
  3. ], function(
  4. components
  5. ) {
  6. return {
  7. components: [],
  8. actionQueue: [],
  9. addComponent: function(type, blueprint, isTransfer) {
  10. var cpn = this[type];
  11. if (!cpn) {
  12. var template = components.components[type];
  13. if (!template) {
  14. template = extend(true, {
  15. type: type
  16. }, blueprint || {});
  17. }
  18. var cpn = extend(true, {}, template);
  19. cpn.obj = this;
  20. this.components.push(cpn);
  21. this[cpn.type] = cpn;
  22. }
  23. if ((cpn.init) && (this.instance != null))
  24. cpn.init(blueprint || {}, isTransfer);
  25. else {
  26. for (var p in blueprint) {
  27. cpn[p] = blueprint[p];
  28. }
  29. }
  30. return cpn;
  31. },
  32. removeComponent: function(type) {
  33. var cpn = this[type];
  34. if (!cpn)
  35. return;
  36. cpn.destroyed = true;
  37. },
  38. extendComponent: function(ext, type, blueprint) {
  39. var template = require('./components/extensions/' + type);
  40. var cpn = this[ext];
  41. extend(true, cpn, template);
  42. if (template.init)
  43. cpn.init(blueprint);
  44. return cpn;
  45. },
  46. update: function() {
  47. var usedTurn = false;
  48. var components = this.components;
  49. var len = components.length;
  50. for (var i = 0; i < len; i++) {
  51. var c = components[i];
  52. if (c.update) {
  53. if (c.update())
  54. usedTurn = true;
  55. }
  56. if (c.destroyed) {
  57. this.syncer.setSelfArray(false, 'removeComponents', c.type);
  58. this.components.spliceWhere(f => (f == c));
  59. delete this[c.type];
  60. len--;
  61. i--;
  62. }
  63. }
  64. if (!usedTurn) {
  65. this.performQueue();
  66. }
  67. },
  68. getSimple: function(self, isSave) {
  69. var s = this.simplify(null, self, isSave);
  70. if (this.instance)
  71. s.zoneId = this.instance.zoneId;
  72. if ((self) && (!isSave)) {
  73. var syncer = this.syncer;
  74. if (this.syncer) {
  75. //Add things that have been queued by the syncer (that aren't tied to server-side components)
  76. var components = this.syncer.oSelf.components
  77. .filter((c => !this[c.type]), this)
  78. .forEach(c => s.components.push(c));
  79. }
  80. }
  81. return s;
  82. },
  83. simplify: function(o, self, isSave) {
  84. var result = {};
  85. if (!o) {
  86. result.components = [];
  87. o = this;
  88. }
  89. for (var p in o) {
  90. var value = o[p];
  91. if (value == null)
  92. continue;
  93. var type = typeof(value);
  94. //build component
  95. if (type == 'object') {
  96. if (value.type) {
  97. if (!value.simplify) {
  98. if (self) {
  99. result.components.push({
  100. type: value.type
  101. });
  102. }
  103. } else {
  104. var component = null;
  105. if (isSave)
  106. component = value.save ? value.save() : value.simplify(self);
  107. else
  108. component = value.simplify(self);
  109. if (value.destroyed) {
  110. if (!component) {
  111. component = {
  112. type: value.type
  113. };
  114. }
  115. component.destroyed = true;
  116. }
  117. if (component)
  118. result.components.push(component);
  119. }
  120. }
  121. } else if (type == 'function') {
  122. } else
  123. result[p] = value;
  124. }
  125. return result;
  126. },
  127. sendEvent: function(event, data) {
  128. process.send({
  129. method: 'event',
  130. id: this.serverId,
  131. data: {
  132. event: event,
  133. data: data
  134. }
  135. });
  136. },
  137. queue: function(action) {
  138. if (action.list) {
  139. var type = action.action;
  140. var data = action.data;
  141. var dLen = data.length;
  142. for (var i = 0; i < dLen; i++) {
  143. var d = data[i];
  144. this.actionQueue.push({
  145. action: type,
  146. data: d
  147. });
  148. }
  149. return;
  150. }
  151. if (action.priority)
  152. this.actionQueue.splice(this.actionQueue.firstIndex(a => !a.priority), 0, action);
  153. else
  154. this.actionQueue.push(action);
  155. },
  156. dequeue: function() {
  157. if (this.actionQueue.length == 0)
  158. return null;
  159. return this.actionQueue.splice(0, 1)[0];
  160. },
  161. clearQueue: function() {
  162. if (this.serverId != null) {
  163. this.instance.syncer.queue('onClearQueue', {
  164. id: this.id
  165. }, [this.serverId]);
  166. }
  167. this.actionQueue = [];
  168. },
  169. performAction: function(action) {
  170. if (action.instanceModule) {
  171. /*action.data.obj = this;
  172. this.instance[action.instanceModule][action.method](action.data);
  173. this.inventory.resolveCallback(action.data, action.data.result);*/
  174. return;
  175. }
  176. var cpn = this[action.cpn];
  177. if (!cpn)
  178. return;
  179. cpn[action.method](action.data);
  180. },
  181. performQueue: function() {
  182. var q = this.dequeue();
  183. if (!q) {
  184. return;
  185. }
  186. if (q.action == 'move') {
  187. if ((this.actionQueue[0]) && (this.actionQueue[0].action == 'move')) {
  188. var sprintChance = this.stats.values.sprintChance || 0;
  189. if (~~(Math.random() * 100) < sprintChance) {
  190. q = this.dequeue();
  191. q.isDouble = true;
  192. }
  193. }
  194. var success = this.performMove(q);
  195. if (!success) {
  196. this.clearQueue();
  197. }
  198. } else if (q.action == 'clearQueue')
  199. this.clearQueue();
  200. else if (q.action == 'spell') {
  201. var success = this.spellbook.cast(q);
  202. if (!success)
  203. this.performQueue();
  204. }
  205. },
  206. performMove: function(action) {
  207. var data = action.data;
  208. var physics = this.instance.physics;
  209. if (!action.force) {
  210. if (physics.isTileBlocking(data.x, data.y))
  211. return false;
  212. data.success = true;
  213. this.fireEvent('beforeMove', data);
  214. if (data.success == false) {
  215. action.priority = true;
  216. this.queue(action);
  217. return true;
  218. }
  219. if (!action.isDouble) {
  220. var deltaX = Math.abs(this.x - data.x);
  221. var deltaY = Math.abs(this.y - data.y);
  222. if (
  223. ((deltaX > 1) || (deltaY > 1)) ||
  224. ((deltaX == 0) && (deltaY == 0))
  225. )
  226. return false;
  227. }
  228. }
  229. //Don't allow mob overlap during combat
  230. if ((this.mob) && (this.mob.target)) {
  231. if (physics.addObject(this, data.x, data.y)) {
  232. physics.removeObject(this, this.x, this.y);
  233. this.x = data.x;
  234. this.y = data.y;
  235. } else
  236. return false;
  237. } else {
  238. physics.removeObject(this, this.x, this.y, data.x, data.y);
  239. physics.addObject(this, data.x, data.y, this.x, this.y);
  240. this.x = data.x;
  241. this.y = data.y;
  242. }
  243. var syncer = this.syncer;
  244. syncer.o.x = this.x;
  245. syncer.o.y = this.y;
  246. if (this.aggro)
  247. this.aggro.move();
  248. this.fireEvent('afterMove');
  249. return true;
  250. },
  251. collisionEnter: function(obj) {
  252. var components = this.components;
  253. var cLen = components.length;
  254. for (var i = 0; i < cLen; i++) {
  255. var c = components[i];
  256. if (c.collisionEnter) {
  257. if (c.collisionEnter(obj))
  258. return true;
  259. }
  260. }
  261. },
  262. collisionExit: function(obj) {
  263. var components = this.components;
  264. var cLen = components.length;
  265. for (var i = 0; i < cLen; i++) {
  266. var c = components[i];
  267. if (c.collisionExit)
  268. c.collisionExit(obj);
  269. }
  270. },
  271. fireEvent: function(event) {
  272. var args = [].slice.call(arguments, 1);
  273. var components = this.components;
  274. var cLen = components.length;
  275. for (var i = 0; i < cLen; i++) {
  276. var cpn = components[i];
  277. var events = cpn.events;
  278. if (!events)
  279. continue;
  280. var callback = events[event];
  281. if (!callback)
  282. continue;
  283. callback.apply(cpn, args);
  284. }
  285. if (this.effects)
  286. this.effects.fireEvent(event, args);
  287. if (this.quests)
  288. this.quests.fireEvent(event, args);
  289. if (this.prophecies)
  290. this.prophecies.fireEvent(event, args);
  291. if (this.inventory)
  292. this.inventory.fireEvent(event, args);
  293. },
  294. destroy: function() {
  295. var components = this.components;
  296. var len = components.length;
  297. for (var i = 0; i < len; i++) {
  298. var c = components[i];
  299. if (c.destroy)
  300. c.destroy();
  301. }
  302. }
  303. };
  304. });