From 727338f4076b47792d1270eb29127d67d48c53b4 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 25 Jan 2017 20:00:05 +0100 Subject: [PATCH] Allow dropping pieces on board --- .../github/lonamiwebs/klooni/game/Board.java | 28 ++++++++++++++++--- .../github/lonamiwebs/klooni/game/Cell.java | 2 +- .../lonamiwebs/klooni/game/PieceHolder.java | 5 +++- .../lonamiwebs/klooni/screens/GameScreen.java | 13 +++++---- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/core/src/io/github/lonamiwebs/klooni/game/Board.java b/core/src/io/github/lonamiwebs/klooni/game/Board.java index b6cb911..6bbb39d 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/Board.java +++ b/core/src/io/github/lonamiwebs/klooni/game/Board.java @@ -4,6 +4,8 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.math.Vector2; public class Board { @@ -11,10 +13,13 @@ public class Board { private final int count; // Cell count public final int cellSize; // Size per cell + final Vector2 pos; + public NinePatch cellPatch; - public Board(int boardSize, int cellSize) { - count = boardSize; + public Board(float x, float y, int cellCount, int cellSize, boolean center) { + pos = new Vector2(x, y); + count = cellCount; this.cellSize = cellSize; cells = new Cell[count][count]; @@ -26,6 +31,11 @@ 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() { @@ -52,6 +62,15 @@ public class Board { return true; } + public boolean putScreenPiece(Piece piece) { + // Get the local piece coordinates + // TODO Works weird, it puts the piece like one too low… + Vector2 local = piece.pos.cpy().sub(pos); + int x = MathUtils.round(local.x / piece.cellSize); + int y = MathUtils.round(local.y / piece.cellSize); + return putPiece(piece, x, y); + } + public boolean putPiece(Piece piece, int x, int y) { if (!canPutPiece(piece, x, y)) return false; @@ -63,9 +82,10 @@ public class Board { return true; } - public void draw(SpriteBatch batch, int x, int y) { + public void draw(SpriteBatch batch) { for (int i = 0; i < count; i++) for (int j = 0; j < count; j++) - cells[i][j].draw(batch, cellPatch, x + j * cellSize, y + i * cellSize, cellSize); + cells[i][j].draw(batch, cellPatch, + pos.x + j * cellSize, pos.y + i * cellSize, cellSize); } } diff --git a/core/src/io/github/lonamiwebs/klooni/game/Cell.java b/core/src/io/github/lonamiwebs/klooni/game/Cell.java index bd728d3..ff6c559 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/Cell.java +++ b/core/src/io/github/lonamiwebs/klooni/game/Cell.java @@ -19,7 +19,7 @@ class Cell { color = c; } - void draw(SpriteBatch batch, NinePatch patch, int x, int y, int size) { + void draw(SpriteBatch batch, NinePatch patch, float x, float y, int size) { draw(color, batch, patch, x, y, size); } diff --git a/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java b/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java index 5608885..2cbbe3e 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java +++ b/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java @@ -58,8 +58,11 @@ public class PieceHolder { return false; } - public boolean dropPiece() { + public boolean dropPiece(Board board) { if (heldPiece > -1) { + if (board.putScreenPiece(pieces[heldPiece])) { + // TODO Remove the piece + } heldPiece = -1; return true; } diff --git a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java index d48ff94..17d3a6f 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java @@ -22,7 +22,12 @@ public class GameScreen implements Screen, InputProcessor { public GameScreen(Klooni aGame) { game = aGame; - board = new Board(10, 20); + + // Board(x, y, cell count, 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; @@ -51,9 +56,7 @@ public class GameScreen implements Screen, InputProcessor { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); - board.draw(batch, - Gdx.graphics.getWidth() / 2 - board.getPxSize() / 2, - Gdx.graphics.getHeight() * 3 / 4 - board.getPxSize() / 2); + board.draw(batch); holder.update(board.cellSize); holder.draw(batch, board.cellPatch); @@ -112,7 +115,7 @@ public class GameScreen implements Screen, InputProcessor { @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { - return holder.dropPiece(); + return holder.dropPiece(board); } @Override