Skip to content

Commit

Permalink
refactor: Move 3D asset loading back into GameLoadingScreen.
Browse files Browse the repository at this point in the history
  • Loading branch information
crykn committed Dec 27, 2023
1 parent d2f95b8 commit 42827cb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public void clearGame() {
});

DiscordGGHandler.instance().setMenuPresence();

// TODO: dispose the assets loaded in GameLoadingScreen?
}

@Override
Expand Down
18 changes: 3 additions & 15 deletions game/src/main/java/de/eskalon/gg/screens/AssetLoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import de.eskalon.commons.screens.AbstractAssetLoadingScreen;
import de.eskalon.commons.screens.EskalonScreenManager;
import de.eskalon.gg.core.ProjektGGApplicationContext;
import de.eskalon.gg.screens.game.GameLoadingScreen;
import de.eskalon.gg.simulation.model.factories.CharacterFactory;
import de.eskalon.gg.simulation.model.types.BuildingType;
import de.eskalon.gg.simulation.model.types.GameMap;
Expand All @@ -38,7 +39,8 @@
import de.eskalon.gg.thirdparty.DiscordGGHandler;

/**
* This screen takes care of loading the game's assets.
* This screen takes care of loading the game's assets with the exception of the
* 3D models. Those are loaded via the {@link GameLoadingScreen}.
*/
public class AssetLoadingScreen extends AbstractAssetLoadingScreen {

Expand Down Expand Up @@ -138,20 +140,6 @@ public void show() {
for (NPCCharacterTrait t : NPCCharacterTrait.values()) {
assetManager.load(t.getJSONAssetDescriptor());
}

// Load the game assets
// FIXME: Rework this, because the game data isn't loaded yet
assetManager.load("models/skybox/skybox.g3db", Model.class);
assetManager.load("models/buildings/test_houses/house1.g3db",
Model.class);
//@formatter:off
// for (GameMap t : GameMap.values()) {
// assetManager.load(t.getSkyboxPath(), Model.class);
// }
// for (BuildingType t : BuildingType.values()) {
// assetManager.load(t.getModelPath(), Model.class);
// }
//@formatter:on
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.eskalon.gg.screens.game;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
Expand All @@ -14,21 +13,25 @@
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import de.damios.guacamole.Stopwatch;
import de.damios.guacamole.gdx.log.Logger;
import de.damios.guacamole.gdx.log.LoggerService;
import de.eskalon.commons.asset.AnnotationAssetManager.Asset;
import de.eskalon.commons.inject.EskalonInjector;
import de.eskalon.commons.inject.annotations.Inject;
import de.eskalon.commons.misc.TaskExecutor;
import de.eskalon.commons.screens.AbstractEskalonUIScreen;
import de.eskalon.commons.screens.EskalonScreenManager;
import de.eskalon.gg.core.ProjektGGApplicationContext;
import de.eskalon.gg.graphics.rendering.GameRenderer;
import de.eskalon.gg.graphics.rendering.SelectableRenderData;
import de.eskalon.gg.simulation.model.World;
import de.eskalon.gg.simulation.model.entities.BuildingSlot;
import de.eskalon.gg.simulation.model.types.BuildingType;

/**
* This screen takes care of initializing the game world and the 3D scene.
* This screen takes care of loading the 3D models, initializing the game world
* and setting up the 3D scene.
*/
public class GameLoadingScreen extends AbstractEskalonUIScreen {

Expand All @@ -50,20 +53,37 @@ public class GameLoadingScreen extends AbstractEskalonUIScreen {

private static final Vector3 Y_AXIS = new Vector3(0, 1, 0);

private Queue<Runnable> taskQueue = new LinkedList<>();
private int taskCount;
private boolean once = false;
private TaskExecutor taskExecutor;
private boolean isDone = false;
private int loadingTicksPerSecond = 30;

private Stopwatch stopwatch;

@Override
public void show() {
super.show();

setImage(backgroundTexture);

taskExecutor = new TaskExecutor();
stopwatch = Stopwatch.createStarted();

World world = appContext.getGameHandler().getSimulation().getWorld();

/* Load game assets */
// This has to be done here, because the game data isn't loaded yet at
// the start of AssetLoadingScreen
assetManager.load(world.getMap().getSkyboxPath(), Model.class);

for (BuildingType t : BuildingType.values()) {
assetManager.load(t.getModelPath(), Model.class);
}

/* Init 3D scene */
// Create ModelInstances for the buildings
for (BuildingSlot s : world.getBuildingSlots()) {
if (s.isBuiltOn()) {
taskQueue.add(() -> {
taskExecutor.execute(() -> {
s.getBuilding().setRenderData(
new SelectableRenderData(assetManager.get(
s.getBuilding().getType().getModelPath(),
Expand All @@ -81,7 +101,7 @@ public void show() {
}
}
// Skybox
taskQueue.add(() -> {
taskExecutor.execute(() -> {
Model skyboxModel = assetManager.get(appContext.getGameHandler()
.getSimulation().getWorld().getMap().getSkyboxPath(),
Model.class);
Expand All @@ -94,39 +114,36 @@ public void show() {
});

// Final task: create the game renderer
taskQueue.add(() -> {
taskExecutor.execute(() -> {
appContext.setGameRenderer(
EskalonInjector.instance().getInstance(GameRenderer.class));
appContext.getGameRenderer().init();
});

taskCount = taskQueue.size();
}

@Override
public void render(float delta) {
viewport.apply();
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.begin();

// Execute stuff
Runnable task;
if ((task = taskQueue.poll()) != null) {
// TODO: this only executes one task per frame!
task.run();
} else if (!once) {
once = true;
LOG.info("[CLIENT] Game loading finished");
if (!isDone && assetManager.update(1000 / loadingTicksPerSecond)
&& taskExecutor.update(1000 / loadingTicksPerSecond)) {
isDone = true;
LOG.info("[CLIENT] Game loading finished in %d miliseconds",
stopwatch.getTime(TimeUnit.MILLISECONDS));
screenManager.pushScreen(RoundEndScreen.class, "simple_zoom");
}

float progress = Math.round(taskCount * 100F / taskQueue.size());
float progress = (assetManager.getProgress()
+ taskExecutor.getProgress()) / 2; // TODO: introduce a better
// approximation of progress

// Draw the background
batch.draw(this.backgroundTexture, 0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
// Render background
super.render(delta);

// Render Progress bar
viewport.apply();
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.begin();

// The actual drawing
batch.draw(bottomBarTexture,
(Gdx.graphics.getWidth() / 2) - (topBarTexture.getWidth() / 2)
+ 1,
Expand All @@ -140,4 +157,10 @@ public void render(float delta) {
batch.end();
}

@Override
public void resize(int width, int height) {
super.resize(width, height);
viewport.update(width, height, true);
}

}

0 comments on commit 42827cb

Please sign in to comment.