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.
 
 
 

118 linhas
2.3 KiB

  1. //Imports
  2. const jimp = require('jimp');
  3. //System
  4. let mapName = null;
  5. let stage = null;
  6. let noFlipTiles = null;
  7. let tileOpacities = null;
  8. //Methods
  9. const init = _mapName => {
  10. mapName = _mapName;
  11. stage = [];
  12. noFlipTiles = [];
  13. tileOpacities = [];
  14. };
  15. const track = tileInfo => {
  16. const { cell, sheetName } = tileInfo;
  17. if (!sheetName || cell === undefined)
  18. return cell;
  19. const existIndex = stage.findIndex(s => s.cell === cell && s.sheetName === sheetName);
  20. if (existIndex !== -1)
  21. return existIndex + 1;
  22. stage.push({
  23. cell,
  24. sheetName
  25. });
  26. const { config: { tilesNoFlip: noFlip, tileOpacities: opacities } } = clientConfig;
  27. if (noFlip?.[sheetName]?.includes(cell - 1))
  28. noFlipTiles.push(stage.length);
  29. tileOpacities.push({
  30. max: opacities?.[sheetName]?.max ?? opacities.default.max,
  31. opacity: opacities?.[sheetName]?.[cell - 1] ?? opacities?.[sheetName]?.default ?? opacities.default.default
  32. });
  33. return stage.length;
  34. };
  35. const getPath = () => {
  36. const path = `images/temp/${mapName}.png`;
  37. return path;
  38. };
  39. const finalize = async () => {
  40. const pathMaps = {};
  41. const paths = [];
  42. stage.forEach(s => {
  43. const sheetName = s.sheetName;
  44. const path = sheetName.includes('.png') ? `../${sheetName}` : `../client/images/${sheetName}.png`;
  45. if (paths.includes(path))
  46. return;
  47. pathMaps[sheetName] = path;
  48. paths.push(path);
  49. });
  50. const loaders = await Promise.all(paths.map(p => jimp.read(p)));
  51. //Load images
  52. const images = await Promise.all(loaders);
  53. const w = 8 * 8;
  54. const h = Math.ceil(stage.length / 8) * 8;
  55. //Create new
  56. const res = new jimp(w, h, async (err, image) => {
  57. stage.forEach(({ sheetName, cell }, i) => {
  58. const mappedPath = pathMaps[sheetName];
  59. const imageIndex = paths.findIndex(p => p === mappedPath);
  60. const sourceImage = images[imageIndex];
  61. const y = ~~(i / 8);
  62. const x = i - (y * 8);
  63. const sy = ~~((cell - 1) / 8);
  64. const sx = cell - 1 - (sy * 8);
  65. image.blit(sourceImage, x * 8, y * 8, sx * 8, sy * 8, 8, 8);
  66. });
  67. const path = `../client/${getPath()}`;
  68. image.writeAsync(path);
  69. });
  70. return res;
  71. };
  72. const getNoFlipTiles = () => {
  73. return noFlipTiles;
  74. };
  75. const getTileOpacities = () => {
  76. return tileOpacities;
  77. };
  78. //Exports
  79. module.exports = {
  80. init,
  81. track,
  82. getPath,
  83. getNoFlipTiles,
  84. getTileOpacities,
  85. finalize
  86. };