Skip to content

Commit

Permalink
LoadingScreen updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaganx0 committed Oct 7, 2023
1 parent f970693 commit b9a5fcd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
62 changes: 47 additions & 15 deletions source/core/src/main/com/csse3200/game/screens/LoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,85 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.async.AsyncExecutor;
import com.badlogic.gdx.utils.async.AsyncTask;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.csse3200.game.GdxGame;
import com.csse3200.game.services.ServiceLocator;

public class LoadingScreen implements Screen {
private final GdxGame game;
private SpriteBatch spriteBatch;
private Texture backgroundTexture; // Background image
private Texture loadingTexture; // Loading animation
private AsyncExecutor asyncExecutor = new AsyncExecutor(1);

private float elapsedTime = 0;
private boolean transitionToMainGame = false;
private Stage stage;
private Label loadingLabel;
private float dotTimer;
public LoadingScreen(GdxGame game) {
this.game = game;
spriteBatch = new SpriteBatch();
backgroundTexture = new Texture("planets/background.png");
loadingTexture = new Texture("images/ui/Sprites/UI_Glass_Scrollbar_01a.png");

// loadingTexture = new Texture("images/mobboss/patrick.png");
stage = new Stage(new ScreenViewport());
Skin skin = new Skin(Gdx.files.internal("uiskin.json")); // Use your own skin file
loadingLabel = new Label("Loading", skin);
loadingLabel.setPosition(Gdx.graphics.getWidth() / 2 - 50, Gdx.graphics.getHeight() / 2);
stage.addActor(loadingLabel);
}

@Override
public void show() {
AssetLoader.loadAllAssets();

}

@Override
public void render(float delta) {
elapsedTime += delta;

// Clear the screen
spriteBatch.begin();
spriteBatch.begin();

// Draw the background image
spriteBatch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

// Draw the loading animation on top of the background
spriteBatch.draw(loadingTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// spriteBatch.draw(loadingTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

spriteBatch.end();
spriteBatch.end();

if (AssetLoader.areAllAssetsLoaded()) {
// Asset loading is complete, transition to the main game screen
if (AssetLoader.areAllAssetsLoaded() || elapsedTime >= 5) {
transitionToMainGame = true;
}

if (transitionToMainGame) {
// Transition to the main game screen
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
}
dotTimer += delta;
int numDots = (int) (dotTimer % 1 * 3); // Add a dot every second

// Add dots to the label text
StringBuilder labelText = new StringBuilder("Loading");
for (int i = 0; i < numDots; i++) {
labelText.append('.');
}
loadingLabel.setText(labelText);
stage.act();
stage.draw();
}

@Override
public void resize(int width, int height) {
// Resize any necessary assets or elements here
stage.getViewport().update(width, height, true);
}

@Override
Expand All @@ -72,6 +104,6 @@ public void hide() {
public void dispose() {
spriteBatch.dispose();
backgroundTexture.dispose();
loadingTexture.dispose();
// loadingTexture.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void clicked(InputEvent event, float x, float y) {
public void clicked(InputEvent event, float x, float y) {
// Store the selected towers in the ServiceLocator for transferring across screens
ServiceLocator.setTowerTypes(selectedTurrets);;
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
game.setScreen(GdxGame.ScreenType.LOAD_SCREEN);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void clearAllAssets() {
* @param type asset type
* @param <T> type
*/
private <T> void loadAsset(String assetName, Class<T> type) {
public <T> void loadAsset(String assetName, Class<T> type) {
logger.debug("Loading {}: {}", type.getSimpleName(), assetName);
try {
assetManager.load(assetName, type);
Expand Down Expand Up @@ -187,4 +187,6 @@ public void unloadAssets(String[] assetNames) {
public void dispose() {
assetManager.clear();
}


}

0 comments on commit b9a5fcd

Please sign in to comment.