From 5c9280962b8e36c739c4ff2d2fb2a482372c7c6e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 26 Jan 2017 19:26:39 +0100 Subject: [PATCH] Handle game layout better --- android/AndroidManifest.xml | 2 +- .../github/lonamiwebs/klooni/game/Board.java | 20 +++---- .../github/lonamiwebs/klooni/game/Cell.java | 3 +- .../lonamiwebs/klooni/game/GameLayout.java | 54 +++++++++++++++++++ .../lonamiwebs/klooni/game/PieceHolder.java | 23 ++++---- .../lonamiwebs/klooni/screens/GameScreen.java | 21 +++----- .../klooni/desktop/DesktopLauncher.java | 10 ++-- .../klooni/client/HtmlLauncher.java | 18 +++---- 8 files changed, 98 insertions(+), 53 deletions(-) create mode 100644 core/src/io/github/lonamiwebs/klooni/game/GameLayout.java diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 91617c8..ec0ff57 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -14,7 +14,7 @@ diff --git a/core/src/io/github/lonamiwebs/klooni/game/Board.java b/core/src/io/github/lonamiwebs/klooni/game/Board.java index 32a9534..f4cbad8 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/Board.java +++ b/core/src/io/github/lonamiwebs/klooni/game/Board.java @@ -10,17 +10,15 @@ import com.badlogic.gdx.math.Vector2; public class Board { Cell[][] cells; - public final int cellCount; // Cell count - public final int cellSize; // Size per cell + public final int cellCount; + public float cellSize; final Vector2 pos; public NinePatch cellPatch; - public Board(float x, float y, int cellCount, int cellSize, boolean center) { - pos = new Vector2(x, y); + public Board(final GameLayout layout, int cellCount) { this.cellCount = cellCount; - this.cellSize = cellSize; cells = new Cell[this.cellCount][this.cellCount]; for (int i = 0; i < this.cellCount; i++) { @@ -32,14 +30,8 @@ public class Board { cellPatch = new NinePatch( new Texture(Gdx.files.internal("ui/cells/basic.png")), 4, 4, 4, 4); - if (center) { - float half = getPxSize() / 2; - pos.sub(half, half); - } - } - - public int getPxSize() { - return cellCount * cellSize; + pos = new Vector2(); + layout.update(this); } private boolean inBounds(int x, int y) { @@ -87,7 +79,7 @@ public class Board { // The reason why we can't check first rows and then columns // (or vice versa) is because the following case (* filled, _ empty): // - // 4x4 board piece + // 4x4 boardHeight piece // _ _ * * * * // _ * * * * // * * _ _ diff --git a/core/src/io/github/lonamiwebs/klooni/game/Cell.java b/core/src/io/github/lonamiwebs/klooni/game/Cell.java index 6aa003a..1a571ee 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/Cell.java +++ b/core/src/io/github/lonamiwebs/klooni/game/Cell.java @@ -23,7 +23,7 @@ class Cell { color = Color.WHITE; } - void draw(SpriteBatch batch, NinePatch patch, float x, float y, int size) { + void draw(SpriteBatch batch, NinePatch patch, float x, float y, float size) { draw(color, batch, patch, x, y, size); } @@ -38,6 +38,7 @@ class Cell { return empty; } + // TODO Use vanish with a pretty animation instead .setEmpty() public void vanish() { empty = true; } diff --git a/core/src/io/github/lonamiwebs/klooni/game/GameLayout.java b/core/src/io/github/lonamiwebs/klooni/game/GameLayout.java new file mode 100644 index 0000000..071ae36 --- /dev/null +++ b/core/src/io/github/lonamiwebs/klooni/game/GameLayout.java @@ -0,0 +1,54 @@ +package io.github.lonamiwebs.klooni.game; + + +import com.badlogic.gdx.Gdx; + +// Helper class to calculate the size of each element +// +// TODO In a future, perhaps this could handle landscape mode differently +// For example, the boardHeight on the left and the piece holder on the right +public class GameLayout { + + // Widths + private float screenWidth, marginWidth; + + // Heights + private float screenHeight, logoHeight, scoreHeight, boardHeight, pieceHolderHeight; + + public GameLayout() { + calculate(); + } + + private void calculate() { + screenWidth = Gdx.graphics.getWidth(); + screenHeight = Gdx.graphics.getHeight(); + + // Widths + marginWidth = screenWidth * 0.05f; + + // Heights + logoHeight = screenHeight * 0.10f; + scoreHeight = screenHeight * 0.15f; + boardHeight = screenHeight * 0.50f; + pieceHolderHeight = screenHeight * 0.25f; + } + + // Note that we're now using Y-up coordinates + void update(Board board) { + // We can't leave our area, so pick the minimum between available + // height and width to determine an appropriated cell size + float availableWidth = screenWidth - marginWidth * 2f; + float boardSize = Math.min(availableWidth, boardHeight); + board.cellSize = boardSize / board.cellCount; + + // Now that we know the board size, we can center the board on the screen + board.pos.set(screenWidth * 0.5f - boardSize * 0.5f, pieceHolderHeight); + } + + void update(PieceHolder holder) { + float availableWidth = screenWidth - marginWidth * 2f; + holder.area.set( + marginWidth, 0f, + availableWidth, pieceHolderHeight); + } +} diff --git a/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java b/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java index a3496ce..f613fba 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java +++ b/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Interpolation; +import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; @@ -14,31 +15,31 @@ public class PieceHolder { int heldPiece; - final Vector2 pos; - final int width, height; + final Rectangle area; - public PieceHolder(int pieceCount, float x, float y, int holderWidth, int holderHeight) { + public PieceHolder(final GameLayout layout, final int pieceCount) { count = pieceCount; pieces = new Piece[count]; - heldPiece = -1; - pos = new Vector2(x, y); - width = holderWidth; - height = holderHeight; + area = new Rectangle(); + layout.update(this); + + // takeMore depends on the layout to be ready + // TODO So, how would pieces handle a layout update? takeMore(); } void takeMore() { - int perPieceSize = width / 3; + float perPieceSize = area.width / count; for (int i = 0; i < count; i++) { pieces[i] = Piece.random(); - // Set the local position and the cell cellSize - pieces[i].pos.set(pos.x + i * perPieceSize, pos.y); + // Set the absolute position on screen and the cells' cellSize + pieces[i].pos.set(area.x + i * perPieceSize, area.y); pieces[i].cellSize = Math.min( perPieceSize / pieces[i].cellCols, - height / pieces[i].cellRows); + area.height / pieces[i].cellRows); } } diff --git a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java index 0876eb6..faf54dc 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.math.MathUtils; import io.github.lonamiwebs.klooni.Klooni; import io.github.lonamiwebs.klooni.game.Board; +import io.github.lonamiwebs.klooni.game.GameLayout; import io.github.lonamiwebs.klooni.game.Piece; import io.github.lonamiwebs.klooni.game.PieceHolder; @@ -19,6 +20,8 @@ public class GameScreen implements Screen, InputProcessor { private Board board; private PieceHolder holder; + private final GameLayout layout; + private SpriteBatch batch; private final Color clearColor; @@ -28,21 +31,13 @@ public class GameScreen implements Screen, InputProcessor { game = aGame; score = 0; clearColor = new Color(0.9f, 0.9f, 0.7f, 1f); - - // Board(x, y, cell cellCount, cell size, center) - board = new Board( - Gdx.graphics.getWidth() / 2, - Gdx.graphics.getHeight() * 3 / 4, - 10, 20, true); - - // PieceHolder(pieces, x, y, w, h) - int holderWidth = Gdx.graphics.getWidth() / 2; - holder = new PieceHolder(3, - Gdx.graphics.getWidth() / 2 - holderWidth / 2, Gdx.graphics.getHeight() / 4, - Gdx.graphics.getWidth() / 2, 80); - batch = new SpriteBatch(); + layout = new GameLayout(); + + board = new Board(layout, 10); + holder = new PieceHolder(layout, 3); + // Fill some random pieces for (int i = 0; i < 10; i++) { board.putPiece(Piece.random(), MathUtils.random(10), MathUtils.random(10)); diff --git a/desktop/src/io/github/lonamiwebs/klooni/desktop/DesktopLauncher.java b/desktop/src/io/github/lonamiwebs/klooni/desktop/DesktopLauncher.java index a658975..3ef230b 100644 --- a/desktop/src/io/github/lonamiwebs/klooni/desktop/DesktopLauncher.java +++ b/desktop/src/io/github/lonamiwebs/klooni/desktop/DesktopLauncher.java @@ -5,8 +5,10 @@ import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import io.github.lonamiwebs.klooni.Klooni; public class DesktopLauncher { - public static void main (String[] arg) { - LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); - new LwjglApplication(new Klooni(), config); - } + public static void main (String[] arg) { + LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); + config.width = 408; + config.height = 680; + new LwjglApplication(new Klooni(), config); + } } diff --git a/html/src/io/github/lonamiwebs/klooni/client/HtmlLauncher.java b/html/src/io/github/lonamiwebs/klooni/client/HtmlLauncher.java index d2ab7c4..e6eb718 100644 --- a/html/src/io/github/lonamiwebs/klooni/client/HtmlLauncher.java +++ b/html/src/io/github/lonamiwebs/klooni/client/HtmlLauncher.java @@ -7,13 +7,13 @@ import io.github.lonamiwebs.klooni.Klooni; public class HtmlLauncher extends GwtApplication { - @Override - public GwtApplicationConfiguration getConfig () { - return new GwtApplicationConfiguration(480, 320); - } + @Override + public GwtApplicationConfiguration getConfig () { + return new GwtApplicationConfiguration(408, 680); + } - @Override - public ApplicationListener createApplicationListener () { - return new Klooni(); - } -} \ No newline at end of file + @Override + public ApplicationListener createApplicationListener () { + return new Klooni(); + } +}