diff options
Diffstat (limited to 'core/src/ch/asynk/zproject')
-rw-r--r-- | core/src/ch/asynk/zproject/Assets.java | 11 | ||||
-rw-r--r-- | core/src/ch/asynk/zproject/ZProject.java | 16 | ||||
-rw-r--r-- | core/src/ch/asynk/zproject/screens/LoadingScreen.java | 171 |
3 files changed, 194 insertions, 4 deletions
diff --git a/core/src/ch/asynk/zproject/Assets.java b/core/src/ch/asynk/zproject/Assets.java index 3eadf9f..8584810 100644 --- a/core/src/ch/asynk/zproject/Assets.java +++ b/core/src/ch/asynk/zproject/Assets.java @@ -9,6 +9,7 @@ public class Assets extends AssetManager implements Disposable { public static final String CORNER = "data/corner.png"; public static final String MAP_00 = "data/map_00.png"; + public static final String LOADING = "data/loading.atlas"; @Override public void dispose() { @@ -27,6 +28,16 @@ public class Assets extends AssetManager implements Disposable return get(assetName, TextureAtlas.class); } + public void loadLoading() + { + load(LOADING, TextureAtlas.class); + } + + public void unloadLoading() + { + unload(LOADING); + } + public void loadGame() { load(MAP_00, Texture.class); diff --git a/core/src/ch/asynk/zproject/ZProject.java b/core/src/ch/asynk/zproject/ZProject.java index 12dc982..7afca28 100644 --- a/core/src/ch/asynk/zproject/ZProject.java +++ b/core/src/ch/asynk/zproject/ZProject.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import ch.asynk.zproject.screens.GameScreen; +import ch.asynk.zproject.screens.LoadingScreen; public class ZProject extends Game { @@ -13,7 +14,8 @@ public class ZProject extends Game private enum State { NONE, - GAME + LOADING, + GAME, } private State state; @@ -24,7 +26,7 @@ public class ZProject extends Game this.state = State.NONE; Gdx.app.setLogLevel(Gdx.app.LOG_DEBUG); debug(String.format("create() [%d;%d] %f", Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Gdx.graphics.getDensity())); - switchToGame(); + switchToLoading(); } @Override public void dispose() @@ -55,6 +57,7 @@ public class ZProject extends Game return; } switch(state) { + case LOADING: assets.unloadLoading(); break; case GAME: assets.unloadGame(); break; } if (state != State.NONE) { @@ -69,10 +72,15 @@ public class ZProject extends Game switchTo(null, State.NONE); } - public void switchToGame() + public void switchToLoading() { - assets.loadGame(); + assets.loadLoading(); assets.finishLoading(); + switchTo(new LoadingScreen(this, () -> assets.loadGame(), () -> switchToGame()), State.LOADING); + } + + public void switchToGame() + { switchTo(new GameScreen(this), State.GAME); } } diff --git a/core/src/ch/asynk/zproject/screens/LoadingScreen.java b/core/src/ch/asynk/zproject/screens/LoadingScreen.java new file mode 100644 index 0000000..154cc11 --- /dev/null +++ b/core/src/ch/asynk/zproject/screens/LoadingScreen.java @@ -0,0 +1,171 @@ +package ch.asynk.zproject.screens; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Screen; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion; + +import ch.asynk.zproject.ZProject; + +public class LoadingScreen implements Screen +{ + private final ZProject zproject; + private final SpriteBatch batch; + private final Color c; + private final OrthographicCamera camera; + + private final float DELAY_AFTER = 1f; + private final float ALPHA_FACTOR = 1.5f; + + private boolean paused; + private TextureAtlas atlas; + private AtlasRegion bar; + private AtlasRegion border; + private AtlasRegion loading; + private AtlasRegion loaded; + + private int x, y; + private float acc; + private float percent; + private float alpha; + private boolean incr; + private boolean isLoaded; + + public interface LoadAction { + void call(); + } + private LoadAction onLoaded; + + public LoadingScreen(final ZProject zproject, LoadAction startLoading, LoadAction onLoaded) + { + this.zproject = zproject; + this.onLoaded = onLoaded; + this.batch = new SpriteBatch(); + this.c = batch.getColor(); + this.camera = new OrthographicCamera(); + int w = Gdx.graphics.getWidth(); + int h = Gdx.graphics.getHeight(); + this.camera.setToOrtho(false, w, h); + this.camera.update(); + this.paused = false; + atlas = zproject.assets.getAtlas(zproject.assets.LOADING); + bar = atlas.findRegion("bar"); + border = atlas.findRegion("border"); + loading = atlas.findRegion("loading"); + loaded = atlas.findRegion("loaded"); + computeCoords(w, h); + percent = 0f; + alpha = 1f; + incr = false; + isLoaded = false; + startLoading.call(); + } + + private void computeCoords(int width, int height) + { + x = (width - border.getRegionWidth()) / 2; + y = (height - border.getRegionHeight()) / 2; + } + + @Override public void render(float delta) + { + if (paused) return; + + percent = zproject.assets.getProgress(); + if (!isLoaded && percent >= 1f) { + acc = 0f; + alpha = 1f; + incr = false; + } + + if (isLoaded) { + acc += delta; + if (acc >= DELAY_AFTER) { + // FIXME callback + // zproject.switchToGame(); + onLoaded.call(); + return; + } + } + + delta *= ALPHA_FACTOR; + if (incr) { + alpha += delta; + if (alpha >= 1f ) { + alpha = 1f; + incr = false; + } + } else { + alpha -= delta; + if (alpha <= 0f ) { + alpha = 0f; + incr = true; + } + } + + if (!isLoaded) { + if (zproject.assets.update()) { + ZProject.debug("LoadingScreen", "assets loaded"); + isLoaded = true; + } + } + + Gdx.gl.glClearColor(0, 0, 0, 1); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + batch.setProjectionMatrix(camera.combined); + batch.begin(); + batch.setColor(c.r, c.g, c.b, 1f); + batch.draw(border, x, y); + batch.draw(bar, x + 4, y + 4, percent * bar.getRegionWidth(), bar.getRegionHeight()); + batch.setColor(c.r, c.g, c.b, alpha); + if (!isLoaded) { + batch.draw(loading, x, y + border.getRegionHeight() + 3); + } else { + batch.draw(loaded, x + border.getRegionWidth() - loaded.getRegionWidth(), y - loaded.getRegionHeight() - 3); + } + batch.end(); + } + + @Override public void resize(int width, int height) + { + if (paused) return; + ZProject.debug("LoadingScreen", String.format("resize (%d,%d)",width, height)); + this.camera.setToOrtho(false, width, height); + this.camera.update(); + computeCoords(width, height); + } + + @Override public void dispose() + { + ZProject.debug("LoadingScreen", "dispose()"); + batch.dispose(); + } + + @Override public void show() + { + ZProject.debug("LoadingScreen", "show()"); + } + + @Override public void hide() + { + ZProject.debug("LoadingScreen", "hide()"); + } + + @Override public void pause() + { + paused = true; + ZProject.debug("pause() "); + } + + @Override public void resume() + { + ZProject.debug("resume() "); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + paused = false; + } +} |