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.
 
 
 

115 lines
2.0 KiB

  1. define([
  2. 'js/rendering/renderer',
  3. 'js/system/events'
  4. ], function (
  5. renderer,
  6. events
  7. ) {
  8. let scale = 40;
  9. let scaleMult = 5;
  10. let round = Math.round.bind(Math);
  11. let maxPathLength = 50;
  12. return {
  13. type: 'pather',
  14. path: [],
  15. pathColor: '0x48edff',
  16. pathAlpha: 0.2,
  17. pathPos: {
  18. x: 0,
  19. y: 0
  20. },
  21. lastX: 0,
  22. lastY: 0,
  23. init: function () {
  24. events.on('onDeath', this.onDeath.bind(this));
  25. events.on('onClearQueue', this.onDeath.bind(this));
  26. this.pathPos.x = round(this.obj.x);
  27. this.pathPos.y = round(this.obj.y);
  28. },
  29. clearPath: function () {
  30. this.path.forEach(function (p) {
  31. renderer.destroyObject({
  32. layerName: 'effects',
  33. sprite: p.sprite
  34. });
  35. });
  36. this.path = [];
  37. },
  38. onDeath: function () {
  39. this.clearPath();
  40. this.pathPos.x = round(this.obj.x);
  41. this.pathPos.y = round(this.obj.y);
  42. },
  43. add: function (x, y) {
  44. if (this.path.length >= maxPathLength)
  45. return;
  46. this.path.push({
  47. x: x,
  48. y: y,
  49. sprite: renderer.buildRectangle({
  50. layerName: 'effects',
  51. color: this.pathColor,
  52. alpha: this.pathAlpha,
  53. x: (x * scale) + scaleMult,
  54. y: (y * scale) + scaleMult,
  55. w: scale - (scaleMult * 2),
  56. h: scale - (scaleMult * 2)
  57. })
  58. });
  59. return true;
  60. },
  61. update: function () {
  62. let x = this.obj.x;
  63. let y = this.obj.y;
  64. if (this.path.length == 0) {
  65. this.pathPos.x = round(x);
  66. this.pathPos.y = round(y);
  67. }
  68. if ((x == this.lastX) && (y == this.lastY))
  69. return;
  70. this.lastX = x;
  71. this.lastY = y;
  72. for (let i = 0; i < this.path.length; i++) {
  73. let p = this.path[i];
  74. if ((p.x == x) && (p.y == y)) {
  75. for (let j = 0; j <= i; j++) {
  76. renderer.destroyObject({
  77. layerName: 'effects',
  78. sprite: this.path[j].sprite
  79. });
  80. }
  81. this.path.splice(0, i + 1);
  82. return;
  83. }
  84. }
  85. },
  86. setPath: function (path) {
  87. this.path = this.path.concat(path);
  88. this.pathPos.x = round(path[path.length - 1].x);
  89. this.pathPos.y = round(path[path.length - 1].y);
  90. }
  91. };
  92. });