sprite.ts 5.0 KB

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