AnimatedSprite2D.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /************************************************************************
  2. * AnimatedSprite2D.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * The {@code AnimatedSprite2D} class represents a GameObject that inherits from
  23. * Sprite2D and extends its functionality to automatically draw a series of sprites
  24. * to the screen forming an animation.
  25. *
  26. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  27. * ! of another GameObject on the tree or as a root).
  28. *
  29. * @author Pedro Schneider
  30. *
  31. * @class
  32. */
  33. class AnimatedSprite2D extends Sprite2D
  34. {
  35. /**
  36. * Initializes as AnimatedSprite2D GameObject with the specified parameters.
  37. *
  38. * @param {String} name name for this AnimatedSprite2D GameObject.
  39. * @param {p5.Image} p5Image this AnimatedSprite2D's p5.Image to be drawn
  40. * to the buffer.
  41. * @param {SpriteFrames} spriteFrames data holding the animation in frames to be
  42. * played by this GameObject.
  43. *
  44. * @constructor
  45. */
  46. constructor(name, p5Image, spriteFrames)
  47. {
  48. super(name, p5Image);
  49. this.spriteFrames = spriteFrames;
  50. this.playing = false;
  51. this.frame = 0;
  52. this.currentAnimation = 0;
  53. this.timeSinceLastFrame = 0;
  54. }
  55. /**
  56. * Sets the current SpriteAnimation to be played based on its index on the list of
  57. * animations on SpriteFrames. Does nothing if the index is invalid.
  58. *
  59. * ! The index refers to the order you added the animations to this AnimatedSprite2D's
  60. * ! SpriteFrames, starting at 0.
  61. *
  62. * @param {number} idx index of the SpriteAnimation on SpriteFrames.
  63. */
  64. setCurrentAnimationByIndex(idx)
  65. {
  66. if (idx < this.spriteFrames.numAnimations)
  67. this.currentAnimation = idx;
  68. else this.currentAnimation = null;
  69. this.frame = 0;
  70. this.timeSinceLastFrame = 0;
  71. }
  72. /**
  73. * Sets the current SpriteAnimation to be played based on its name on the list of
  74. * animations on SpriteFrames. Does nothing if the name is invalid.
  75. *
  76. * @param {String} name name of the SpriteAnimation on SpriteFrames.
  77. */
  78. setCurrentAnimationByName(name)
  79. {
  80. this.currentAnimation = this.spriteFrames.getAnimationIndexByName(name);
  81. this.frame = 0;
  82. this.timeSinceLastFrame = 0;
  83. }
  84. /**
  85. * Sets the current SpriteAnimation's time between frames.
  86. *
  87. * @param {number} time new time in seconds between frames for the
  88. * current SpriteAnimation.
  89. */
  90. setCurrentFrameTime(time)
  91. {
  92. this.getCurrentAnimation().setFrameTime(time);
  93. }
  94. /**
  95. * Sets the current SpriteAnimation's frames per second.
  96. *
  97. * @param {number} fps new frames per second for the current SpriteAnimation.
  98. */
  99. setCurrentFPS(fps)
  100. {
  101. this.getCurrentAnimation().setFPS(fps);
  102. }
  103. /**
  104. * Returns the SpriteAnimation with the given index on the list os SpriteAnimations on
  105. * this AnimatedSprite2D's SpriteFrames.
  106. *
  107. * ! The index refers to the order you added the animations to this AnimatedSprite2D's
  108. * ! SpriteFrames, starting at 0.
  109. *
  110. * @param {number} idx index of the desired SpriteAnimation on SpriteFrames' list.
  111. *
  112. * @returns {SpriteAnimation} the desired SpriteAnimation based on the given index,
  113. * or null if the index is invalid.
  114. */
  115. getAnimationByIndex(idx)
  116. {
  117. return this.spriteFrames.getAnimationByIndex(idx);
  118. }
  119. /**
  120. * Returns the current SpriteAnimation on this AnimatedSprite2D's Sprite Frames.
  121. *
  122. * @returns {SpriteAnimation} the current SpriteAnimation on the SprteFrames.
  123. */
  124. getCurrentAnimation()
  125. {
  126. return this.spriteFrames.getAnimationByIndex(this.currentAnimation);
  127. }
  128. /**
  129. * Rerturns the p5.Image of the current frame on the current SpriteAnimation.
  130. *
  131. * @returns {p5.Image} image of the current frame.
  132. */
  133. getCurrentFrame()
  134. {
  135. return this.getCurrentAnimation().getFrame(this.frame);
  136. }
  137. /**
  138. * Returns the time in seconds between frames of the current SpriteAnimation.
  139. *
  140. * @returns {number} time in seconds between frames of the current SpriteAnimation.
  141. */
  142. getCurrentFrameTime()
  143. {
  144. return this.getCurrentAnimation().getFrameTime();
  145. }
  146. /**
  147. * Returns the number of frames of the current SpriteAnimation.
  148. *
  149. * @returns {number} number of frames of the current SpriteAnimation.
  150. */
  151. getCurrentNumFrames()
  152. {
  153. return this.getCurrentAnimation().getNumFrames();
  154. }
  155. /**
  156. * Starts playing the current SpriteAnimation.
  157. */
  158. play()
  159. {
  160. this.playing = true;
  161. }
  162. /**
  163. * Stops playing the current SpriteAnimation.
  164. */
  165. stop()
  166. {
  167. this.playing = false;
  168. }
  169. /**
  170. * Rerturns the playing state of the current SpriteAnimation.
  171. *
  172. * @returns {boolean} true if the animation is playing, false if not.
  173. */
  174. isPlaying()
  175. {
  176. return this.playing;
  177. }
  178. /**
  179. * Updates the current SpriteAnimation if its playing, and updates this
  180. * AnimatedSprite2D's P5Image to the current frame based on the current
  181. * SpriteAnimation.
  182. *
  183. * @param {number} delta number of seconds ellapsed since the last frame.
  184. *
  185. * @override
  186. */
  187. update(delta)
  188. {
  189. this.udpateGlobalTransform();
  190. // Forwards the animation.
  191. if (this.playing)
  192. {
  193. this.timeSinceLastFrame += delta;
  194. if (this.timeSinceLastFrame >= this.getCurrentFrameTime())
  195. {
  196. this.frame = (this.frame + 1) % this.getCurrentNumFrames();
  197. this.timeSinceLastFrame = 0;
  198. }
  199. }
  200. this.P5Image = this.getCurrentFrame();
  201. this.updateChildren(delta);
  202. }
  203. }