SpriteFrames.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /************************************************************************
  2. * SpriteFrames.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. * This {@code SpriteAnimation} class provides an interface to store an animation in the form
  23. * of a series of images.
  24. *
  25. * ! This class has no use in the engine aside from holding the data for an animation inside a
  26. * ! SpriteFrames object. Its use is not recomended anywhere else.
  27. *
  28. * @author Pedro Schneider
  29. *
  30. * @class
  31. */
  32. class SpriteAnimation extends Resource
  33. {
  34. /**
  35. * Initialize a SpriteAnimation with the given parameters.
  36. *
  37. * @param {String} name name for the SpriteAnimation.
  38. * @param {p5.Image} p5Image p5.Image holding all the frames for the aniamtion.
  39. * @param {number} rows number of rows of frames the image has.
  40. * @param {number} cols number of columns of frames the image has.
  41. * @param {Array} indices indices of the frames from left-to-right and top-to-bottom
  42. * that should be included in the animation.
  43. * @param {number} fps frames per second the animation should be played at.
  44. *
  45. * @constructor
  46. */
  47. constructor(name = "default", p5Image, rows, cols, indices, fps)
  48. {
  49. super(name);
  50. this.fullP5Image = p5Image;
  51. this.rows = rows;
  52. this.columns = cols;
  53. this.indices = indices;
  54. this.numFrames = this.indices.length;
  55. this.frameTime = 1 / fps;
  56. // Extract all the requested frames from the image as individual p5.Images.
  57. this.frames = [];
  58. for (let i = 0; i < this.numFrames; i++)
  59. {
  60. this.frames.push(this.makeFrame(i));
  61. }
  62. }
  63. /**
  64. * Extracts a frame from the full image based on its index on the image, from left-to-right and
  65. * top-to-bottom.
  66. *
  67. * @param {number} idx index of the desired frame on the full image.
  68. *
  69. * @returns {p5.Image} frame with the requested index on the full image.
  70. */
  71. makeFrame(idx)
  72. {
  73. let r = int(this.indices[idx] / this.columns);
  74. let c = this.indices[idx] % this.columns;
  75. let w = this.fullP5Image.width / this.columns;
  76. let h = this.fullP5Image.height / this.rows;
  77. let x = c * w;
  78. let y = r * h;
  79. return this.fullP5Image.get(x, y, w, h);
  80. }
  81. /**
  82. * Sets the time between frames for the animation.
  83. *
  84. * @param {number} time new time between frames for the animation.
  85. */
  86. setFrameTime(time)
  87. {
  88. this.frameTime = time;
  89. }
  90. /**
  91. * Sets the frames per second for the animation.
  92. *
  93. * @param {number} fps new frames per second value for the animation.
  94. */
  95. setFPS(fps)
  96. {
  97. this.frameTime = 1 / fps;
  98. }
  99. /**
  100. * Returns a frame of the animation based on its index.
  101. *
  102. * @param {number} idx index of the desired frame on the animation.
  103. *
  104. * @returns {p5.Image} frame with the requested index on the animation, or null if
  105. * the requested index is invalid.
  106. */
  107. getFrame(idx)
  108. {
  109. if (idx >= 0 && idx < this.numFrames)
  110. return this.frames[idx];
  111. return null;
  112. }
  113. /**
  114. * Returns the time between frames for the animation.
  115. *
  116. * @returns {number} time between frames for the aniamtion.
  117. */
  118. getFrameTime()
  119. {
  120. return this.frameTime;
  121. }
  122. /**
  123. * Returns the number of frames the animation has.
  124. *
  125. * @returns {number} number of frames the animation has.
  126. */
  127. getNumFrames()
  128. {
  129. return this.numFrames;
  130. }
  131. }
  132. /**
  133. * This {@code SpriteFrames} class provides an interface to store multiple related SpriteAnimations
  134. * inside a sigle object for use with the AnimatedSprite2D GameObject.
  135. *
  136. * @author Pedro Schneider
  137. *
  138. * @class
  139. */
  140. class SpriteFrames extends Resource
  141. {
  142. /**
  143. * Initializes an empty SpriteFrames.
  144. *
  145. * @constructor
  146. */
  147. constructor()
  148. {
  149. super();
  150. this.animations = [];
  151. this.numAnimations = 0;
  152. }
  153. /**
  154. * Returns an animation based on its index on the list of aniamtions.
  155. *
  156. * @param {number} idx index of the desired animation.
  157. *
  158. * @returns {SpriteAnimation} animation with the requested index, or null if the
  159. * index is invalid.
  160. */
  161. getAnimationByIndex(idx)
  162. {
  163. if (idx >= 0 && idx < this.numAnimations) return this.animations[idx];
  164. return null;
  165. }
  166. /**
  167. * Returns an animation baes on its name.
  168. *
  169. * @param {String} name name of the desired animation.
  170. *
  171. * @returns {SpriteAnimation} animation with the requested name, or
  172. * null if no animation has that name.
  173. */
  174. getAnimationByName(name)
  175. {
  176. for (let i = 0; this.numAnimations; i++)
  177. {
  178. if (this.animations[i].name == name)
  179. {
  180. return this.animations[i];
  181. }
  182. }
  183. return null;
  184. }
  185. /**
  186. * Returns the index of an animation on the list of animations based on its name.
  187. *
  188. * @param {number} name name of the desired animation.
  189. * @returns {number} index on the list of animations of the animation with the requested
  190. * name, or null if no animation has that name
  191. */
  192. getAnimationIndexByName(name)
  193. {
  194. for (let i = 0; this.numAnimations; i++)
  195. {
  196. if (this.animations[i].name == name)
  197. return i;
  198. }
  199. return null;
  200. }
  201. /**
  202. * Adds a new SprteAniamtion to the list of aniamtions with the given data.
  203. *
  204. * @param {String} name name for the SpriteAnimation.
  205. * @param {p5.Image} p5Image p5.Image holding all the frames for the aniamtion.
  206. * @param {number} rows number of rows of frames the image has.
  207. * @param {number} cols number of columns of frames the image has.
  208. * @param {Array} indices indices of the frames from left-to-right and top-to-bottom
  209. * that should be included in the animation.
  210. * @param {number} fps frames per second the animation should be played at.
  211. * Default is 24.
  212. */
  213. addAnimation(name, p5Image, rows, cols, indices, fps = 24)
  214. {
  215. this.animations.push(new SpriteAnimation(name, p5Image, rows, cols, indices, fps));
  216. this.numAnimations++;
  217. }
  218. }