Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

368 righe
8.6 KiB

  1. define([
  2. 'objects/objects',
  3. 'world/physics',
  4. 'world/spawners',
  5. 'world/resourceSpawner',
  6. 'config/zoneBase',
  7. 'world/randomMap'
  8. ], function(
  9. objects,
  10. physics,
  11. spawners,
  12. resourceSpawner,
  13. globalZone,
  14. randomMap
  15. ) {
  16. var mapFile = null;
  17. var mapscale = 38;
  18. var padding = null;
  19. var map = {
  20. name: null,
  21. layers: [],
  22. mapFile: null,
  23. size: {
  24. w: 0,
  25. h: 0
  26. },
  27. instanced: false,
  28. custom: null,
  29. collisionMap: null,
  30. clientMap: null,
  31. oldLayers: {
  32. tiles: null,
  33. walls: null,
  34. doodads: null
  35. },
  36. objBlueprints: [],
  37. spawn: {
  38. x: 0,
  39. y: 0
  40. },
  41. rooms: [],
  42. hiddenRooms: [],
  43. hiddenWalls: null,
  44. hiddenTiles: null,
  45. zone: null,
  46. init: function(args) {
  47. this.name = args.name;
  48. try {
  49. this.zone = require('../config/maps/' + this.name + '/zone');
  50. } catch (e) {
  51. this.zone = globalZone;
  52. }
  53. var chats = null;
  54. try {
  55. chats = require('../config/maps/' + this.name + '/chats');
  56. } catch (e) {}
  57. if (chats)
  58. this.zone.chats = chats;
  59. var dialogues = null;
  60. try {
  61. dialogues = require('../config/maps/' + this.name + '/dialogues');
  62. } catch (e) {}
  63. if (dialogues)
  64. this.zone.dialogues = dialogues;
  65. this.zone = extend(true, {}, globalZone, this.zone);
  66. var resources = this.zone.resources || {};
  67. for (var r in resources) {
  68. resourceSpawner.register(r, resources[r]);
  69. }
  70. mapFile = require('../config/maps/' + this.name + '/map');
  71. this.mapFile = mapFile;
  72. mapScale = mapFile.tilesets[0].tileheight;
  73. this.instanced = mapFile.properties.instanced;
  74. this.custom = mapFile.properties.custom;
  75. if (this.instanced)
  76. this.instanced = (this.instanced == '1');
  77. if (mapFile.properties.spawn)
  78. this.spawn = JSON.parse(mapFile.properties.spawn);
  79. },
  80. create: function() {
  81. this.getMapFile();
  82. this.clientMap = {
  83. zoneId: -1,
  84. map: this.layers,
  85. hiddenWalls: this.hiddenWalls,
  86. hiddenTiles: this.hiddenTiles,
  87. collisionMap: this.collisionMap,
  88. clientObjects: this.objBlueprints,
  89. padding: padding,
  90. hiddenRooms: this.hiddenRooms
  91. };
  92. },
  93. getMapFile: function() {
  94. this.build();
  95. randomMap = extend(true, {}, randomMap);
  96. this.oldMap = this.layers;
  97. randomMap.templates = extend(true, [], this.rooms);
  98. randomMap.generateMappings(this);
  99. for (var i = 0; i < this.size.w; i++) {
  100. var row = this.layers[i];
  101. for (var j = 0; j < this.size.h; j++) {
  102. var cell = row[j];
  103. if (!cell)
  104. continue;
  105. cell = cell.split(',');
  106. var cLen = cell.length;
  107. var newCell = '';
  108. for (var k = 0; k < cLen; k++) {
  109. var c = cell[k];
  110. var newC = randomMap.randomizeTile(c);
  111. newCell += newC;
  112. //Wall?
  113. if ((c >= 160) && (c <= 352) && (newC == 0)) {
  114. this.collisionMap[i][j] = 0;
  115. }
  116. if (k < cLen - 1)
  117. newCell += ',';
  118. }
  119. if (this.hiddenWalls[i][j])
  120. this.hiddenWalls[i][j] = randomMap.randomizeTile(this.hiddenWalls[i][j]);
  121. if (this.hiddenTiles[i][j])
  122. this.hiddenTiles[i][j] = randomMap.randomizeTile(this.hiddenTiles[i][j]);
  123. row[j] = newCell;
  124. }
  125. }
  126. randomMap.templates
  127. .filter(r => r.properties.mapping)
  128. .forEach(function(m) {
  129. var x = m.x;
  130. var y = m.y;
  131. var w = m.width;
  132. var h = m.height;
  133. for (var i = x; i < x + w; i++) {
  134. var row = this.layers[i];
  135. for (var j = y; j < y + h; j++) {
  136. row[j] = '';
  137. }
  138. }
  139. }, this);
  140. physics.init(this.collisionMap);
  141. padding = mapFile.properties.padding;
  142. mapFile = null;
  143. console.log('(M ' + this.name + '): Ready');
  144. },
  145. build: function() {
  146. this.size.w = mapFile.width;
  147. this.size.h = mapFile.height;
  148. this.layers = _.get2dArray(this.size.w, this.size.h, null);
  149. this.hiddenWalls = _.get2dArray(this.size.w, this.size.h, null);
  150. this.hiddenTiles = _.get2dArray(this.size.w, this.size.h, null);
  151. this.oldLayers.tiles = _.get2dArray(this.size.w, this.size.h, 0);
  152. this.oldLayers.walls = _.get2dArray(this.size.w, this.size.h, 0);
  153. this.oldLayers.objects = _.get2dArray(this.size.w, this.size.h, 0);
  154. var builders = {
  155. tile: this.builders.tile.bind(this),
  156. object: this.builders.object.bind(this)
  157. };
  158. this.collisionMap = _.get2dArray(this.size.w, this.size.h);
  159. //Rooms need to be ahead of exits
  160. mapFile.layers.rooms = (mapFile.layers.rooms || [])
  161. .sort(function(a, b) {
  162. if ((a.exit) && (!b.exit))
  163. return 1;
  164. else
  165. return 0;
  166. });
  167. for (var i = 0; i < mapFile.layers.length; i++) {
  168. var layer = mapFile.layers[i];
  169. var layerName = layer.name;
  170. if (!layer.visible)
  171. continue;
  172. var data = layer.data || layer.objects;
  173. var len = data.length;
  174. for (var j = 0; j < len; j++) {
  175. var cell = data[j];
  176. if ((cell.gid) || (cell.id))
  177. builders.object(layerName, cell, j);
  178. else
  179. builders.tile(layerName, cell, j);
  180. }
  181. }
  182. },
  183. builders: {
  184. getCellInfo: function(cell) {
  185. var flipX = null;
  186. if ((cell ^ 0x80000000) > 0) {
  187. flipX = true;
  188. cell = cell ^ 0x80000000;
  189. }
  190. var firstGid = 0;
  191. var sheetName = null;
  192. for (var s = 0; s < mapFile.tilesets.length; s++) {
  193. var tileset = mapFile.tilesets[s];
  194. if (tileset.firstgid <= cell) {
  195. sheetName = tileset.name;
  196. firstGid = tileset.firstgid;
  197. }
  198. }
  199. cell = cell - firstGid + 1;
  200. return {
  201. sheetName: sheetName,
  202. cell: cell,
  203. flipX: flipX
  204. };
  205. },
  206. tile: function(layerName, cell, i) {
  207. var y = ~~(i / this.size.w);
  208. var x = i - (y * this.size.w);
  209. if (cell == 0) {
  210. if (layerName == 'tiles')
  211. this.collisionMap[x][y] = 1;
  212. return;
  213. }
  214. var cellInfo = this.builders.getCellInfo(cell);
  215. var sheetName = cellInfo.sheetName;
  216. var cell = cellInfo.cell;
  217. if (sheetName == 'walls')
  218. cell += 192;
  219. else if (sheetName == 'objects')
  220. cell += 384;
  221. if ((layerName != 'hiddenWalls') && (layerName != 'hiddenTiles')) {
  222. var layer = this.layers;
  223. if (this.oldLayers[layerName])
  224. this.oldLayers[layerName][x][y] = cell;
  225. layer[x][y] = (layer[x][y] == null) ? cell : layer[x][y] + ',' + cell;
  226. }
  227. else if (layerName == 'hiddenWalls')
  228. this.hiddenWalls[x][y] = cell;
  229. else if (layerName == 'hiddenTiles')
  230. this.hiddenTiles[x][y] = cell;
  231. if (layerName.indexOf('walls') > -1)
  232. this.collisionMap[x][y] = 1;
  233. else if (sheetName.toLowerCase().indexOf('tiles') > -1) {
  234. if ((cell == 6) || (cell == 7) || (cell == 54) || (cell == 55) || (cell == 62) || (cell == 63) || (cell == 154))
  235. this.collisionMap[x][y] = 1;
  236. }
  237. },
  238. object: function(layerName, cell, i) {
  239. var clientObj = (layerName == 'clientObjects');
  240. var cellInfo = this.builders.getCellInfo(cell.gid);
  241. var name = (cell.name || '');
  242. var objZoneName = name;
  243. if (name.indexOf('|') > -1) {
  244. var split = name.split('|');
  245. name = split[0];
  246. objZoneName = split[1];
  247. }
  248. var blueprint = {
  249. clientObj: clientObj,
  250. sheetName: cellInfo.sheetName,
  251. cell: cellInfo.cell - 1,
  252. x: cell.x / mapScale,
  253. y: (cell.y / mapScale) - 1,
  254. name: name,
  255. properties: cell.properties || {}
  256. };
  257. if ((this.zone) && (this.zone.objects) && (this.zone.objects[objZoneName.toLowerCase()]))
  258. extend(true, blueprint, this.zone.objects[objZoneName.toLowerCase()]);
  259. if ((blueprint.properties.cpnNotice) || (layerName == 'rooms') || (layerName == 'hiddenRooms')) {
  260. blueprint.y++;
  261. blueprint.width = cell.width / mapScale;
  262. blueprint.height = cell.height / mapScale;
  263. } else if (cell.width == 24)
  264. blueprint.x++;
  265. if (layerName == 'rooms') {
  266. if (blueprint.properties.exit) {
  267. var room = this.rooms.find(function(r) {
  268. return (!(
  269. (blueprint.x + blueprint.width < r.x) ||
  270. (blueprint.y + blueprint.height < r.y) ||
  271. (blueprint.x >= r.x + r.width) ||
  272. (blueprint.y >= r.y + r.height)
  273. ));
  274. });
  275. room.exits.push(blueprint);
  276. } else {
  277. blueprint.exits = [];
  278. blueprint.objects = [];
  279. this.rooms.push(blueprint);
  280. }
  281. } else if (layerName == 'hiddenRooms') {
  282. this.hiddenRooms.push(blueprint);
  283. } else if (!clientObj) {
  284. if (!mapFile.properties.isRandom)
  285. spawners.register(blueprint, mapFile.properties.spawnCd);
  286. else {
  287. var room = this.rooms.find(function(r) {
  288. return (!(
  289. (blueprint.x < r.x) ||
  290. (blueprint.y < r.y) ||
  291. (blueprint.x >= r.x + r.width) ||
  292. (blueprint.y >= r.y + r.height)
  293. ));
  294. });
  295. room.objects.push(blueprint);
  296. }
  297. } else {
  298. var obj = objects.buildObjects([blueprint], true).getSimple(true);
  299. this.objBlueprints.push(obj);
  300. }
  301. }
  302. }
  303. }
  304. return map;
  305. });