babylon.sprite.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 _frameCount = 0;
  22. private _manager: SpriteManager;
  23. private _time = 0;
  24. private _onAnimationEnd: () => void;
  25. public get size(): number {
  26. return this.width;
  27. }
  28. public set size(value: number) {
  29. this.width = value;
  30. this.height = value;
  31. }
  32. constructor(public name: string, manager: SpriteManager) {
  33. this._manager = manager;
  34. this._manager.sprites.push(this);
  35. this.position = Vector3.Zero();
  36. }
  37. public playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: () => void): void {
  38. this._fromIndex = from;
  39. this._toIndex = to;
  40. this._loopAnimation = loop;
  41. this._delay = delay;
  42. this._animationStarted = true;
  43. this._direction = from < to ? 1 : -1;
  44. this.cellIndex = from;
  45. this._time = 0;
  46. this._onAnimationEnd = onAnimationEnd;
  47. }
  48. public stopAnimation(): void {
  49. this._animationStarted = false;
  50. }
  51. public _animate(deltaTime: number): void {
  52. if (!this._animationStarted)
  53. return;
  54. this._time += deltaTime;
  55. if (this._time > this._delay) {
  56. this._time = this._time % this._delay;
  57. this.cellIndex += this._direction;
  58. if (this.cellIndex > this._toIndex) {
  59. if (this._loopAnimation) {
  60. this.cellIndex = this._fromIndex;
  61. } else {
  62. this.cellIndex = this._toIndex;
  63. this._animationStarted = false;
  64. if (this._onAnimationEnd) {
  65. this._onAnimationEnd();
  66. }
  67. if (this.disposeWhenFinishedAnimating) {
  68. this.dispose();
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public dispose(): void {
  75. for (var i = 0; i < this._manager.sprites.length; i++) {
  76. if (this._manager.sprites[i] == this) {
  77. this._manager.sprites.splice(i, 1);
  78. }
  79. }
  80. }
  81. }
  82. }