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;
}
}