sprite.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { Vector3, Color4 } from "../Maths/math";
  2. import { Nullable } from "../types";
  3. import { ActionManager } from "../Actions/actionManager";
  4. import { ISpriteManager } from "./spriteManager";
  5. /**
  6. * Class used to represent a sprite
  7. * @see http://doc.babylonjs.com/babylon101/sprites
  8. */
  9. export class Sprite {
  10. /** Gets or sets the current world position */
  11. public position: Vector3;
  12. /** Gets or sets the main color */
  13. public color = new Color4(1.0, 1.0, 1.0, 1.0);
  14. /** Gets or sets the width */
  15. public width = 1.0;
  16. /** Gets or sets the height */
  17. public height = 1.0;
  18. /** Gets or sets rotation angle */
  19. public angle = 0;
  20. /** Gets or sets the cell index in the sprite sheet */
  21. public cellIndex = 0;
  22. /** Gets or sets a boolean indicating if UV coordinates should be inverted in U axis */
  23. public invertU = 0;
  24. /** Gets or sets a boolean indicating if UV coordinates should be inverted in B axis */
  25. public invertV = 0;
  26. /** Gets or sets a boolean indicating that this sprite should be disposed after animation ends */
  27. public disposeWhenFinishedAnimating: boolean;
  28. /** Gets the list of attached animations */
  29. public animations = new Array<Animation>();
  30. /** Gets or sets a boolean indicating if the sprite can be picked */
  31. public isPickable = false;
  32. /**
  33. * Gets or sets the associated action manager
  34. */
  35. public actionManager: Nullable<ActionManager>;
  36. private _animationStarted = false;
  37. private _loopAnimation = false;
  38. private _fromIndex = 0;
  39. private _toIndex = 0;
  40. private _delay = 0;
  41. private _direction = 1;
  42. private _manager: ISpriteManager;
  43. private _time = 0;
  44. private _onAnimationEnd: () => void;
  45. /**
  46. * Gets or sets a boolean indicating if the sprite is visible (renderable). Default is true
  47. */
  48. public isVisible = true;
  49. /**
  50. * Gets or sets the sprite size
  51. */
  52. public get size(): number {
  53. return this.width;
  54. }
  55. public set size(value: number) {
  56. this.width = value;
  57. this.height = value;
  58. }
  59. /**
  60. * Creates a new Sprite
  61. * @param name defines the name
  62. * @param manager defines the manager
  63. */
  64. constructor(
  65. /** defines the name */
  66. public name: string,
  67. manager: ISpriteManager) {
  68. this._manager = manager;
  69. this._manager.sprites.push(this);
  70. this.position = Vector3.Zero();
  71. }
  72. /**
  73. * Starts an animation
  74. * @param from defines the initial key
  75. * @param to defines the end key
  76. * @param loop defines if the animation must loop
  77. * @param delay defines the start delay (in ms)
  78. * @param onAnimationEnd defines a callback to call when animation ends
  79. */
  80. public playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: () => void): void {
  81. this._fromIndex = from;
  82. this._toIndex = to;
  83. this._loopAnimation = loop;
  84. this._delay = delay;
  85. this._animationStarted = true;
  86. if (from < to) {
  87. this._direction = 1;
  88. } else {
  89. this._direction = -1;
  90. this._toIndex = from;
  91. this._fromIndex = to;
  92. }
  93. this.cellIndex = from;
  94. this._time = 0;
  95. this._onAnimationEnd = onAnimationEnd;
  96. }
  97. /** Stops current animation (if any) */
  98. public stopAnimation(): void {
  99. this._animationStarted = false;
  100. }
  101. /** @hidden */
  102. public _animate(deltaTime: number): void {
  103. if (!this._animationStarted) {
  104. return;
  105. }
  106. this._time += deltaTime;
  107. if (this._time > this._delay) {
  108. this._time = this._time % this._delay;
  109. this.cellIndex += this._direction;
  110. if (this._direction > 0 && this.cellIndex > this._toIndex || this._direction < 0 && this.cellIndex < this._fromIndex) {
  111. if (this._loopAnimation) {
  112. this.cellIndex = this._direction > 0 ? this._fromIndex : this._toIndex;
  113. } else {
  114. this.cellIndex = this._toIndex;
  115. this._animationStarted = false;
  116. if (this._onAnimationEnd) {
  117. this._onAnimationEnd();
  118. }
  119. if (this.disposeWhenFinishedAnimating) {
  120. this.dispose();
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /** Release associated resources */
  127. public dispose(): void {
  128. for (var i = 0; i < this._manager.sprites.length; i++) {
  129. if (this._manager.sprites[i] == this) {
  130. this._manager.sprites.splice(i, 1);
  131. }
  132. }
  133. }
  134. }