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.1 KiB

  1. define([
  2. 'js/rendering/renderer'
  3. ], function (
  4. renderer
  5. ) {
  6. return {
  7. type: 'moveAnimation',
  8. targetX: 0,
  9. targetY: 0,
  10. x: 0,
  11. y: 0,
  12. ttl: 50,
  13. endTime: 0,
  14. particles: null,
  15. particleBlueprint: null,
  16. particleExplosionBlueprint: null,
  17. init: function (blueprint) {
  18. const particleBlueprint = $.extend({
  19. scale: {
  20. start: {
  21. min: 6,
  22. max: 16
  23. },
  24. end: {
  25. min: 0,
  26. max: 10
  27. }
  28. },
  29. opacity: {
  30. start: 0.05,
  31. end: 0
  32. },
  33. lifetime: {
  34. min: 1,
  35. max: 2
  36. },
  37. speed: {
  38. start: {
  39. min: 2,
  40. max: 20
  41. },
  42. end: {
  43. min: 0,
  44. max: 8
  45. }
  46. },
  47. color: {
  48. start: 'fcfcfc',
  49. end: 'c0c3cf'
  50. },
  51. randomScale: true,
  52. randomSpeed: true,
  53. chance: 0.4
  54. }, this.particleBlueprint);
  55. this.particles = this.obj.addComponent('particles', { blueprint: particleBlueprint });
  56. this.endTime = +new Date() + this.ttl;
  57. let obj = this.obj;
  58. this.x = obj.x;
  59. this.y = obj.y;
  60. if (this.targetX > this.x)
  61. this.obj.flipX = false;
  62. else if (this.targetX < this.x)
  63. this.obj.flipX = true;
  64. this.obj.setSpritePosition();
  65. },
  66. update: function () {
  67. let source = this.obj;
  68. let target = this.target;
  69. let dx = this.targetX - this.x;
  70. let dy = this.targetY - this.y;
  71. let ticksLeft = ~~((this.endTime - (+new Date())) / 16);
  72. if (ticksLeft <= 0) {
  73. source.x = this.targetX;
  74. source.y = this.targetY;
  75. source.setSpritePosition();
  76. this.destroyed = true;
  77. this.particles.destroyed = true;
  78. //Sometimes we just move to a point without exploding
  79. if (target) {
  80. const particleExplosionBlueprint = this.particleExplosionBlueprint || {};
  81. target.addComponent('explosion', {
  82. new: true,
  83. blueprint: particleExplosionBlueprint
  84. }).explode();
  85. }
  86. } else {
  87. dx /= ticksLeft;
  88. dy /= ticksLeft;
  89. this.x += dx;
  90. this.y += dy;
  91. source.x = (~~((this.x * 32) / 8) * 8) / 32;
  92. source.y = (~~((this.y * 32) / 8) * 8) / 32;
  93. source.setSpritePosition();
  94. }
  95. renderer.updateSprites();
  96. }
  97. };
  98. });