AssetHandler.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /************************************************************************
  2. * AssetHandler.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 AssetHandler} singleton provides an interface for the user
  23. * to load various types of assets to memory.
  24. *
  25. * @author Pedro Schneider
  26. *
  27. * @namespace
  28. */
  29. const AssetHandler = {
  30. cachedTextures: [], // Cache for TextureRes.
  31. cachedAudio: [], // Cache for AudioRes.
  32. cachedFonts: [], // Cache for FontRes.
  33. /**
  34. * Loads an image to cache as a TextureRes. Can be loaded from a request to a server
  35. * or from a local path to a file. The recomended place to call this function is
  36. * inside preload for static loading, but it can also be called dynamically inside
  37. * the game.
  38. *
  39. * @param {String} name name for the TextureRes to be saved as.
  40. * @param {String} link link to load the image file from (server or local).
  41. */
  42. loadTexture: function(name, link)
  43. {
  44. let textRes = new TextureRes(name, null);
  45. this.cachedTextures.push(textRes);
  46. loadImage(link, img =>
  47. {
  48. textRes.P5Image = img;
  49. });
  50. },
  51. /**
  52. * Query a TextureRes by its name.
  53. *
  54. * @param {String} name name of the requested TextureRes.
  55. *
  56. * @returns {TextureRes} reference to the first loaded TextureRes whose name matches
  57. * the parameter, or null if no TextureRes matches the name.
  58. */
  59. getTextureByName: function(name)
  60. {
  61. for (let i = 0; i < this.cachedTextures.length; i++)
  62. {
  63. if (this.cachedTextures[i].name == name)
  64. {
  65. return this.cachedTextures[i];
  66. }
  67. }
  68. return null;
  69. },
  70. /**
  71. * Query a p5.Image by the name of the TextureRes it was loaded as.
  72. *
  73. * @param {String} name name of the TextureRes that holds the desired
  74. * p5.Image.
  75. *
  76. * @returns {p5.Image} p5.Image held by the first loaded TextureRes whose name
  77. * matches the parameter, or null if no TextureRes matches the name.
  78. */
  79. getP5ImageByName: function(name)
  80. {
  81. for (let i = 0; i < this.cachedTextures.length; i++)
  82. {
  83. if (this.cachedTextures[i].name == name)
  84. {
  85. return this.cachedTextures[i].P5Image;
  86. }
  87. }
  88. return null;
  89. },
  90. /**
  91. * Loads an audio to cache as an AudioRes. Can be loaded from a request to a server
  92. * or from a local path to a file. The recomended place to call this function is
  93. * inside preload for static loading, but it can also be called dynamically inside
  94. * the game.
  95. *
  96. * @param {String} name name for the AudioRes to be saved as.
  97. * @param {String} link link to load the audio file from (server or local).
  98. */
  99. loadAudio: function(name, link)
  100. {
  101. let audio = createAudio(link);
  102. this.cachedAudio.push(new AudioRes(name, audio));
  103. },
  104. /**
  105. * Query an AudioRes by its name.
  106. *
  107. * @param {String} name name of the requested AudioRes.
  108. *
  109. * @returns {AudioRes} reference to the first loaded AudioRes whose name matches
  110. * the parameter, or null if no AudioRes matches the name.
  111. */
  112. getAudioByName: function(name)
  113. {
  114. for (let i = 0; i < this.cachedAudio.length; i++)
  115. {
  116. if (this.cachedAudio[i].name == name)
  117. {
  118. return this.cachedAudio[i];
  119. }
  120. }
  121. return null;
  122. },
  123. /**
  124. * Query a p5.Audio by the name of the AudioRes it was loaded as.
  125. *
  126. * @param {String} name name of the AudioRes that holds the desired
  127. * p5.Audio.
  128. *
  129. * @returns {p5.Audio} p5.Audio held by the first loaded AudioRes whose name
  130. * matches the parameter, or null if no AudioRes matches the name.
  131. */
  132. getP5AudioByName: function(name)
  133. {
  134. for (let i = 0; i < this.cachedAudio.length; i++)
  135. {
  136. if (this.cachedAudio[i].name == name)
  137. {
  138. return this.cachedAudio[i].P5Audio;
  139. }
  140. }
  141. return null;
  142. },
  143. /**
  144. * Loads a font to cache as FontRes. Can be loaded from a request to a server
  145. * or from a local path to a file. The recomended place to call this function is
  146. * inside preload for static loading, but it can also be called dynamically inside
  147. * the game.
  148. *
  149. * @param {String} name name for the FontRes to be saved as.
  150. * @param {String} link link to load the font file from (server or local).
  151. */
  152. loadFont: function(name, link)
  153. {
  154. let fontRes = new FontRes(name, null);
  155. this.cachedFonts.push(fontRes);
  156. loadFont(link, font =>
  157. {
  158. fontRes.P5Font = font;
  159. })
  160. },
  161. /**
  162. * Query a FontRes by its name.
  163. *
  164. * @param {String} name name of the requested FontRes.
  165. *
  166. * @returns {FontRes} reference to the first loaded FontRes whose name matches
  167. * the parameter, or null if no FontRes matches the name.
  168. */
  169. getFontByName: function(name)
  170. {
  171. for (let i = 0; i < this.cachedFonts.length; i++)
  172. {
  173. if (this.cachedFonts[i].name == name)
  174. {
  175. return this.cachedFonts[i];
  176. }
  177. }
  178. return null;
  179. },
  180. /**
  181. * Query a p5.Font by the name of the FontRes it was loaded as.
  182. *
  183. * @param {String} name name of the FontRes that holds the desired
  184. * p5.Font.
  185. *
  186. * @returns {p5.Font} p5.Font held by the first loaded FontRes whose name
  187. * matches the parameter, or null if no FontRes matches the name.
  188. */
  189. getP5FontByName: function(name)
  190. {
  191. for (let i = 0; i < this.cachedFonts.length; i++)
  192. {
  193. if (this.cachedFonts[i].name == name)
  194. {
  195. return this.cachedFonts[i].P5Font;
  196. }
  197. }
  198. return null;
  199. }
  200. };