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.
 
 
 

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