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.
 
 
 

118 righe
2.9 KiB

  1. define([
  2. 'js/rendering/renderer'
  3. ], function (
  4. renderer
  5. ) {
  6. return {
  7. build: function (config) {
  8. let obj = {
  9. lines: []
  10. };
  11. let maxDeviate = config.maxDeviate || (scale * 0.3);
  12. let fx = config.fromX * scale;
  13. let fy = config.fromY * scale;
  14. let tx = config.toX * scale;
  15. let ty = config.toY * scale;
  16. let angle = Math.atan2(ty - fy, tx - fx);
  17. let distance = Math.sqrt(Math.pow(tx - fx, 2) + Math.pow(ty - fy, 2));
  18. let divDistance = Math.min(20, distance);
  19. let divisions = config.divisions || Math.max(1, distance / divDistance);
  20. let x = fx;
  21. let y = fy;
  22. for (let i = 0; i < divisions; i++) {
  23. let line = {
  24. sprites: []
  25. };
  26. let ntx = fx + (Math.cos(angle) * (divDistance * i)) + ~~(Math.random() * (maxDeviate * 2)) - maxDeviate;
  27. let nty = fy + (Math.sin(angle) * (divDistance * i)) + ~~(Math.random() * (maxDeviate * 2)) - maxDeviate;
  28. if (i === divisions - 1) {
  29. ntx = tx;
  30. nty = ty;
  31. }
  32. let nAngle = Math.atan2(nty - y, ntx - x);
  33. let steps = ~~(Math.sqrt(Math.pow(ntx - x, 2) + Math.pow(nty - y, 2)) / scaleMult);
  34. let patches = {};
  35. for (let j = 0; j < steps; j++) {
  36. let alpha = 1;
  37. if ((config.colors) && (i === divisions - 1) && (j > (steps * 0.75)))
  38. alpha = 1 - (j / steps);
  39. let c = (config.colors || [0xffeb38, 0xfaac45, 0xfafcfc])[~~(Math.random() * (config.colors ? config.colors.length : 3))];
  40. line.sprites.push(renderer.buildRectangle({
  41. x: ~~(x / scaleMult) * scaleMult,
  42. y: ~~(y / scaleMult) * scaleMult,
  43. w: scaleMult,
  44. h: scaleMult,
  45. alpha: alpha,
  46. color: c,
  47. layerName: 'effects'
  48. }));
  49. let xx = x;
  50. let yy = y;
  51. if ((!patches[xx + '-' + yy]) && (!config.colors)) {
  52. patches[xx + '-' + yy] = 1;
  53. let lightPatch = renderer.buildObject({
  54. sheetName: 'white',
  55. x: 0,
  56. y: 0,
  57. cell: 0,
  58. layerName: 'lightPatches'
  59. });
  60. lightPatch.alpha = Math.random() * 0.5;
  61. lightPatch.tint = '0xffffff';
  62. lightPatch.x = ~~((xx - scaleMult) / scaleMult) * scaleMult;
  63. lightPatch.y = ~~((yy - scaleMult) / scaleMult) * scaleMult;
  64. lightPatch.width = scaleMult * 3;
  65. lightPatch.height = scaleMult * 3;
  66. lightPatch.blendMode = PIXI.BLEND_MODES.ADD;
  67. line.sprites.push(lightPatch);
  68. }
  69. x += Math.cos(nAngle) * scaleMult;
  70. y += Math.sin(nAngle) * scaleMult;
  71. }
  72. obj.lines.push(line);
  73. }
  74. return obj;
  75. },
  76. toHex: function rgbToHex (r, g, b) {
  77. let componentToHex = function (c) {
  78. let hex = c.toString(16);
  79. return hex.length === 1 ? '0' + hex : hex;
  80. };
  81. return '0x' + componentToHex(r) + componentToHex(g) + componentToHex(b);
  82. },
  83. update: function (obj) {
  84. },
  85. destroy: function (obj) {
  86. obj.lines.forEach(function (l) {
  87. l.sprites.forEach(function (s) {
  88. s.parent.removeChild(s);
  89. });
  90. });
  91. }
  92. };
  93. });