sprite.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Vector3 } from "../Maths/math.vector";
  2. import { Nullable } from "../types";
  3. import { ActionManager } from "../Actions/actionManager";
  4. import { ISpriteManager } from "./spriteManager";
  5. import { Color4 } from '../Maths/math.color';
  6. import { Observable } from '../Misc/observable';
  7. /**
  8. * Class used to represent a sprite
  9. * @see http://doc.babylonjs.com/babylon101/sprites
  10. */
  11. export class Sprite {
  12. /** Gets or sets the current world position */
  13. public position: Vector3;
  14. /** Gets or sets the main color */
  15. public color = new Color4(1.0, 1.0, 1.0, 1.0);
  16. /** Gets or sets the width */
  17. public width = 1.0;
  18. /** Gets or sets the height */
  19. public height = 1.0;
  20. /** Gets or sets rotation angle */
  21. public angle = 0;
  22. /** Gets or sets the cell index in the sprite sheet */
  23. public cellIndex: number;
  24. /** Gets or sets the cell reference in the sprite sheet, uses sprite's filename when added to sprite sheet */
  25. public cellRef: string;
  26. /** Gets or sets a boolean indicating if UV coordinates should be inverted in U axis */
  27. public invertU = 0;
  28. /** Gets or sets a boolean indicating if UV coordinates should be inverted in B axis */
  29. public invertV = 0;
  30. /** Gets or sets a boolean indicating that this sprite should be disposed after animation ends */
  31. public disposeWhenFinishedAnimating: boolean;
  32. /** Gets the list of attached animations */
  33. public animations = new Array<Animation>();
  34. /** Gets or sets a boolean indicating if the sprite can be picked */
  35. public isPickable = false;
  36. /** Gets or sets a boolean indicating that sprite texture alpha will be used for precise picking (false by default) */
  37. public useAlphaForPicking = false;
  38. /** @hidden */
  39. public _xOffset: number;
  40. /** @hidden */
  41. public _yOffset: number;
  42. /** @hidden */
  43. public _xSize: number;
  44. /** @hidden */
  45. public _ySize: number;
  46. /**
  47. * Gets or sets the associated action manager
  48. */
  49. public actionManager: Nullable<ActionManager>;
  50. /**
  51. * An event triggered when the control has been disposed
  52. */
  53. public onDisposeObservable = new Observable<Sprite>();
  54. private _animationStarted = false;
  55. private _loopAnimation = false;
  56. private _fromIndex = 0;
  57. private _toIndex = 0;
  58. private _delay = 0;
  59. private _direction = 1;
  60. private _manager: ISpriteManager;
  61. private _time = 0;
  62. private _onAnimationEnd: () => void;
  63. /**
  64. * Gets or sets a boolean indicating if the sprite is visible (renderable). Default is true
  65. */
  66. public isVisible = true;
  67. /**
  68. * Gets or sets the sprite size
  69. */
  70. public get size(): number {
  71. return this.width;
  72. }
  73. public set size(value: number) {
  74. this.width = value;
  75. this.height = value;
  76. }
  77. /**
  78. * Gets or sets the unique id of the sprite
  79. */
  80. public uniqueId: number;
  81. /**
  82. * Creates a new Sprite
  83. * @param name defines the name
  84. * @param manager defines the manager
  85. */
  86. constructor(
  87. /** defines the name */
  88. public name: string,
  89. manager: ISpriteManager) {
  90. this._manager = manager;
  91. this._manager.sprites.push(this);
  92. this.uniqueId = this._manager.scene.getUniqueId();
  93. this.position = Vector3.Zero();
  94. }
  95. /**
  96. * Returns the string "Sprite"
  97. * @returns "Sprite"
  98. */
  99. public getClassName(): string {
  100. return "Sprite";
  101. }
  102. /**
  103. * Starts an animation
  104. * @param from defines the initial key
  105. * @param to defines the end key
  106. * @param loop defines if the animation must loop
  107. * @param delay defines the start delay (in ms)
  108. * @param onAnimationEnd defines a callback to call when animation ends
  109. */
  110. public playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: () => void): void {
  111. this._fromIndex = from;
  112. this._toIndex = to;
  113. this._loopAnimation = loop;
  114. this._delay = delay;
  115. this._animationStarted = true;
  116. if (from < to) {
  117. this._direction = 1;
  118. } else {
  119. this._direction = -1;
  120. this._toIndex = from;
  121. this._fromIndex = to;
  122. }
  123. this.cellIndex = from;
  124. this._time = 0;
  125. this._onAnimationEnd = onAnimationEnd;
  126. }
  127. /** Stops current animation (if any) */
  128. public stopAnimation(): void {
  129. this._animationStarted = false;
  130. }
  131. /** @hidden */
  132. public _animate(deltaTime: number): void {
  133. if (!this._animationStarted) {
  134. return;
  135. }
  136. this._time += deltaTime;
  137. if (this._time > this._delay) {
  138. this._time = this._time % this._delay;
  139. this.cellIndex += this._direction;
  140. if (this._direction > 0 && this.cellIndex > this._toIndex || this._direction < 0 && this.cellIndex < this._fromIndex) {
  141. if (this._loopAnimation) {
  142. this.cellIndex = this._direction > 0 ? this._fromIndex : this._toIndex;
  143. } else {
  144. this.cellIndex = this._toIndex;
  145. this._animationStarted = false;
  146. if (this._onAnimationEnd) {
  147. this._onAnimationEnd();
  148. }
  149. if (this.disposeWhenFinishedAnimating) {
  150. this.dispose();
  151. }
  152. }
  153. }
  154. }
  155. }
  156. /** Release associated resources */
  157. public dispose(): void {
  158. for (var i = 0; i < this._manager.sprites.length; i++) {
  159. if (this._manager.sprites[i] == this) {
  160. this._manager.sprites.splice(i, 1);
  161. }
  162. }
  163. // Callback
  164. this.onDisposeObservable.notifyObservers(this);
  165. this.onDisposeObservable.clear();
  166. }
  167. }