thinSprite.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * ThinSprite Class used to represent a thin sprite
  3. * This is the base class for sprites but can also directly be used with ThinEngine
  4. * @see https://doc.babylonjs.com/features/featuresDeepDive/sprites
  5. */
  6. export class ThinSprite {
  7. /**
  8. * Returns a boolean indicating if the animation is started
  9. */
  10. get animationStarted() {
  11. return this._animationStarted;
  12. }
  13. /** Gets the initial key for the animation (setting it will restart the animation) */
  14. get fromIndex() {
  15. return this._fromIndex;
  16. }
  17. /** Gets or sets the end key for the animation (setting it will restart the animation) */
  18. get toIndex() {
  19. return this._toIndex;
  20. }
  21. /** Gets or sets a boolean indicating if the animation is looping (setting it will restart the animation) */
  22. get loopAnimation() {
  23. return this._loopAnimation;
  24. }
  25. /** Gets or sets the delay between cell changes (setting it will restart the animation) */
  26. get delay() {
  27. return Math.max(this._delay, 1);
  28. }
  29. /**
  30. * Creates a new Thin Sprite
  31. */
  32. constructor() {
  33. /** Gets or sets the width */
  34. this.width = 1.0;
  35. /** Gets or sets the height */
  36. this.height = 1.0;
  37. /** Gets or sets rotation angle */
  38. this.angle = 0;
  39. /** Gets or sets a boolean indicating if UV coordinates should be inverted in U axis */
  40. this.invertU = false;
  41. /** Gets or sets a boolean indicating if UV coordinates should be inverted in B axis */
  42. this.invertV = false;
  43. /** Gets or sets a boolean indicating if the sprite is visible (renderable). Default is true */
  44. this.isVisible = true;
  45. this._animationStarted = false;
  46. this._loopAnimation = false;
  47. this._fromIndex = 0;
  48. this._toIndex = 0;
  49. this._delay = 0;
  50. this._direction = 1;
  51. this._time = 0;
  52. this._onBaseAnimationEnd = null;
  53. this.position = { x: 1.0, y: 1.0, z: 1.0 };
  54. this.color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  55. }
  56. /**
  57. * Starts an animation
  58. * @param from defines the initial key
  59. * @param to defines the end key
  60. * @param loop defines if the animation must loop
  61. * @param delay defines the start delay (in ms)
  62. * @param onAnimationEnd defines a callback for when the animation ends
  63. */
  64. playAnimation(from, to, loop, delay, onAnimationEnd) {
  65. this._fromIndex = from;
  66. this._toIndex = to;
  67. this._loopAnimation = loop;
  68. this._delay = delay || 1;
  69. this._animationStarted = true;
  70. this._onBaseAnimationEnd = onAnimationEnd;
  71. if (from < to) {
  72. this._direction = 1;
  73. }
  74. else {
  75. this._direction = -1;
  76. this._toIndex = from;
  77. this._fromIndex = to;
  78. }
  79. this.cellIndex = from;
  80. this._time = 0;
  81. }
  82. /** Stops current animation (if any) */
  83. stopAnimation() {
  84. this._animationStarted = false;
  85. }
  86. /**
  87. * @internal
  88. */
  89. _animate(deltaTime) {
  90. if (!this._animationStarted) {
  91. return;
  92. }
  93. this._time += deltaTime;
  94. if (this._time > this._delay) {
  95. this._time = this._time % this._delay;
  96. this.cellIndex += this._direction;
  97. if ((this._direction > 0 && this.cellIndex > this._toIndex) || (this._direction < 0 && this.cellIndex < this._fromIndex)) {
  98. if (this._loopAnimation) {
  99. this.cellIndex = this._direction > 0 ? this._fromIndex : this._toIndex;
  100. }
  101. else {
  102. this.cellIndex = this._toIndex;
  103. this._animationStarted = false;
  104. if (this._onBaseAnimationEnd) {
  105. this._onBaseAnimationEnd();
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. //# sourceMappingURL=thinSprite.js.map