babylon.sprite.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module BABYLON {
  2. export class Sprite {
  3. public position: Vector3;
  4. public color = new Color4(1.0, 1.0, 1.0, 1.0);
  5. public width = 1.0;
  6. public height = 1.0;
  7. public angle = 0;
  8. public cellIndex = 0;
  9. public invertU = 0;
  10. public invertV = 0;
  11. public disposeWhenFinishedAnimating: boolean;
  12. public animations = new Array<Animation>();
  13. public isPickable = false;
  14. public actionManager: ActionManager;
  15. private _animationStarted = false;
  16. private _loopAnimation = false;
  17. private _fromIndex = 0;
  18. private _toIndex = 0;
  19. private _delay = 0;
  20. private _direction = 1;
  21. private _manager: ISpriteManager;
  22. private _time = 0;
  23. private _onAnimationEnd: () => void;
  24. /**
  25. * Gets or sets a boolean indicating if the sprite is visible (renderable). Default is true
  26. */
  27. public isVisible = true;
  28. public get size(): number {
  29. return this.width;
  30. }
  31. public set size(value: number) {
  32. this.width = value;
  33. this.height = value;
  34. }
  35. constructor(public name: string, manager: ISpriteManager) {
  36. this._manager = manager;
  37. this._manager.sprites.push(this);
  38. this.position = Vector3.Zero();
  39. }
  40. public playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: () => void): void {
  41. this._fromIndex = from;
  42. this._toIndex = to;
  43. this._loopAnimation = loop;
  44. this._delay = delay;
  45. this._animationStarted = true;
  46. this._direction = from < to ? 1 : -1;
  47. this.cellIndex = from;
  48. this._time = 0;
  49. this._onAnimationEnd = onAnimationEnd;
  50. }
  51. public stopAnimation(): void {
  52. this._animationStarted = false;
  53. }
  54. /** @hidden */
  55. public _animate(deltaTime: number): void {
  56. if (!this._animationStarted)
  57. return;
  58. this._time += deltaTime;
  59. if (this._time > this._delay) {
  60. this._time = this._time % this._delay;
  61. this.cellIndex += this._direction;
  62. if (this.cellIndex > this._toIndex) {
  63. if (this._loopAnimation) {
  64. this.cellIndex = this._fromIndex;
  65. } else {
  66. this.cellIndex = this._toIndex;
  67. this._animationStarted = false;
  68. if (this._onAnimationEnd) {
  69. this._onAnimationEnd();
  70. }
  71. if (this.disposeWhenFinishedAnimating) {
  72. this.dispose();
  73. }
  74. }
  75. }
  76. }
  77. }
  78. public dispose(): void {
  79. for (var i = 0; i < this._manager.sprites.length; i++) {
  80. if (this._manager.sprites[i] == this) {
  81. this._manager.sprites.splice(i, 1);
  82. }
  83. }
  84. }
  85. }
  86. }