sprite.ts 7.5 KB

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