summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2018-06-27 00:18:54 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2018-06-28 16:24:42 +0200
commitb6f07387af85687bf957e3e75e664be5ce972bd4 (patch)
treed80d10fd85e911d1a633e5b59735ec6a8532223f
parentfb5a31554b5dc909d5de25eae306d6033d25c907 (diff)
downloadgdx-boardgame-b6f07387af85687bf957e3e75e664be5ce972bd4.zip
gdx-boardgame-b6f07387af85687bf957e3e75e664be5ce972bd4.tar.gz
implement LoadingScreen
-rw-r--r--README.md2
-rw-r--r--android/assets/data/loading.atlas34
-rw-r--r--android/assets/data/loading.pngbin0 -> 14501 bytes
-rw-r--r--core/src/ch/asynk/zproject/Assets.java11
-rw-r--r--core/src/ch/asynk/zproject/ZProject.java16
-rw-r--r--core/src/ch/asynk/zproject/screens/LoadingScreen.java171
6 files changed, 229 insertions, 5 deletions
diff --git a/README.md b/README.md
index b28f3ba..65e305b 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,5 @@
# TODO
- - LoadingScreen
- Fonts
- UI widgets
- SquareMap
@@ -13,3 +12,4 @@
- GameScreen
- BoardCamera
- Assets
+ - LoadingScreen
diff --git a/android/assets/data/loading.atlas b/android/assets/data/loading.atlas
new file mode 100644
index 0000000..cb8c853
--- /dev/null
+++ b/android/assets/data/loading.atlas
@@ -0,0 +1,34 @@
+
+loading.png
+size: 1024,128
+format: RGBA8888
+filter: Nearest,Nearest
+repeat: none
+bar
+ rotate: false
+ xy: 2, 18
+ size: 692, 27
+ orig: 692, 27
+ offset: 0, 0
+ index: -1
+border
+ rotate: false
+ xy: 2, 47
+ size: 700, 36
+ orig: 700, 36
+ offset: 0, 0
+ index: -1
+loaded
+ rotate: false
+ xy: 704, 69
+ size: 276, 14
+ orig: 276, 14
+ offset: 0, 0
+ index: -1
+loading
+ rotate: false
+ xy: 2, 2
+ size: 130, 14
+ orig: 130, 14
+ offset: 0, 0
+ index: -1
diff --git a/android/assets/data/loading.png b/android/assets/data/loading.png
new file mode 100644
index 0000000..c68fdd7
--- /dev/null
+++ b/android/assets/data/loading.png
Binary files differ
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;
+ }
+}