thinSprite.ts 4.7 KB

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