pathCursor.ts 2.8 KB

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