Use custom texture on Cell's

This commit is contained in:
Lonami Exo 2017-01-24 13:13:42 +01:00
parent 6a1fb0bb51
commit 560d9d2591
5 changed files with 28 additions and 26 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 B

View file

@ -1,5 +1,8 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Board {
@ -8,6 +11,8 @@ public class Board {
private final int count; // Cell count
private final int size; // Size per cell
private NinePatch cellPatch;
public Board(int boardSize, int cellSize) {
count = boardSize;
size = cellSize;
@ -18,6 +23,9 @@ public class Board {
cells[i][j] = new Cell();
}
}
cellPatch = new NinePatch(
new Texture(Gdx.files.internal("ui/cells/basic.png")), 4, 4, 4, 4);
}
private boolean inBounds(int x, int y) {
@ -46,7 +54,7 @@ public class Board {
for (int i = 0; i < piece.height; i++)
for (int j = 0; j < piece.width; j++)
cells[y+i][x+j].set(piece.texture);
cells[y+i][x+j].set(piece.color);
return true;
}
@ -54,6 +62,6 @@ public class Board {
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);
cells[i][j].draw(batch, cellPatch, j * size, i * size, size);
}
}

View file

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

View file

@ -1,8 +1,6 @@
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
@ -16,11 +14,9 @@ public class Piece {
};
final int width, height;
boolean shape[][];
private boolean shape[][];
final Texture texture;
private Color color;
final Color color;
private Piece(int w, int h, boolean swapSize, int colorIndex) {
color = new Color(colors[colorIndex]);
@ -33,18 +29,10 @@ public class Piece {
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];

View file

@ -4,9 +4,11 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.game.Board;
import io.github.lonamiwebs.klooni.game.Piece;
public class GameScreen implements Screen {
@ -18,6 +20,11 @@ public class GameScreen implements Screen {
game = aGame;
board = new Board(10, 20);
batch = new SpriteBatch();
// Fill some random pieces
for (int i = 0; i < 10; i++) {
board.putPiece(Piece.random(), MathUtils.random(10), MathUtils.random(10));
}
}
@Override