Skip to content

Commit

Permalink
LoadingScreen update
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaganx0 committed Oct 7, 2023
1 parent 8cfb8af commit f970693
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
49 changes: 26 additions & 23 deletions source/core/src/main/com/csse3200/game/screens/AssetLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void>) () -> {
loadAllAssets();
return null;
});
}
public class AssetLoader {
private static final Set<String> loadedAssets = new HashSet<>();
// Define your asset file paths here
public static final String[] textures = {
"images/desert_bg.png",
Expand Down Expand Up @@ -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
}
Expand All @@ -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);
}

}

15 changes: 4 additions & 11 deletions source/core/src/main/com/csse3200/game/screens/LoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Void>() {
@Override
public Void call() {
AssetLoader.loadAllAssets();
return null;
}
});
AssetLoader.loadAllAssets();
}

@Override
Expand All @@ -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);
}
}
Expand Down

0 comments on commit f970693

Please sign in to comment.