AnimatedSprite2D.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * @author Pedro Schneider
  27. *
  28. * @class
  29. */
  30. class AnimatedSprite2D extends Sprite2D
  31. {
  32. /**
  33. * @constructor
  34. * Initializes as AnimatedSprite2D GameObject with the specified parameters.
  35. *
  36. * @param {String} name name for this AnimatedSprite2D GameObject.
  37. * @param {p5.Image} p5Image this AnimatedSprite2D's p5.Image to be drawn
  38. * to the buffer.
  39. * @param {SpriteFrames} spriteFrames data holding the animation in frames to be
  40. * played by this GameObject.
  41. */
  42. constructor(name, p5Image, spriteFrames)
  43. {
  44. super(name, p5Image);
  45. this.spriteFrames = spriteFrames;
  46. this.playing = false;
  47. this.frame = 0;
  48. this.currentAnimation = 0;
  49. this.timeSinceLastFrame = 0;
  50. }
  51. /**
  52. * Sets the current SpriteAnimation to be played based on its index on the list of
  53. * animations on SpriteFrames. Does nothing if the index is invalid.
  54. *
  55. * ! The index refers to the order you added the animations to this AnimatedSprite2D's
  56. * ! SpriteFrames, starting at 0.
  57. *
  58. * @param {number} idx index of the SpriteAnimation on SpriteFrames.
  59. */
  60. setCurrentAnimationByIndex(idx)
  61. {
  62. if (idx < this.spriteFrames.numAnimations)
  63. this.currentAnimation = idx;
  64. else this.currentAnimation = null;
  65. this.frame = 0;
  66. this.timeSinceLastFrame = 0;
  67. }
  68. /**
  69. * Sets the current SpriteAnimation to be played based on its name on the list of
  70. * animations on SpriteFrames. Does nothing if the name is invalid.
  71. *
  72. * @param {String} name name of the SpriteAnimation on SpriteFrames.
  73. */
  74. setCurrentAnimationByName(name)
  75. {
  76. this.currentAnimation = this.spriteFrames.getAnimationIndexByName(name);
  77. this.frame = 0;
  78. this.timeSinceLastFrame = 0;
  79. }
  80. /**
  81. * Sets the current SpriteAnimation's time between frames.
  82. *
  83. * @param {number} time new time in seconds between frames for the
  84. * current SpriteAnimation.
  85. */
  86. setCurrentFrameTime(time)
  87. {
  88. this.getCurrentAnimation().setFrameTime(time);
  89. }
  90. /**
  91. * Sets the current SpriteAnimation's frames per second.
  92. *
  93. * @param {number} fps new frames per second for the current SpriteAnimation.
  94. */
  95. setCurrentFPS(fps)
  96. {
  97. this.getCurrentAnimation().setFPS(fps);
  98. }
  99. /**
  100. * Returns the SpriteAnimation with the given index on the list os SpriteAnimations on
  101. * this AnimatedSprite2D's SpriteFrames.
  102. *
  103. * ! The index refers to the order you added the animations to this AnimatedSprite2D's
  104. * ! SpriteFrames, starting at 0.
  105. *
  106. * @param {number} idx index of the desired SpriteAnimation on SpriteFrames' list.
  107. *
  108. * @returns {SpriteAnimation} the desired SpriteAnimation based on the given index,
  109. * or null if the index is invalid.
  110. */
  111. getAnimationByIndex(idx)
  112. {
  113. return this.spriteFrames.getAnimationByIndex(idx);
  114. }
  115. /**
  116. * Returns the current SpriteAnimation on this AnimatedSprite2D's Sprite Frames.
  117. *
  118. * @returns {SpriteAnimation} the current SpriteAnimation on the SprteFrames.
  119. */
  120. getCurrentAnimation()
  121. {
  122. return this.spriteFrames.getAnimationByIndex(this.currentAnimation);
  123. }
  124. /**
  125. * Rerturns the p5.Image of the current frame on the current SpriteAnimation.
  126. *
  127. * @returns {p5.Image} image of the current frame.
  128. */
  129. getCurrentFrame()
  130. {
  131. return this.getCurrentAnimation().getFrame(this.frame);
  132. }
  133. /**
  134. * Returns the time in seconds between frames of the current SpriteAnimation.
  135. *
  136. * @returns {number} time in seconds between frames of the current SpriteAnimation.
  137. */
  138. getCurrentFrameTime()
  139. {
  140. return this.getCurrentAnimation().getFrameTime();
  141. }
  142. /**
  143. * Returns the number of frames of the current SpriteAnimation.
  144. *
  145. * @returns {number} number of frames of the current SpriteAnimation.
  146. */
  147. getCurrentNumFrames()
  148. {
  149. return this.getCurrentAnimation().getNumFrames();
  150. }
  151. /**
  152. * Starts playing the current SpriteAnimation.
  153. */
  154. play()
  155. {
  156. this.playing = true;
  157. }
  158. /**
  159. * Stops playing the current SpriteAnimation.
  160. */
  161. stop()
  162. {
  163. this.playing = false;
  164. }
  165. /**
  166. * Rerturns the playing state of the current SpriteAnimation.
  167. *
  168. * @returns {boolean} true if the animation is playing, false if not.
  169. */
  170. isPlaying()
  171. {
  172. return this.playing;
  173. }
  174. /**
  175. * @override
  176. * Updates the current SpriteAnimation if its playing, and updates this
  177. * AnimatedSprite2D's P5Image to the current frame based on the current
  178. * SpriteAnimation.
  179. *
  180. * @param {number} delta number of seconds ellapsed since the last frame.
  181. */
  182. update(delta)
  183. {
  184. if (this.playing)
  185. {
  186. this.timeSinceLastFrame += delta;
  187. if (this.timeSinceLastFrame >= this.getCurrentFrameTime())
  188. {
  189. this.frame = (this.frame + 1) % this.getCurrentNumFrames();
  190. this.timeSinceLastFrame = 0;
  191. }
  192. }
  193. this.P5Image = this.getCurrentFrame();
  194. this._update(delta);
  195. for (let i = 0; i < this.children.length; i++)
  196. this.children[i].update(delta);
  197. }
  198. }