Allow dropping pieces on board

This commit is contained in:
Lonami Exo 2017-01-25 20:00:05 +01:00
parent c0fdc5ece8
commit 727338f407
4 changed files with 37 additions and 11 deletions

View file

@ -4,6 +4,8 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
public class Board { public class Board {
@ -11,10 +13,13 @@ public class Board {
private final int count; // Cell count private final int count; // Cell count
public final int cellSize; // Size per cell public final int cellSize; // Size per cell
final Vector2 pos;
public NinePatch cellPatch; public NinePatch cellPatch;
public Board(int boardSize, int cellSize) { public Board(float x, float y, int cellCount, int cellSize, boolean center) {
count = boardSize; pos = new Vector2(x, y);
count = cellCount;
this.cellSize = cellSize; this.cellSize = cellSize;
cells = new Cell[count][count]; cells = new Cell[count][count];
@ -26,6 +31,11 @@ public class Board {
cellPatch = new NinePatch( cellPatch = new NinePatch(
new Texture(Gdx.files.internal("ui/cells/basic.png")), 4, 4, 4, 4); 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() { public int getPxSize() {
@ -52,6 +62,15 @@ public class Board {
return true; 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) { public boolean putPiece(Piece piece, int x, int y) {
if (!canPutPiece(piece, x, y)) if (!canPutPiece(piece, x, y))
return false; return false;
@ -63,9 +82,10 @@ public class Board {
return true; 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 i = 0; i < count; i++)
for (int j = 0; j < count; j++) 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);
} }
} }

View file

@ -19,7 +19,7 @@ class Cell {
color = c; 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); draw(color, batch, patch, x, y, size);
} }

View file

@ -58,8 +58,11 @@ public class PieceHolder {
return false; return false;
} }
public boolean dropPiece() { public boolean dropPiece(Board board) {
if (heldPiece > -1) { if (heldPiece > -1) {
if (board.putScreenPiece(pieces[heldPiece])) {
// TODO Remove the piece
}
heldPiece = -1; heldPiece = -1;
return true; return true;
} }

View file

@ -22,7 +22,12 @@ public class GameScreen implements Screen, InputProcessor {
public GameScreen(Klooni aGame) { public GameScreen(Klooni aGame) {
game = 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) // PieceHolder(pieces, x, y, w, h)
int holderWidth = Gdx.graphics.getWidth() / 2; int holderWidth = Gdx.graphics.getWidth() / 2;
@ -51,9 +56,7 @@ public class GameScreen implements Screen, InputProcessor {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin(); batch.begin();
board.draw(batch, board.draw(batch);
Gdx.graphics.getWidth() / 2 - board.getPxSize() / 2,
Gdx.graphics.getHeight() * 3 / 4 - board.getPxSize() / 2);
holder.update(board.cellSize); holder.update(board.cellSize);
holder.draw(batch, board.cellPatch); holder.draw(batch, board.cellPatch);
@ -112,7 +115,7 @@ public class GameScreen implements Screen, InputProcessor {
@Override @Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) { public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return holder.dropPiece(); return holder.dropPiece(board);
} }
@Override @Override