Class ResourceService

java.lang.Object
com.csse3200.game.services.ResourceService
All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable

public class ResourceService extends Object implements com.badlogic.gdx.utils.Disposable
Service for loading resources, e.g. textures, texture atlases, sounds, music, etc. Add new load methods when new types of resources are added to the game.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initialise this ResourceService to use the default AssetManager.
    ResourceService(com.badlogic.gdx.assets.AssetManager assetManager)
    Initialise this ResourceService to use the provided AssetManager.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears all loaded assets and assets in the preloading queue.
    <T> boolean
    containsAsset(String resourceName, Class<T> type)
    Check if an asset has been loaded already
    boolean
    Check if a FreeType font generator has been loaded for the given path.
    void
     
    com.badlogic.gdx.graphics.g2d.BitmapFont
    generateFreeTypeFont(String key, int size)
    Generates a BitmapFont from a loaded FreeType font generator.
    com.badlogic.gdx.graphics.g2d.BitmapFont
    generateFreeTypeFont(String key, int size, com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter parameter)
    Generates a BitmapFont from a loaded FreeType font generator with custom parameters.
    <T> T
    getAsset(String filename, Class<T> type)
    Load an asset from a file.
    int
    Returns the loading completion progress as a percentage.
    void
    Blocking call to load all assets.
    void
    loadFont(String fontPath, String key)
    Loads FreeType font generators from TTF files.
    boolean
    loadForMillis(int duration)
    Loads assets for the specified duration in milliseconds.
    void
    loadMusic(String[] musicNames)
    Loads a list of music assets into the asset manager.
    void
    loadSounds(String[] soundNames)
    Loads a list of sounds into the asset manager.
    void
    loadTextureAtlases(String[] textureAtlasNames)
    Loads a list of texture atlas assets into the asset manager.
    void
    loadTextures(String[] textureNames)
    Loads a list of texture assets into the asset manager.
    void
    unloadAssets(String[] assetNames)
    Unloads a list of assets from the asset manager.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResourceService

      public ResourceService()
      Initialise this ResourceService to use the default AssetManager.
    • ResourceService

      public ResourceService(com.badlogic.gdx.assets.AssetManager assetManager)
      Initialise this ResourceService to use the provided AssetManager.
      Parameters:
      assetManager - AssetManager to use in this service.
      Requires:
      assetManager != null
  • Method Details

    • getAsset

      public <T> T getAsset(String filename, Class<T> type)
      Load an asset from a file.
      Type Parameters:
      T - Type of class to load into
      Parameters:
      filename - Asset path
      type - Class to load into
      Returns:
      Instance of class loaded from path
      See Also:
      • AssetManager.get(String, Class)
    • containsAsset

      public <T> boolean containsAsset(String resourceName, Class<T> type)
      Check if an asset has been loaded already
      Type Parameters:
      T - Type of the asset
      Parameters:
      resourceName - path of the asset
      type - Class type of the asset
      Returns:
      true if asset has been loaded, false otherwise
      See Also:
      • AssetManager.contains(String)
    • getProgress

      public int getProgress()
      Returns the loading completion progress as a percentage.
      Returns:
      progress
    • loadAll

      public void loadAll()
      Blocking call to load all assets.
      See Also:
      • AssetManager.finishLoading()
    • loadForMillis

      public boolean loadForMillis(int duration)
      Loads assets for the specified duration in milliseconds.
      Parameters:
      duration - duration to load for
      Returns:
      finished loading
      See Also:
      • AssetManager.update(int)
    • clearAllAssets

      public void clearAllAssets()
      Clears all loaded assets and assets in the preloading queue.
      See Also:
      • AssetManager.clear()
    • loadTextures

      public void loadTextures(String[] textureNames)
      Loads a list of texture assets into the asset manager.
      Parameters:
      textureNames - texture filenames
    • loadTextureAtlases

      public void loadTextureAtlases(String[] textureAtlasNames)
      Loads a list of texture atlas assets into the asset manager.
      Parameters:
      textureAtlasNames - texture atlas filenames
    • loadSounds

      public void loadSounds(String[] soundNames)
      Loads a list of sounds into the asset manager.
      Parameters:
      soundNames - sound filenames
    • loadMusic

      public void loadMusic(String[] musicNames)
      Loads a list of music assets into the asset manager.
      Parameters:
      musicNames - music filenames
    • loadFont

      public void loadFont(String fontPath, String key)
      Loads FreeType font generators from TTF files. This allows for dynamic font generation with custom sizes and parameters.
      Parameters:
      fontPath - path to the TTF font file
      key - key to store the font generator
    • generateFreeTypeFont

      public com.badlogic.gdx.graphics.g2d.BitmapFont generateFreeTypeFont(String key, int size)
      Generates a BitmapFont from a loaded FreeType font generator.
      Parameters:
      key - key to the font generator
      size - font size in pixels
      Returns:
      generated BitmapFont, or null if the font generator is not loaded
    • generateFreeTypeFont

      public com.badlogic.gdx.graphics.g2d.BitmapFont generateFreeTypeFont(String key, int size, com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter parameter)
      Generates a BitmapFont from a loaded FreeType font generator with custom parameters.
      Parameters:
      key - key to the font generator
      size - font size in pixels
      parameter - custom font generation parameters (can be null for defaults)
      Returns:
      generated BitmapFont, or null if the font generator is not loaded
    • containsFreeTypeFont

      public boolean containsFreeTypeFont(String key)
      Check if a FreeType font generator has been loaded for the given path.
      Parameters:
      key - key to the font generator
      Returns:
      true if the font generator is loaded, false otherwise
    • unloadAssets

      public void unloadAssets(String[] assetNames)
      Unloads a list of assets from the asset manager.
      Parameters:
      assetNames - list of asset names
    • dispose

      public void dispose()
      Specified by:
      dispose in interface com.badlogic.gdx.utils.Disposable