pathCursor.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Path2, Vector3 } from '../Maths/math';
  2. /**
  3. * A cursor which tracks a point on a path
  4. */
  5. export class PathCursor {
  6. /**
  7. * Stores path cursor callbacks for when an onchange event is triggered
  8. */
  9. private _onchange = new Array<(cursor: PathCursor) => void>();
  10. /**
  11. * The value of the path cursor
  12. */
  13. value: number = 0;
  14. /**
  15. * The animation array of the path cursor
  16. */
  17. animations = new Array<Animation>();
  18. /**
  19. * Initializes the path cursor
  20. * @param path The path to track
  21. */
  22. constructor(private path: Path2) {
  23. }
  24. /**
  25. * Gets the cursor point on the path
  26. * @returns A point on the path cursor at the cursor location
  27. */
  28. public getPoint(): Vector3 {
  29. var point = this.path.getPointAtLengthPosition(this.value);
  30. return new Vector3(point.x, 0, point.y);
  31. }
  32. /**
  33. * Moves the cursor ahead by the step amount
  34. * @param step The amount to move the cursor forward
  35. * @returns This path cursor
  36. */
  37. public moveAhead(step: number = 0.002): PathCursor {
  38. this.move(step);
  39. return this;
  40. }
  41. /**
  42. * Moves the cursor behind by the step amount
  43. * @param step The amount to move the cursor back
  44. * @returns This path cursor
  45. */
  46. public moveBack(step: number = 0.002): PathCursor {
  47. this.move(-step);
  48. return this;
  49. }
  50. /**
  51. * Moves the cursor by the step amount
  52. * If the step amount is greater than one, an exception is thrown
  53. * @param step The amount to move the cursor
  54. * @returns This path cursor
  55. */
  56. public move(step: number): PathCursor {
  57. if (Math.abs(step) > 1) {
  58. throw "step size should be less than 1.";
  59. }
  60. this.value += step;
  61. this.ensureLimits();
  62. this.raiseOnChange();
  63. return this;
  64. }
  65. /**
  66. * Ensures that the value is limited between zero and one
  67. * @returns This path cursor
  68. */
  69. private ensureLimits(): PathCursor {
  70. while (this.value > 1) {
  71. this.value -= 1;
  72. }
  73. while (this.value < 0) {
  74. this.value += 1;
  75. }
  76. return this;
  77. }
  78. /**
  79. * Runs onchange callbacks on change (used by the animation engine)
  80. * @returns This path cursor
  81. */
  82. private raiseOnChange(): PathCursor {
  83. this._onchange.forEach((f) => f(this));
  84. return this;
  85. }
  86. /**
  87. * Executes a function on change
  88. * @param f A path cursor onchange callback
  89. * @returns This path cursor
  90. */
  91. public onchange(f: (cursor: PathCursor) => void): PathCursor {
  92. this._onchange.push(f);
  93. return this;
  94. }
  95. }