From 6a1fb0bb51dcd88b4fe2a7355b3941864a65bb57 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 24 Jan 2017 12:54:54 +0100 Subject: [PATCH] Add game logic (board, piece and cell) --- .../github/lonamiwebs/klooni/game/Board.java | 59 ++++++++++ .../github/lonamiwebs/klooni/game/Cell.java | 36 ++++++ .../github/lonamiwebs/klooni/game/Piece.java | 103 ++++++++++++++++++ .../lonamiwebs/klooni/screens/GameScreen.java | 62 +++++++++++ .../klooni/screens/MainMenuScreen.java | 3 +- 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 core/src/io/github/lonamiwebs/klooni/game/Board.java create mode 100644 core/src/io/github/lonamiwebs/klooni/game/Cell.java create mode 100644 core/src/io/github/lonamiwebs/klooni/game/Piece.java create mode 100644 core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java diff --git a/core/src/io/github/lonamiwebs/klooni/game/Board.java b/core/src/io/github/lonamiwebs/klooni/game/Board.java new file mode 100644 index 0000000..5724f13 --- /dev/null +++ b/core/src/io/github/lonamiwebs/klooni/game/Board.java @@ -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); + } +} diff --git a/core/src/io/github/lonamiwebs/klooni/game/Cell.java b/core/src/io/github/lonamiwebs/klooni/game/Cell.java new file mode 100644 index 0000000..672adac --- /dev/null +++ b/core/src/io/github/lonamiwebs/klooni/game/Cell.java @@ -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; + } +} diff --git a/core/src/io/github/lonamiwebs/klooni/game/Piece.java b/core/src/io/github/lonamiwebs/klooni/game/Piece.java new file mode 100644 index 0000000..e97e84e --- /dev/null +++ b/core/src/io/github/lonamiwebs/klooni/game/Piece.java @@ -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."); + } +} diff --git a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java new file mode 100644 index 0000000..b3ede1d --- /dev/null +++ b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java @@ -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() { + + } +} diff --git a/core/src/io/github/lonamiwebs/klooni/screens/MainMenuScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/MainMenuScreen.java index 19d70f9..5d065d7 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/MainMenuScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/MainMenuScreen.java @@ -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(); } });