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.
 
 
 

173 lines
4.7 KiB

  1. //Imports
  2. const objects = require('../../../objects/objects');
  3. const spawners = require('./../../spawners');
  4. const resourceSpawner = require('../../resourceSpawner');
  5. const mapObjects = require('../mapObjects');
  6. const getObjectifiedProperties = require('../getObjectifiedProperties');
  7. //Helpers
  8. const buildRoom = (mapModule, blueprint) => {
  9. if (blueprint.properties.exit) {
  10. const room = mapModule.rooms.find(r => {
  11. return (!(
  12. (blueprint.x + blueprint.width < r.x) ||
  13. (blueprint.y + blueprint.height < r.y) ||
  14. (blueprint.x >= r.x + r.width) ||
  15. (blueprint.y >= r.y + r.height)
  16. ));
  17. });
  18. room.exits.push(blueprint);
  19. } else if (blueprint.properties.resource)
  20. resourceSpawner.register(blueprint.properties.resource, blueprint);
  21. else {
  22. blueprint.exits = [];
  23. blueprint.objects = [];
  24. mapModule.rooms.push(blueprint);
  25. }
  26. };
  27. const buildHiddenRoom = (mapModule, blueprint) => {
  28. const { mapFile } = mapModule;
  29. const { properties } = blueprint;
  30. blueprint.fog = (properties || {}).fog;
  31. blueprint.interior = (properties || {}).interior;
  32. blueprint.discoverable = (properties || {}).discoverable;
  33. blueprint.layer = ~~((properties || {}).layer || 0);
  34. if (!mapFile.properties.isRandom)
  35. mapModule.hiddenRooms.push(blueprint);
  36. else {
  37. const room = mapModule.rooms.find(r => {
  38. return !(
  39. blueprint.x < r.x ||
  40. blueprint.y < r.y ||
  41. blueprint.x >= r.x + r.width ||
  42. blueprint.y >= r.y + r.height
  43. );
  44. });
  45. room.objects.push(blueprint);
  46. }
  47. };
  48. const buildRegularObject = (mapModule, blueprint, mapObj) => {
  49. const { mapScale, mapFile } = mapModule;
  50. if (!mapFile.properties.isRandom) {
  51. blueprint.y -= (mapObj.height / mapScale);
  52. spawners.register(blueprint, blueprint.spawnCd || mapFile.properties.spawnCd);
  53. } else {
  54. const room = mapModule.rooms.find(r => {
  55. return !(
  56. blueprint.x < r.x ||
  57. blueprint.y < r.y ||
  58. blueprint.x >= r.x + r.width ||
  59. blueprint.y >= r.y + r.height
  60. );
  61. });
  62. room.objects.push(blueprint);
  63. }
  64. };
  65. const buildClientObject = (mapModule, blueprint) => {
  66. const { mapScale } = mapModule;
  67. const { width, height, polyline } = blueprint;
  68. if (width && !polyline) {
  69. blueprint.width = width / mapScale;
  70. blueprint.height = height / mapScale;
  71. }
  72. const obj = objects.buildObjects([blueprint], true).getSimple(true);
  73. mapModule.objBlueprints.push(obj);
  74. };
  75. //Builder
  76. const buildObject = (mapModule, layerName, mapObj) => {
  77. const { mapScale } = mapModule;
  78. const { gid, x, y, width, height, sheetName, cell, polyline, polygon, properties } = mapObj;
  79. const clientObj = (layerName === 'clientObjects');
  80. const cellInfo = mapModule.getCellInfo(gid, x, y, layerName);
  81. let name = (mapObj.name || '');
  82. let objZoneName = name;
  83. if (name.indexOf('|') > -1) {
  84. const split = name.split('|');
  85. name = split[0];
  86. objZoneName = split[1];
  87. }
  88. const blueprint = {
  89. clientObj: clientObj,
  90. sheetName: sheetName !== undefined ? sheetName : cellInfo.sheetName,
  91. cell: cell !== undefined ? cell : cellInfo.cell - 1,
  92. x: x / mapScale,
  93. y: y / mapScale,
  94. name: name,
  95. layerName: layerName,
  96. properties: getObjectifiedProperties(properties),
  97. polyline: polyline ?? polygon
  98. };
  99. blueprint.id = blueprint.properties.id;
  100. if (objZoneName !== name)
  101. blueprint.objZoneName = objZoneName;
  102. if (mapModule.zone) {
  103. if ((mapModule.zone.objects) && (mapModule.zone.objects[objZoneName.toLowerCase()]))
  104. extend(blueprint, mapModule.zone.objects[objZoneName.toLowerCase()]);
  105. else if ((mapModule.zone.objects) && (mapModule.zone.mobs[objZoneName.toLowerCase()]))
  106. extend(blueprint, mapModule.zone.mobs[objZoneName.toLowerCase()]);
  107. }
  108. if (blueprint.sheetName && blueprint.cell !== undefined) {
  109. blueprint.properties.cpnSprite = {
  110. cell: blueprint.cell,
  111. sheetName: blueprint.sheetName
  112. };
  113. blueprint.properties.cpnTextSprite = {
  114. text: blueprint.name
  115. };
  116. delete blueprint.sheetName;
  117. delete blueprint.cell;
  118. blueprint.properties.cpnTransform = {
  119. width: mapObj.width,
  120. height: mapObj.height
  121. };
  122. }
  123. if (blueprint.blocking)
  124. mapModule.collisionMap[blueprint.x][blueprint.y] = 1;
  125. if (blueprint.properties.cpnNotice || blueprint.properties.cpnLightPatch || layerName === 'rooms' || layerName === 'hiddenRooms') {
  126. blueprint.width = width / mapScale;
  127. blueprint.height = height / mapScale;
  128. }
  129. if (blueprint.polyline)
  130. mapObjects.polyline(mapModule.size, blueprint, mapObj, mapScale);
  131. if (layerName === 'rooms')
  132. buildRoom(mapModule, blueprint, mapObj);
  133. else if (layerName === 'hiddenRooms')
  134. buildHiddenRoom(mapModule, blueprint, mapObj);
  135. else if (!clientObj)
  136. buildRegularObject(mapModule, blueprint, mapObj);
  137. else
  138. buildClientObject(mapModule, blueprint, mapObj);
  139. };
  140. module.exports = buildObject;