babylon.sprite.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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._animationStarted = false;
  63. if (this._onAnimationEnd) {
  64. this._onAnimationEnd();
  65. }
  66. if (this.disposeWhenFinishedAnimating) {
  67. this.dispose();
  68. }
  69. }
  70. }
  71. }
  72. }
  73. public dispose(): void {
  74. for (var i = 0; i < this._manager.sprites.length; i++) {
  75. if (this._manager.sprites[i] == this) {
  76. this._manager.sprites.splice(i, 1);
  77. }
  78. }
  79. }
  80. }
  81. }