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.
 
 
 

119 lines
2.1 KiB

  1. define([
  2. 'js/rendering/effects'
  3. ], function (
  4. effects
  5. ) {
  6. let scale = 40;
  7. return {
  8. type: 'projectile',
  9. source: null,
  10. target: null,
  11. row: null,
  12. col: null,
  13. x: 0,
  14. y: 0,
  15. ttl: 50,
  16. endTime: 0,
  17. particles: null,
  18. init: function (blueprint) {
  19. if ((!this.source) || (!this.target)) {
  20. this.obj.destroyed = true;
  21. return;
  22. }
  23. this.endTime = +new Date() + this.ttl;
  24. let source = this.source;
  25. this.x = source.x;
  26. this.y = source.y;
  27. if (blueprint.projectileOffset) {
  28. if ((source.sprite) && (source.sprite.scale.x < 0))
  29. blueprint.projectileOffset.x *= -1;
  30. this.x += (blueprint.projectileOffset.x || 0);
  31. this.y += (blueprint.projectileOffset.y || 0);
  32. }
  33. this.obj.x = this.x;
  34. this.obj.y = this.y;
  35. let particlesBlueprint = this.particles ? {
  36. blueprint: this.particles
  37. } : {
  38. blueprint: {
  39. color: {
  40. start: ['7a3ad3', '3fa7dd'],
  41. end: ['3fa7dd', '7a3ad3']
  42. },
  43. scale: {
  44. start: {
  45. min: 2,
  46. max: 14
  47. },
  48. end: {
  49. min: 0,
  50. max: 8
  51. }
  52. },
  53. lifetime: {
  54. min: 1,
  55. max: 3
  56. },
  57. alpha: {
  58. start: 0.7,
  59. end: 0
  60. },
  61. randomScale: true,
  62. randomColor: true,
  63. chance: 0.6
  64. }
  65. };
  66. this.particles = this.obj.addComponent('particles', particlesBlueprint);
  67. this.obj.addComponent('explosion', particlesBlueprint);
  68. effects.register(this);
  69. },
  70. renderManual: function () {
  71. let source = this.obj;
  72. let target = this.target;
  73. let dx = target.x - this.x;
  74. let dy = target.y - this.y;
  75. let ticksLeft = ~~((this.endTime - (+new Date())) / 16);
  76. if (ticksLeft <= 0) {
  77. this.obj.x = target.x;
  78. this.obj.y = target.y;
  79. this.particles.emitter.emit = false;
  80. if (!this.noExplosion)
  81. this.obj.explosion.explode();
  82. this.obj.destroyed = true;
  83. } else {
  84. dx /= ticksLeft;
  85. dy /= ticksLeft;
  86. this.x += dx;
  87. this.y += dy;
  88. this.obj.x = (~~((this.x * scale) / 4) * 4) / scale;
  89. this.obj.y = (~~((this.y * scale) / 4) * 4) / scale;
  90. }
  91. },
  92. destroy: function () {
  93. effects.unregister(this);
  94. }
  95. };
  96. });