Add PieceHolder to display current pieces

This commit is contained in:
Lonami Exo 2017-01-24 13:45:19 +01:00
parent 560d9d2591
commit 60b0329d51
5 changed files with 80 additions and 4 deletions

View file

@ -11,7 +11,7 @@ public class Board {
private final int count; // Cell count private final int count; // Cell count
private final int size; // Size per cell private final int size; // Size per cell
private NinePatch cellPatch; public NinePatch cellPatch;
public Board(int boardSize, int cellSize) { public Board(int boardSize, int cellSize) {
count = boardSize; count = boardSize;
@ -28,6 +28,10 @@ public class Board {
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);
} }
public int getPxSize() {
return count * size;
}
private boolean inBounds(int x, int y) { private boolean inBounds(int x, int y) {
return x >= 0 && x < count && y >= 0 && y < count; return x >= 0 && x < count && y >= 0 && y < count;
} }
@ -59,9 +63,9 @@ public class Board {
return true; return true;
} }
public void draw(SpriteBatch batch) { public void draw(SpriteBatch batch, int x, int y) {
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, j * size, i * size, size); cells[i][j].draw(batch, cellPatch, x + j * size, y + i * size, size);
} }
} }

View file

@ -20,6 +20,10 @@ class Cell {
} }
void draw(SpriteBatch batch, NinePatch patch, int x, int y, int size) { void draw(SpriteBatch batch, NinePatch patch, int x, int y, int size) {
draw(color, batch, patch, x, y, size);
}
static void draw(Color color, SpriteBatch batch, NinePatch patch, int x, int y, int size) {
// TODO Use skin atlas // TODO Use skin atlas
batch.setColor(color); batch.setColor(color);
patch.draw(batch, x, y, size, size); patch.draw(batch, x, y, size, size);

View file

@ -1,6 +1,8 @@
package io.github.lonamiwebs.klooni.game; package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
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.MathUtils;
// Pieces can be L shaped and be rotated 0 to 3 times to make it random // Pieces can be L shaped and be rotated 0 to 3 times to make it random
@ -88,4 +90,14 @@ public class Piece {
} }
throw new RuntimeException("Random function is broken."); throw new RuntimeException("Random function is broken.");
} }
void draw(SpriteBatch batch, NinePatch patch, int x, int y, int size) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (shape[i][j]) {
Cell.draw(color, batch, patch, x + j * size, y + i * size, size);
}
}
}
}
} }

View file

@ -0,0 +1,44 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class PieceHolder {
final Piece[] pieces;
final int count;
public final int width, height;
public PieceHolder(int pieceCount, int holderWidth, int holderHeight) {
count = pieceCount;
pieces = new Piece[count];
width = holderWidth;
height = holderHeight;
takeMore();
}
void takeMore() {
for (int i = 0; i < count; i++) {
pieces[i] = Piece.random();
}
}
// TODO Scale evenly, and when taking the piece, scale to match the on-board cells' size
public void draw(SpriteBatch batch, NinePatch patch, int x, int y) {
int perPieceSize = width / 3;
for (int i = 0; i < count; i++) {
if (pieces[i] != null) {
// Pick the smallest value (either width/cell count, or height/cell count)
int cellSize = Math.min(
perPieceSize / pieces[i].width,
height / pieces[i].height
);
pieces[i].draw(batch, patch, x + i * perPieceSize, y, cellSize);
}
}
}
}

View file

@ -9,16 +9,21 @@ import com.badlogic.gdx.math.MathUtils;
import io.github.lonamiwebs.klooni.Klooni; import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.game.Board; import io.github.lonamiwebs.klooni.game.Board;
import io.github.lonamiwebs.klooni.game.Piece; import io.github.lonamiwebs.klooni.game.Piece;
import io.github.lonamiwebs.klooni.game.PieceHolder;
public class GameScreen implements Screen { public class GameScreen implements Screen {
private Klooni game; private Klooni game;
private Board board; private Board board;
private PieceHolder holder;
private SpriteBatch batch; private SpriteBatch batch;
public GameScreen(Klooni aGame) { public GameScreen(Klooni aGame) {
game = aGame; game = aGame;
board = new Board(10, 20); board = new Board(10, 20);
holder = new PieceHolder(3, Gdx.graphics.getWidth() / 2, 80);
batch = new SpriteBatch(); batch = new SpriteBatch();
// Fill some random pieces // Fill some random pieces
@ -38,7 +43,14 @@ public class GameScreen implements Screen {
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.draw(batch, board.cellPatch,
Gdx.graphics.getWidth() / 2 - holder.width / 2,
Gdx.graphics.getHeight() / 4);
batch.end(); batch.end();
} }