Allow picking up a piece
This commit is contained in:
parent
60b0329d51
commit
5fd653feb2
5 changed files with 148 additions and 36 deletions
|
@ -37,15 +37,15 @@ public class Board {
|
|||
}
|
||||
|
||||
private boolean inBounds(Piece piece, int x, int y) {
|
||||
return inBounds(x, y) && inBounds(x + piece.width, y + piece.height - 1);
|
||||
return inBounds(x, y) && inBounds(x + piece.cellCols, y + piece.celRows - 1);
|
||||
}
|
||||
|
||||
private boolean canPutPiece(Piece piece, int x, int y) {
|
||||
if (!inBounds(piece, x, y))
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < piece.height; i++)
|
||||
for (int j = 0; j < piece.width; j++)
|
||||
for (int i = 0; i < piece.celRows; i++)
|
||||
for (int j = 0; j < piece.cellCols; j++)
|
||||
if (!cells[y+i][x+j].isEmpty() && piece.filled(i, j))
|
||||
return false;
|
||||
|
||||
|
@ -56,8 +56,8 @@ public class Board {
|
|||
if (!canPutPiece(piece, x, y))
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < piece.height; i++)
|
||||
for (int j = 0; j < piece.width; j++)
|
||||
for (int i = 0; i < piece.celRows; i++)
|
||||
for (int j = 0; j < piece.cellCols; j++)
|
||||
cells[y+i][x+j].set(piece.color);
|
||||
|
||||
return true;
|
||||
|
|
|
@ -23,7 +23,8 @@ class Cell {
|
|||
draw(color, batch, patch, x, y, size);
|
||||
}
|
||||
|
||||
static void draw(Color color, SpriteBatch batch, NinePatch patch, int x, int y, int size) {
|
||||
static void draw(Color color, SpriteBatch batch, NinePatch patch,
|
||||
float x, float y, float size) {
|
||||
// TODO Use skin atlas
|
||||
batch.setColor(color);
|
||||
patch.draw(batch, x, y, size, size);
|
||||
|
|
|
@ -4,6 +4,8 @@ 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.Rectangle;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
// Pieces can be L shaped and be rotated 0 to 3 times to make it random
|
||||
// Maximum size = 4
|
||||
|
@ -15,19 +17,23 @@ public class Piece {
|
|||
0x57cb84ff, 0x5abee2ff // L's
|
||||
};
|
||||
|
||||
final int width, height;
|
||||
final Vector2 pos;
|
||||
float cellSize = 10f; // Default
|
||||
|
||||
final int cellCols, celRows;
|
||||
private boolean shape[][];
|
||||
|
||||
final Color color;
|
||||
|
||||
private Piece(int w, int h, boolean swapSize, int colorIndex) {
|
||||
private Piece(int cols, int rows, boolean swapSize, int colorIndex) {
|
||||
color = new Color(colors[colorIndex]);
|
||||
|
||||
width = swapSize ? h : w;
|
||||
height = swapSize ? w : h;
|
||||
shape = new boolean[height][width];
|
||||
for (int i = 0; i < height; i++) {
|
||||
for (int j = 0; j < width; j++) {
|
||||
pos = new Vector2();
|
||||
cellCols = swapSize ? rows : cols;
|
||||
celRows = swapSize ? cols : rows;
|
||||
shape = new boolean[celRows][cellCols];
|
||||
for (int i = 0; i < celRows; i++) {
|
||||
for (int j = 0; j < cellCols; j++) {
|
||||
shape[i][j] = true;
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +42,8 @@ public class Piece {
|
|||
private Piece(int lSize, int rotateCount, int colorIndex) {
|
||||
color = new Color(colors[colorIndex]);
|
||||
|
||||
width = height = lSize;
|
||||
pos = new Vector2();
|
||||
cellCols = celRows = lSize;
|
||||
shape = new boolean[lSize][lSize];
|
||||
switch (rotateCount % 4) {
|
||||
case 0: // ┌
|
||||
|
@ -91,11 +98,16 @@ public class Piece {
|
|||
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++) {
|
||||
Rectangle getRectangle() {
|
||||
return new Rectangle(pos.x, pos.y, cellCols * cellSize, celRows * cellSize);
|
||||
}
|
||||
|
||||
void draw(SpriteBatch batch, NinePatch patch) {
|
||||
for (int i = 0; i < celRows; i++) {
|
||||
for (int j = 0; j < cellCols; j++) {
|
||||
if (shape[i][j]) {
|
||||
Cell.draw(color, batch, patch, x + j * size, y + i * size, size);
|
||||
Cell.draw(color, batch, patch,
|
||||
pos.x + j * cellSize, pos.y + i * cellSize, cellSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,26 @@
|
|||
package io.github.lonamiwebs.klooni.game;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
public class PieceHolder {
|
||||
|
||||
final Piece[] pieces;
|
||||
final int count;
|
||||
public final int width, height;
|
||||
|
||||
public PieceHolder(int pieceCount, int holderWidth, int holderHeight) {
|
||||
int heldPiece;
|
||||
|
||||
final Vector2 pos;
|
||||
final int width, height;
|
||||
|
||||
public PieceHolder(int pieceCount, float x, float y, int holderWidth, int holderHeight) {
|
||||
count = pieceCount;
|
||||
pieces = new Piece[count];
|
||||
|
||||
heldPiece = -1;
|
||||
pos = new Vector2(x, y);
|
||||
width = holderWidth;
|
||||
height = holderHeight;
|
||||
|
||||
|
@ -20,24 +28,62 @@ public class PieceHolder {
|
|||
}
|
||||
|
||||
void takeMore() {
|
||||
int perPieceSize = width / 3;
|
||||
for (int i = 0; i < count; i++) {
|
||||
pieces[i] = Piece.random();
|
||||
|
||||
// Set the local position and the cell size
|
||||
pieces[i].pos.set(pos.x + i * perPieceSize, pos.y);
|
||||
pieces[i].cellSize = Math.min(
|
||||
perPieceSize / pieces[i].cellCols,
|
||||
height / pieces[i].celRows);
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
// Pick the piece below the finger/mouse
|
||||
public boolean pickPiece() {
|
||||
Vector2 mouse = new Vector2(
|
||||
Gdx.input.getX(),
|
||||
Gdx.graphics.getHeight() - Gdx.input.getY()); // Y axis is inverted
|
||||
|
||||
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
|
||||
);
|
||||
if (pieces[i].getRectangle().contains(mouse)) {
|
||||
heldPiece = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
pieces[i].draw(batch, patch, x + i * perPieceSize, y, cellSize);
|
||||
heldPiece = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean dropPiece() {
|
||||
if (heldPiece > -1) {
|
||||
heldPiece = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if (heldPiece > -1) {
|
||||
Piece piece = pieces[heldPiece];
|
||||
|
||||
Vector2 mouse = new Vector2(
|
||||
Gdx.input.getX(),
|
||||
Gdx.graphics.getHeight() - Gdx.input.getY()); // Y axis is inverted
|
||||
|
||||
// Center the piece
|
||||
mouse.sub(piece.getRectangle().width / 2, piece.getRectangle().height / 2);
|
||||
|
||||
piece.pos.lerp(mouse, 0.4f);
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(SpriteBatch batch, NinePatch patch) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (pieces[i] != null) {
|
||||
pieces[i].draw(batch, patch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package io.github.lonamiwebs.klooni.screens;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.InputProcessor;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
|
@ -11,7 +12,7 @@ import io.github.lonamiwebs.klooni.game.Board;
|
|||
import io.github.lonamiwebs.klooni.game.Piece;
|
||||
import io.github.lonamiwebs.klooni.game.PieceHolder;
|
||||
|
||||
public class GameScreen implements Screen {
|
||||
public class GameScreen implements Screen, InputProcessor {
|
||||
|
||||
private Klooni game;
|
||||
private Board board;
|
||||
|
@ -22,7 +23,12 @@ public class GameScreen implements Screen {
|
|||
public GameScreen(Klooni aGame) {
|
||||
game = aGame;
|
||||
board = new Board(10, 20);
|
||||
holder = new PieceHolder(3, Gdx.graphics.getWidth() / 2, 80);
|
||||
|
||||
// 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();
|
||||
|
||||
|
@ -32,9 +38,11 @@ public class GameScreen implements Screen {
|
|||
}
|
||||
}
|
||||
|
||||
//region Screen
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
|
||||
Gdx.input.setInputProcessor(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -47,9 +55,8 @@ public class GameScreen implements Screen {
|
|||
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);
|
||||
holder.update();
|
||||
holder.draw(batch, board.cellPatch);
|
||||
|
||||
batch.end();
|
||||
}
|
||||
|
@ -78,4 +85,50 @@ public class GameScreen implements Screen {
|
|||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Input
|
||||
|
||||
@Override
|
||||
public boolean keyDown(int keycode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyTyped(char character) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||
return holder.pickPiece();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
return holder.dropPiece();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseMoved(int screenX, int screenY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scrolled(int amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//endregion
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue