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.
 
 
 

124 lines
3.0 KiB

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