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.
 
 
 

298 lines
5.8 KiB

  1. define([
  2. '../objects/objBase',
  3. '../leaderboard/leaderboard'
  4. ], function(
  5. objBase,
  6. leaderboard
  7. ) {
  8. return {
  9. nextId: 0,
  10. objects: [],
  11. init: function(_instance) {
  12. this.instance = _instance;
  13. this.physics = this.instance.physics;
  14. },
  15. getNextId: function() {
  16. return this.nextId++;
  17. },
  18. build: function(skipPush, clientObj) {
  19. var o = extend(true, {}, objBase);
  20. if (clientObj)
  21. o.update = null;
  22. else {
  23. o.id = this.nextId++;
  24. o.addComponent('syncer');
  25. o.instance = this.instance;
  26. if (!skipPush)
  27. this.objects.push(o);
  28. }
  29. return o;
  30. },
  31. transferObject: function(o) {
  32. var obj = this.build();
  33. var components = o.components;
  34. delete o.components;
  35. delete o.id;
  36. for (var p in o) {
  37. obj[p] = o[p];
  38. }
  39. var cLen = components.length;
  40. for (var i = 0; i < cLen; i++) {
  41. var c = components[i];
  42. var cpn = obj.addComponent(c.type, null, true);
  43. for (var p in c) {
  44. cpn[p] = c[p];
  45. }
  46. if (cpn.transfer)
  47. cpn.transfer();
  48. }
  49. return obj;
  50. },
  51. buildObjects: function(list, skipPush) {
  52. var lLen = list.length;
  53. for (var i = 0; i < lLen; i++) {
  54. var l = list[i];
  55. var obj = this.build(skipPush, l.clientObj);
  56. obj.sheetName = l.sheetName;
  57. obj.cell = l.cell;
  58. obj.name = l.name;
  59. obj.x = l.x;
  60. obj.y = l.y;
  61. if (l.width) {
  62. obj.width = l.width;
  63. obj.height = l.height;
  64. }
  65. //Add components (certain ones need to happen first)
  66. //TODO: Clean this part up
  67. var properties = extend(true, {}, l.properties);
  68. ['cpnMob'].forEach(function(c) {
  69. var blueprint = properties[c] || null;
  70. if ((blueprint) && (typeof(blueprint) == 'string'))
  71. blueprint = JSON.parse(blueprint);
  72. if (!blueprint)
  73. return;
  74. delete properties[c];
  75. var type = c.replace('cpn', '').toLowerCase();
  76. obj.addComponent(type, blueprint);
  77. }, this);
  78. for (var p in properties) {
  79. if (p.indexOf('cpn') == -1) {
  80. obj[p] = properties[p];
  81. continue;
  82. }
  83. var type = p.replace('cpn', '').toLowerCase();
  84. var blueprint = properties[p] || null;
  85. if ((blueprint) && (typeof(blueprint) == 'string'))
  86. blueprint = JSON.parse(blueprint);
  87. obj.addComponent(type, blueprint);
  88. }
  89. var extraProperties = l.extraProperties || {};
  90. for (var p in extraProperties) {
  91. var cpn = obj[p];
  92. var e = extraProperties[p];
  93. for (var pp in e) {
  94. cpn[pp] = e[pp];
  95. }
  96. if (cpn.init)
  97. cpn.init();
  98. }
  99. if (this.physics) {
  100. if (!obj.width)
  101. this.physics.addObject(obj, obj.x, obj.y);
  102. else
  103. this.physics.addRegion(obj);
  104. }
  105. if (lLen == 1)
  106. return obj;
  107. }
  108. },
  109. find: function(callback) {
  110. return this.objects.find(callback);
  111. },
  112. removeObject: function(obj, callback, useServerId) {
  113. var objects = this.objects;
  114. var oLen = objects.length
  115. var found = null;
  116. for (var i = 0; i < oLen; i++) {
  117. var o = objects[i];
  118. var match = false;
  119. if (useServerId)
  120. match = (o.serverId == obj.id)
  121. else
  122. match = (o.id == obj.id);
  123. if (match) {
  124. o.destroy();
  125. found = o;
  126. objects.splice(i, 1);
  127. break;
  128. }
  129. }
  130. if (this.physics)
  131. this.physics.removeObject(found, found.x, found.y);
  132. callback && callback(found);
  133. },
  134. addObject: function(o, callback) {
  135. var newO = this.build(true);
  136. var components = o.components;
  137. delete o.components;
  138. for (var p in o) {
  139. newO[p] = o[p];
  140. }
  141. var len = components.length;
  142. for (var i = 0; i < len; i++) {
  143. var c = components[i];
  144. newO.addComponent(c.type, c);
  145. var newC = newO[c.type];
  146. for (var p in c) {
  147. newC[p] = c[p];
  148. }
  149. }
  150. this.objects.push(newO);
  151. this.physics.addObject(newO, newO.x, newO.y);
  152. callback(newO);
  153. return newO;
  154. },
  155. sendEvent: function(msg) {
  156. var player = this.objects.find(p => p.id == msg.id);
  157. if (!player)
  158. return;
  159. player.socket.emit('event', {
  160. event: msg.data.event,
  161. data: msg.data.data
  162. });
  163. },
  164. sendEvents: function(msg) {
  165. var players = {};
  166. var objects = this.objects;
  167. var data = msg.data;
  168. for (var e in data) {
  169. var event = data[e];
  170. var eLen = event.length;
  171. for (var j = 0; j < eLen; j++) {
  172. var eventEntry = event[j];
  173. var obj = eventEntry.obj;
  174. if (e != 'serverModule') {
  175. var to = eventEntry.to;
  176. var toLen = to.length;
  177. for (var i = 0; i < toLen; i++) {
  178. var toId = to[i];
  179. var player = players[toId];
  180. if (!player) {
  181. var findPlayer = objects.find(o => o.id == toId);
  182. if (!findPlayer)
  183. continue;
  184. else
  185. player = (players[toId] = {
  186. socket: findPlayer.socket,
  187. events: {}
  188. });
  189. }
  190. var eventList = player.events[e] || (player.events[e] = []);
  191. eventList.push(obj);
  192. }
  193. }
  194. else
  195. global[obj.module][obj.method](obj.msg);
  196. }
  197. }
  198. for (var p in players) {
  199. var player = players[p];
  200. player.socket.emit('events', player.events);
  201. }
  202. },
  203. updateObject: function(msg) {
  204. var player = this.objects.find(p => p.id == msg.serverId);
  205. if (!player)
  206. return;
  207. var obj = msg.obj;
  208. for (var p in obj) {
  209. player[p] = obj[p];
  210. }
  211. if (obj.dead)
  212. leaderboard.killCharacter(player.name);
  213. if (obj.level) {
  214. leaderboard.setLevel(player.name, obj.level);
  215. io.sockets.emit('events', {
  216. onGetMessages: [{
  217. messages: [{
  218. class: 'q1',
  219. message: player.name + ' has reached level ' + obj.level
  220. }]
  221. }],
  222. onGetConnectedPlayer: [cons.getCharacterList()]
  223. });
  224. }
  225. },
  226. update: function() {
  227. var objects = this.objects;
  228. var len = objects.length;
  229. for (var i = 0; i < len; i++) {
  230. var o = objects[i];
  231. //Don't remove it from the list if it's destroyed, but don't update it either
  232. //That's syncer's job
  233. if ((o.update) && (!o.destroyed))
  234. o.update();
  235. }
  236. }
  237. };
  238. });