babylon.sprite.ts 2.6 KB

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