From f97069325358b0b4cf795325533db35296e616f8 Mon Sep 17 00:00:00 2001 From: Gaganx0 Date: Sat, 7 Oct 2023 16:40:25 +1000 Subject: [PATCH] LoadingScreen update --- .../csse3200/game/screens/AssetLoader.java | 49 ++++++++++--------- .../csse3200/game/screens/LoadingScreen.java | 15 ++---- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/screens/AssetLoader.java b/source/core/src/main/com/csse3200/game/screens/AssetLoader.java index fd7913569..cda84a02e 100644 --- a/source/core/src/main/com/csse3200/game/screens/AssetLoader.java +++ b/source/core/src/main/com/csse3200/game/screens/AssetLoader.java @@ -9,15 +9,10 @@ import com.csse3200.game.services.ResourceService; import com.csse3200.game.services.ServiceLocator; -public class AssetLoader { - private final AsyncExecutor asyncExecutor = new AsyncExecutor(1); +import java.util.*; - public void loadAllAssetsInBackground() { - asyncExecutor.submit((AsyncTask) () -> { - loadAllAssets(); - return null; - }); - } +public class AssetLoader { + private static final Set loadedAssets = new HashSet<>(); // Define your asset file paths here public static final String[] textures = { "images/desert_bg.png", @@ -210,12 +205,21 @@ public void loadAllAssetsInBackground() { public static void loadAllAssets() { ResourceService resourceService = ServiceLocator.getResourceService(); - resourceService.loadTextures(textures); - resourceService.loadTextureAtlases(textureAtlases); - resourceService.loadSounds(Sounds); - resourceService.loadMusic(music); + for (String assetPath : textures) { + resourceService.loadAsset(assetPath, Texture.class); + } - // Wait for the assets to finish loading (you can implement a loading screen) + for (String assetPath : textureAtlases) { + resourceService.loadAsset(assetPath, TextureAtlas.class); + } + + for (String assetPath : Sounds) { + resourceService.loadAsset(assetPath, Sound.class); + } + + for (String assetPath : music) { + resourceService.loadAsset(assetPath, Music.class); + } while (!resourceService.loadForMillis(10)) { // Display loading progress if needed } @@ -230,20 +234,19 @@ public static void unloadAllAssets() { resourceService.unloadAssets(music); } - public static Texture getTexture(String assetPath) { - return ServiceLocator.getResourceService().getAsset(assetPath, Texture.class); - } + public static boolean areAllAssetsLoaded() { + ResourceService resourceService = ServiceLocator.getResourceService(); - public static TextureAtlas getTextureAtlas(String assetPath) { - return ServiceLocator.getResourceService().getAsset(assetPath, TextureAtlas.class); + return loadedAssets.containsAll(Arrays.asList(textures)) && + loadedAssets.containsAll(Arrays.asList(textureAtlases)) && + loadedAssets.containsAll(Arrays.asList(Sounds)) && + loadedAssets.containsAll(Arrays.asList(music)); } - public static Sound getSound(String assetPath) { - return ServiceLocator.getResourceService().getAsset(assetPath, Sound.class); + public static void onAssetLoaded(String assetPath) { + loadedAssets.add(assetPath); } - public static Music getMusic(String assetPath) { - return ServiceLocator.getResourceService().getAsset(assetPath, Music.class); - } + } diff --git a/source/core/src/main/com/csse3200/game/screens/LoadingScreen.java b/source/core/src/main/com/csse3200/game/screens/LoadingScreen.java index 7a409930e..e9cb8afa2 100644 --- a/source/core/src/main/com/csse3200/game/screens/LoadingScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/LoadingScreen.java @@ -8,7 +8,6 @@ import com.badlogic.gdx.utils.async.AsyncExecutor; import com.badlogic.gdx.utils.async.AsyncTask; import com.csse3200.game.GdxGame; -import com.csse3200.game.screens.AssetLoader; import com.csse3200.game.services.ServiceLocator; public class LoadingScreen implements Screen { @@ -21,20 +20,13 @@ public class LoadingScreen implements Screen { public LoadingScreen(GdxGame game) { this.game = game; spriteBatch = new SpriteBatch(); - backgroundTexture = new Texture("planets/background.png"); // Replace with your background image + backgroundTexture = new Texture("planets/background.png"); loadingTexture = new Texture("images/ui/Sprites/UI_Glass_Scrollbar_01a.png"); } @Override public void show() { - // Start loading assets in the background thread - asyncExecutor.submit(new AsyncTask() { - @Override - public Void call() { - AssetLoader.loadAllAssets(); - return null; - } - }); + AssetLoader.loadAllAssets(); } @Override @@ -50,7 +42,8 @@ public void render(float delta) { spriteBatch.end(); - if (ServiceLocator.getResourceService().loadForMillis(2000)) { + if (AssetLoader.areAllAssetsLoaded()) { + // Asset loading is complete, transition to the main game screen game.setScreen(GdxGame.ScreenType.MAIN_GAME); } }