babylon.sprite.ts 2.3 KB

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