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.
 
 
 

134 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 cdMax = this.cdMax;
  37. if (((this.lineGrow) && (this.linePercentage < 1)) || ((this.shrinking) && (this.linePercentage > 0)))
  38. cdMax = 1;
  39. if (this.cd > 0) {
  40. this.cd--;
  41. return;
  42. }
  43. this.cd = cdMax;
  44. lightningBuilder.destroy(this.effect);
  45. this.effect = null;
  46. if (!this.shrinking) {
  47. this.ttl--;
  48. if (this.ttl == 0) {
  49. this.destroyed = true;
  50. return;
  51. }
  52. }
  53. let xOffset = (this.toX >= this.obj.x) ? 1 : 0;
  54. let fromX = this.obj.x + xOffset;
  55. let fromY = this.obj.y + 0.5;
  56. let toX = this.toX + 0.5;
  57. let toY = this.toY + 0.5;
  58. let changeTo = (
  59. (
  60. (this.lineGrow) &&
  61. (this.linePercentage < 1)
  62. ) ||
  63. (
  64. (this.shrinking) &&
  65. (this.linePercentage > 0)
  66. )
  67. );
  68. if (changeTo) {
  69. var linePercentage = this.linePercentage;
  70. if (this.shrinking)
  71. linePercentage /= 1.5;
  72. else {
  73. linePercentage *= 1.5;
  74. if (linePercentage > 1)
  75. linePercentage = 1;
  76. }
  77. this.linePercentage = linePercentage;
  78. let angle = Math.atan2(toY - fromY, toX - fromX);
  79. let distance = Math.sqrt(Math.pow(fromX - toX, 2) + Math.pow(fromY - toY, 2));
  80. toX = fromX + (Math.cos(angle) * distance * this.linePercentage);
  81. toY = fromY + (Math.sin(angle) * distance * this.linePercentage);
  82. }
  83. this.effect = lightningBuilder.build({
  84. fromX: fromX,
  85. fromY: fromY,
  86. toX: toX,
  87. toY: toY,
  88. divisions: this.divisions,
  89. colors: this.colors,
  90. maxDeviate: this.maxDeviate
  91. });
  92. if ((this.shrinking) && (linePercentage < 0.1))
  93. this.destroyed = true;
  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. });