Update TechnicClient.java - Added screen cache
This commit is contained in:
@@ -6,21 +6,26 @@ import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
|
||||
import com.badlogic.gdx.utils.GdxRuntimeException;
|
||||
import com.ghost.technic.screen.BattlefieldScreen;
|
||||
import com.ghost.technic.server.Player;
|
||||
import com.ghost.technic.server.game.GameActionService;
|
||||
import com.ghost.technic.server.game.GameState;
|
||||
import com.ghost.technic.server.game.event.EventBus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
|
||||
public class TechnicClient extends Game {
|
||||
|
||||
private Skin skin;
|
||||
private Screen currentScreen;
|
||||
private AssetManager assetManager;
|
||||
private GameState gameState;
|
||||
private GameActionService gameActionService;
|
||||
|
||||
private final Map<Class<? extends Screen>, Screen> screenCache = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
|
||||
@@ -38,29 +43,29 @@ public class TechnicClient extends Game {
|
||||
gameState.setPlayers(playerOne, playerTwo);
|
||||
gameState.setup();
|
||||
|
||||
currentScreen = new BattlefieldScreen(skin); // store reference
|
||||
currentScreen.show();
|
||||
// screen
|
||||
addScreen(new BattlefieldScreen(skin));
|
||||
setScreen(BattlefieldScreen.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
if (currentScreen != null) {
|
||||
currentScreen.render(Gdx.graphics.getDeltaTime());
|
||||
}
|
||||
public void addScreen(Screen screen) {
|
||||
screenCache.put(screen.getClass(), screen);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
if (currentScreen != null) {
|
||||
currentScreen.resize(width, height);
|
||||
public void setScreen(Class<? extends Screen> screenClass) {
|
||||
var screen = screenCache.get(screenClass);
|
||||
if (screen == null) {
|
||||
throw new GdxRuntimeException("Screen " + screenClass.getSimpleName() + " not found in cache");
|
||||
}
|
||||
super.setScreen(screen);
|
||||
}
|
||||
|
||||
public void removeScreen(Screen screen) {
|
||||
screenCache.remove(screen.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
if (currentScreen != null) {
|
||||
currentScreen.dispose();
|
||||
}
|
||||
skin.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user