feat: input handling!

This commit is contained in:
Martin Prokoph
2025-06-27 23:22:34 +02:00
parent 755599b4f3
commit b1be4e0731
6 changed files with 147 additions and 29 deletions

View File

@@ -3,18 +3,59 @@ package eu.midnightdust.yaytris.game;
import java.awt.Color;
import java.util.*;
import static eu.midnightdust.yaytris.Tetris.random;
public class Space {
private final Color[][] gameMap; // Bereits abgesetzte Tetrominos werden nur noch als einzelne Farben ('Blobs') auf der Karte abgespeichert
private TetrominoShape nextShape;
private Tetromino currentTetromino;
public Space() {
gameMap = new Color[7][12];
for (int x = 0; x < gameMap.length; x++) {
for (int y = 0; y < gameMap[x].length; y++) {
if (Math.random() < 0.5f) {
gameMap[x][y] = Color.getHSBColor((float) Math.random(), 1.f, 1.f);
gameMap = new Color[12][7];
nextShape = getNextShape();
Tetromino mino = new Tetromino(TetrominoShape.T);
mino.move(-2);
mino.fall(10);
onLinesChanged(mino, 9, 10, 11);
Tetromino mina = new Tetromino(TetrominoShape.LINE);
mina.move(1);
mina.rotate();
mina.fall(11);
onLinesChanged(mina, 9, 10, 11);
}
public void spawnTetromino() {
currentTetromino = new Tetromino(nextShape);
nextShape = getNextShape();
}
public TetrominoShape getNextShape() {
return TetrominoShape.values()[random.nextInt(TetrominoShape.values().length)];
}
public int getMapWidth() {
return gameMap[0].length;
}
public int getMapHeight() {
return gameMap.length;
}
public Color[][] getGameMapWithTetromino() {
Color[][] tempGameMap = new Color[gameMap.length][gameMap[0].length];
for (int y = 0; y < tempGameMap.length; y++) {
System.arraycopy(gameMap[y], 0, tempGameMap[y], 0, tempGameMap[y].length);
if (currentTetromino != null) {
Color[] newBlobs = currentTetromino.getLine(y);
for (int i = 0; i < newBlobs.length; i++) {
if (newBlobs[i] == null) continue;
tempGameMap[y][i] = newBlobs[i];
}
}
}
return tempGameMap;
}
public Color[][] getGameMap() {
@@ -25,6 +66,7 @@ public class Space {
int combo = 0;
Set<Integer> completedLines = new TreeSet<>();
for (int line : lines) {
if (line > getMapHeight()) continue;
Color[] newBlobs = tetromino.getLine(line);
for (int i = 0; i < newBlobs.length; i++) {
if (newBlobs[i] == null) continue;
@@ -39,9 +81,14 @@ public class Space {
for (int completedIndex = 0; completedIndex < completedLines.size(); completedIndex++) { // Remove completed lines
int line = completedLines.toArray(new Integer[0])[completedIndex];
for (int i = line+completedIndex; i >= 0; i--) {
gameMap[i] = gameMap[i-1];
if (i >= getMapHeight()) continue;
gameMap[i] = (i-1 < 0) ? new Color[gameMap[i].length] : gameMap[i-1];
}
}
return combo;
}
public Tetromino getCurrentTetromino() {
return currentTetromino;
}
}

View File

@@ -1,5 +1,6 @@
package eu.midnightdust.yaytris.game;
import eu.midnightdust.yaytris.Tetris;
import eu.midnightdust.yaytris.util.Vec2i;
import java.awt.*;
@@ -12,18 +13,40 @@ public class Tetromino {
public Tetromino(TetrominoShape shape) {
this.shape = shape;
this.collision = shape.boundary;
this.centerPos = Vec2i.of(0, 0);
this.centerPos = Vec2i.of(2, 0);
}
public void fall(int length) {
centerPos = centerPos.offset(Vec2i.of(0, length));
Vec2i newPos = centerPos.offset(Vec2i.of(0, length));
if (Tetris.space != null && newPos.getY()+this.collision.length > Tetris.space.getMapHeight()) {
int[] affectedLines = new int[this.collision.length];
int line = centerPos.getY();
for (int i = 0; i < this.collision.length; i++) {
affectedLines[i] = line;
line++;
}
Tetris.space.onLinesChanged(this, affectedLines);
Tetris.space.spawnTetromino();
}
centerPos = newPos;
}
public void move(int xOffset) {
Vec2i newPos = centerPos.offset(Vec2i.of(xOffset, 0));
if (Tetris.space == null || newPos.getX() < 0 || newPos.getX() + collision[0].length > Tetris.space.getGameMap()[0].length) {
return;
}
centerPos = newPos;
}
public void rotate() {
int[][] newCollision = new int[collision[0].length][collision.length];
for (int i = 0; i < collision.length; i++) {
for (int j = 0; j < collision[i].length; j++) {
newCollision[j][i] = collision[i][j];
int M = collision.length;
int N = collision[0].length;
int[][] newCollision = new int[N][M];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
newCollision[j][M-i-1] = collision[i][j];
}
}
this.collision = newCollision;
@@ -32,8 +55,12 @@ public class Tetromino {
public Color[] getLine(int line) {
Color[] l = new Color[7];
for (int i = 0; i < l.length; i++) {
if (collision.length < line-centerPos.getX() && collision[line-centerPos.getX()][i] != 0)
l[i] = shape.color;
int relY = line - centerPos.getY();
if (relY >= collision.length || relY < 0) continue;
int relX = i-centerPos.getX();
if (relX >= collision[relY].length || relX < 0) continue;
if (collision[relY][relX] != 0) l[i] = shape.color;
}
return l;
}