Loaded Factory - UQcsse3200/2024-studio-1 GitHub Wiki

Motivation

Many different factories were needing to "load" assets with the ResourceService, which lead to a lot of duplicate code e.g

ResourceService resourceService = ServiceLocator.getResourceService();
resourceService.loadTextures(... some list of filepaths ...);
resourceService.loadTextureAtlases(... some list of filepaths ...);
resourceService.loadSounds(... some list of filepaths ...);
resourceService.loadMusic(... some list of filepaths ...);

while (!resourceService.loadForMillis(20)) {
    logger.info("Loading... {}%", resourceService.getProgress());
}

This made it unclear when assets would be loaded or unloaded which marked a challenge for the stability of the program between runs, levels and rooms.

Implementation

Many factories were converted from static factories to instantiated factories, this makes the lifespan of the assets much clearer, when a factory is instantiated it's assets are loaded, when it is disposed it's assets are unloaded, they were also given different protected methods to register the assets they need to load, this is done by overriding the following methods:

    /**
     * Get all the filepaths to sounds needed by this Factory
     *
     * @return the filepaths needed.
     */
    protected String[] getSoundFilepaths();

    /**
     * Get all the filepaths to texture atlases needed by this Factory
     *
     * @return the filepaths needed.
     */
    protected String[] getTextureAtlasFilepaths();

    /**
     * Get all the filepaths to textures needed by this Factory
     *
     * @return the filepaths needed.
     */
    protected String[] getTextureFilepaths();

    /**
     * Get all the filepaths to music needed by this Factory
     *
     * @return the filepaths needed.
     */
    protected String[] getMusicFilepaths();

this can be implemented by sub classes in the following way (snippet from NPCFactory):

  @Override
  protected String[] getTextureFilepaths() {
    return new String[]{
            "images/ghost_1.png",
            "images/ghost_king.png",
            "images/rat.png",
            "images/minotaur.png",
            "images/dog.png",
            "images/snake.png",
            "images/dino.png",
            "images/minotaur.png",
            "images/bear.png"
    };
  }

Testing:

Testing for this class is primarily done through the logging, as each child class needs to manage it's own assets they can provide a logger to Loaded Factory so that loading can be included in that particular factory's logging and testing.

As for internal testing of the class, as it is an abstract class it's testing is primarily through it's derivatives, further specific testing may be created in the future.