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.
 
 
 

151 linhas
2.9 KiB

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