Add game logic (board, piece and cell)

This commit is contained in:
Lonami Exo 2017-01-24 12:54:54 +01:00
parent f81177d4f3
commit 6a1fb0bb51
5 changed files with 262 additions and 1 deletions

View file

@ -0,0 +1,59 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Board {
Cell[][] cells;
private final int count; // Cell count
private final int size; // Size per cell
public Board(int boardSize, int cellSize) {
count = boardSize;
size = cellSize;
cells = new Cell[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
cells[i][j] = new Cell();
}
}
}
private boolean inBounds(int x, int y) {
return x >= 0 && x < count && y >= 0 && y < count;
}
private boolean inBounds(Piece piece, int x, int y) {
return inBounds(x, y) && inBounds(x + piece.width, y + piece.height - 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++)
if (!cells[y+i][x+j].isEmpty() && piece.filled(i, j))
return false;
return true;
}
public boolean putPiece(Piece piece, int x, int y) {
if (!canPutPiece(piece, x, y))
return false;
for (int i = 0; i < piece.height; i++)
for (int j = 0; j < piece.width; j++)
cells[y+i][x+j].set(piece.texture);
return true;
}
public void draw(SpriteBatch batch) {
for (int i = 0; i < count; i++)
for (int j = 0; j < count; j++)
cells[i][j].draw(batch, j * size, i * size, size);
}
}

View file

@ -0,0 +1,36 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
class Cell {
private boolean empty;
private Texture texture;
Cell() {
empty = true;
texture = Piece.getTexture(Color.WHITE);
// texture or color? like in a future, uhm, nah, all cells same texture diff colorz
}
void set(Texture tex) {
empty = false;
texture = tex; // TODO Disposing uhm? Or use the skin better
}
void draw(SpriteBatch batch, int x, int y, int size) {
//batch.setColor(color);
batch.draw(texture, x, y, size, size);
}
boolean isEmpty() {
return empty;
}
public void vanish() {
empty = true;
}
}

View file

@ -0,0 +1,103 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
// Pieces can be L shaped and be rotated 0 to 3 times to make it random
// Maximum size = 4
public class Piece {
private final static int[] colors = {
0x7988bfff, 0x98dc53ff, 0x4cd4aeff, // Squares
0xfec63dff, 0xec9548ff, 0xe66a82ff, 0xda6554ff, // Lines
0x57cb84ff, 0x5abee2ff // L's
};
final int width, height;
boolean shape[][];
final Texture texture;
private Color color;
private Piece(int w, int h, 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++) {
shape[i][j] = true;
}
}
texture = getTexture(color);
}
static Texture getTexture(Color color) {
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGB888);
pixmap.drawPixel(0, 0, color.toIntBits());
return new Texture(pixmap);
}
private Piece(int lSize, int rotateCount, int colorIndex) {
color = new Color(colors[colorIndex]);
texture = getTexture(color);
width = height = lSize;
shape = new boolean[lSize][lSize];
switch (rotateCount % 4) {
case 0: //
for (int j = 0; j < lSize; j++)
shape[0][j] = true;
for (int i = 0; i < lSize; i++)
shape[i][0] = true;
break;
case 1: //
for (int j = 0; j < lSize; j++)
shape[0][j] = true;
for (int i = 0; i < lSize; i++)
shape[i][lSize-1] = true;
break;
case 2: //
for (int j = 0; j < lSize; j++)
shape[lSize-1][j] = true;
for (int i = 0; i < lSize; i++)
shape[i][lSize-1] = true;
break;
case 3: //
for (int j = 0; j < lSize; j++)
shape[lSize-1][j] = true;
for (int i = 0; i < lSize; i++)
shape[i][0] = true;
break;
}
}
boolean filled(int i, int j) {
return shape[i][j];
}
public static Piece random() {
int color = MathUtils.random(8); // 9 pieces
switch (color) {
// Squares
case 0: return new Piece(1, 1, false, color);
case 1: return new Piece(2, 2, false, color);
case 2: return new Piece(3, 3, false, color);
// Lines
case 3: return new Piece(1, 2, MathUtils.randomBoolean(), color);
case 4: return new Piece(1, 3, MathUtils.randomBoolean(), color);
case 5: return new Piece(1, 4, MathUtils.randomBoolean(), color);
case 6: return new Piece(1, 5, MathUtils.randomBoolean(), color);
// L's
case 7: return new Piece(2, MathUtils.random(3), color);
case 8: return new Piece(3, MathUtils.random(3), color);
}
throw new RuntimeException("Random function is broken.");
}
}

View file

@ -0,0 +1,62 @@
package io.github.lonamiwebs.klooni.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.game.Board;
public class GameScreen implements Screen {
private Klooni game;
private Board board;
private SpriteBatch batch;
public GameScreen(Klooni aGame) {
game = aGame;
board = new Board(10, 20);
batch = new SpriteBatch();
}
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.9f, 0.9f, 0.7f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
board.draw(batch);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}

View file

@ -55,7 +55,8 @@ public class MainMenuScreen implements Screen {
playButton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
System.out.println("Clicked! Is checked: " + playButton.isChecked());
game.setScreen(new GameScreen(game));
dispose();
}
});