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.
 
 
 

136 lines
2.6 KiB

  1. define([
  2. 'js/rendering/lightningBuilder',
  3. 'js/rendering/effects'
  4. ], function (
  5. lightningBuilder,
  6. effects
  7. ) {
  8. return {
  9. type: 'lightningEffect',
  10. cd: 0,
  11. cdMax: 1,
  12. effect: null,
  13. ttl: 6,
  14. lineGrow: false,
  15. linePercentage: 0.1,
  16. lineShrink: false,
  17. shrinking: false,
  18. init: function () {
  19. effects.register(this);
  20. let xOffset = (this.toX >= this.obj.x) ? 1 : 0;
  21. let fromX = this.obj.x + xOffset;
  22. let fromY = this.obj.y + 0.5;
  23. let toX = this.lineGrow ? fromX : this.toX + 0.5;
  24. let toY = this.lineGrow ? fromY : this.toY + 0.5;
  25. this.effect = lightningBuilder.build({
  26. fromX: fromX,
  27. fromY: fromY,
  28. toX: toX,
  29. toY: toY,
  30. divisions: this.divisions,
  31. colors: this.colors,
  32. maxDeviate: this.maxDeviate
  33. });
  34. },
  35. renderManual: function () {
  36. let linePercentage = this.linePercentage;
  37. let cdMax = this.cdMax;
  38. if (((this.lineGrow) && (linePercentage < 1)) || ((this.shrinking) && (linePercentage > 0)))
  39. cdMax = 1;
  40. if (this.cd > 0) {
  41. this.cd--;
  42. return;
  43. }
  44. this.cd = cdMax;
  45. lightningBuilder.destroy(this.effect);
  46. this.effect = null;
  47. if (!this.shrinking) {
  48. this.ttl--;
  49. if (this.ttl === 0) {
  50. this.destroyed = true;
  51. return;
  52. }
  53. }
  54. let xOffset = (this.toX >= this.obj.x) ? 1 : 0;
  55. let fromX = this.obj.x + xOffset;
  56. let fromY = this.obj.y + 0.5;
  57. let toX = this.toX + 0.5;
  58. let toY = this.toY + 0.5;
  59. let changeTo = (
  60. (
  61. (this.lineGrow) &&
  62. (linePercentage < 1)
  63. ) ||
  64. (
  65. (this.shrinking) &&
  66. (linePercentage > 0)
  67. )
  68. );
  69. if (changeTo) {
  70. if (this.shrinking)
  71. linePercentage /= 1.5;
  72. else {
  73. linePercentage *= 1.5;
  74. if (linePercentage > 1)
  75. linePercentage = 1;
  76. }
  77. let angle = Math.atan2(toY - fromY, toX - fromX);
  78. let distance = Math.sqrt(Math.pow(fromX - toX, 2) + Math.pow(fromY - toY, 2));
  79. toX = fromX + (Math.cos(angle) * distance * linePercentage);
  80. toY = fromY + (Math.sin(angle) * distance * linePercentage);
  81. }
  82. this.effect = lightningBuilder.build({
  83. fromX: fromX,
  84. fromY: fromY,
  85. toX: toX,
  86. toY: toY,
  87. divisions: this.divisions,
  88. colors: this.colors,
  89. maxDeviate: this.maxDeviate
  90. });
  91. if ((this.shrinking) && (linePercentage < 0.1))
  92. this.destroyed = true;
  93. this.linePercentage = linePercentage;
  94. },
  95. destroyManual: function () {
  96. if ((!this.lineShrink) || (this.shrinking)) {
  97. if (this.effect)
  98. lightningBuilder.destroy(this.effect);
  99. //effects.unregister(this);
  100. return;
  101. }
  102. this.destroyed = false;
  103. this.shrinking = true;
  104. return true;
  105. }
  106. };
  107. });