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.
 
 
 

127 lines
2.2 KiB

  1. define([
  2. 'js/rendering/renderer',
  3. 'js/system/events',
  4. 'js/system/client'
  5. ], function (
  6. renderer,
  7. events,
  8. client
  9. ) {
  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('onRespawn', this.onDeath.bind(this));
  25. events.on('onDeath', this.onDeath.bind(this));
  26. events.on('onClearQueue', this.onDeath.bind(this));
  27. this.pathPos.x = round(this.obj.x);
  28. this.pathPos.y = round(this.obj.y);
  29. },
  30. clearPath: function () {
  31. this.path.forEach(function (p) {
  32. renderer.destroyObject({
  33. layerName: 'effects',
  34. sprite: p.sprite
  35. });
  36. });
  37. this.path = [];
  38. },
  39. onDeath: function () {
  40. this.clearPath();
  41. this.pathPos.x = round(this.obj.x);
  42. this.pathPos.y = round(this.obj.y);
  43. },
  44. add: function (x, y) {
  45. if (this.path.length >= maxPathLength || this.obj.moveAnimation)
  46. return;
  47. this.pathPos.x = x;
  48. this.pathPos.y = y;
  49. this.path.push({
  50. x: x,
  51. y: y,
  52. sprite: renderer.buildRectangle({
  53. layerName: 'effects',
  54. color: this.pathColor,
  55. alpha: this.pathAlpha,
  56. x: (x * scale) + scaleMult,
  57. y: (y * scale) + scaleMult,
  58. w: scale - (scaleMult * 2),
  59. h: scale - (scaleMult * 2)
  60. })
  61. });
  62. client.request({
  63. cpn: 'player',
  64. method: 'move',
  65. priority: !this.path.length,
  66. data: {
  67. x: x,
  68. y: y
  69. }
  70. });
  71. },
  72. update: function () {
  73. if (this.obj.moveAnimation)
  74. this.clearPath();
  75. let x = this.obj.x;
  76. let y = this.obj.y;
  77. if (this.path.length === 0) {
  78. this.pathPos.x = round(x);
  79. this.pathPos.y = round(y);
  80. }
  81. if ((x === this.lastX) && (y === this.lastY))
  82. return;
  83. this.lastX = x;
  84. this.lastY = y;
  85. for (let i = 0; i < this.path.length; i++) {
  86. let p = this.path[i];
  87. if ((p.x === x) && (p.y === y)) {
  88. for (let j = 0; j <= i; j++) {
  89. renderer.destroyObject({
  90. layerName: 'effects',
  91. sprite: this.path[j].sprite
  92. });
  93. }
  94. this.path.splice(0, i + 1);
  95. return;
  96. }
  97. }
  98. },
  99. destroy: function () {
  100. this.unhookEvents();
  101. }
  102. };
  103. });