babylon.sprite.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. module BABYLON {
  2. export class Sprite {
  3. public position: Vector3;
  4. public color = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  5. public size = 1.0;
  6. public angle = 0;
  7. public cellIndex = 0;
  8. public invertU = 0;
  9. public invertV = 0;
  10. public disposeWhenFinishedAnimating: boolean;
  11. public animations = new Array<Animation>();
  12. private _animationStarted = false;
  13. private _loopAnimation = false;
  14. private _fromIndex = 0;
  15. private _toIndex = 0;
  16. private _delay = 0;
  17. private _direction = 1;
  18. private _frameCount = 0;
  19. private _manager: SpriteManager;
  20. private _time = 0;
  21. constructor(public name: string, manager: SpriteManager) {
  22. this._manager = manager;
  23. this._manager.sprites.push(this);
  24. this.position = BABYLON.Vector3.Zero();
  25. }
  26. public playAnimation(from: number, to: number, loop: boolean, delay: number): void {
  27. this._fromIndex = from;
  28. this._toIndex = to;
  29. this._loopAnimation = loop;
  30. this._delay = delay;
  31. this._animationStarted = true;
  32. this._direction = from < to ? 1 : -1;
  33. this.cellIndex = from;
  34. this._time = 0;
  35. }
  36. public stopAnimation(): void {
  37. this._animationStarted = false;
  38. }
  39. public _animate(deltaTime: number): void {
  40. if (!this._animationStarted)
  41. return;
  42. this._time += deltaTime;
  43. if (this._time > this._delay) {
  44. this._time = this._time % this._delay;
  45. this.cellIndex += this._direction;
  46. if (this.cellIndex == this._toIndex) {
  47. if (this._loopAnimation) {
  48. this.cellIndex = this._fromIndex;
  49. } else {
  50. this._animationStarted = false;
  51. if (this.disposeWhenFinishedAnimating) {
  52. this.dispose();
  53. }
  54. }
  55. }
  56. }
  57. }
  58. public dispose(): void {
  59. for (var i = 0; i < this._manager.sprites.length; i++) {
  60. if (this._manager.sprites[i] == this) {
  61. this._manager.sprites.splice(i, 1);
  62. }
  63. }
  64. }
  65. }
  66. }