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.
 
 
 

123 lines
2.2 KiB

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