AssetHandler.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const AssetHandler = {
  2. cachedTextures: [],
  3. cachedAudio: [],
  4. cachedFonts: [],
  5. loadTexture: function(name, link)
  6. {
  7. let textRes = new TextureRes(name, null);
  8. this.cachedTextures.push(textRes);
  9. loadImage(link, img =>
  10. {
  11. textRes.P5Image = img;
  12. });
  13. },
  14. getTextureByName: function(name)
  15. {
  16. for (let i = 0; i < this.cachedTextures.length; i++)
  17. {
  18. if (this.cachedTextures[i].name == name)
  19. {
  20. return this.cachedTextures[i];
  21. }
  22. }
  23. return null;
  24. },
  25. getP5ImageByName: function(name)
  26. {
  27. for (let i = 0; i < this.cachedTextures.length; i++)
  28. {
  29. if (this.cachedTextures[i].name == name)
  30. {
  31. return this.cachedTextures[i].P5Image;
  32. }
  33. }
  34. return null;
  35. },
  36. loadAudio: function(name, link)
  37. {
  38. let audio = createAudio(link);
  39. this.cachedAudio.push(new AudioRes(name, audio));
  40. },
  41. getAudioByName: function(name)
  42. {
  43. for (let i = 0; i < this.cachedAudio.length; i++)
  44. {
  45. if (this.cachedAudio[i].name == name)
  46. {
  47. return this.cachedAudio[i];
  48. }
  49. }
  50. return null;
  51. },
  52. getP5AudioByName: function(name)
  53. {
  54. for (let i = 0; i < this.cachedAudio.length; i++)
  55. {
  56. if (this.cachedAudio[i].name == name)
  57. {
  58. return this.cachedAudio[i].P5Audio;
  59. }
  60. }
  61. return null;
  62. },
  63. loadFont: function(name, link)
  64. {
  65. let fontRes = new FontRes(name, null);
  66. this.cachedFonts.push(fontRes);
  67. loadFont(link, font =>
  68. {
  69. fontRes.P5Font = font;
  70. })
  71. },
  72. getFontByName: function(name)
  73. {
  74. for (let i = 0; i < this.cachedFonts.length; i++)
  75. {
  76. if (this.cachedFonts[i].name == name)
  77. {
  78. return this.cachedFonts[i];
  79. }
  80. }
  81. return null;
  82. },
  83. getP5FontByName: function(name)
  84. {
  85. for (let i = 0; i < this.cachedFonts.length; i++)
  86. {
  87. if (this.cachedFonts[i].name == name)
  88. {
  89. return this.cachedFonts[i].P5Font;
  90. }
  91. }
  92. return null;
  93. }
  94. };