feat: day 1 progress

This commit is contained in:
Martin Prokoph
2025-06-27 19:58:59 +02:00
commit 9e45ea3f37
29 changed files with 988 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
package eu.midnightdust.yaytris.game;
import java.awt.Color;
import java.util.*;
public class Space {
private final Color[][] gameMap; // Bereits abgesetzte Tetrominos werden nur noch als einzelne Farben ('Blobs') auf der Karte abgespeichert
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);
}
}
}
}
public Color[][] getGameMap() {
return gameMap;
}
public int onLinesChanged(Tetromino tetromino, int... lines) {
int combo = 0;
Set<Integer> completedLines = new TreeSet<>();
for (int line : lines) {
Color[] newBlobs = tetromino.getLine(line);
for (int i = 0; i < newBlobs.length; i++) {
if (newBlobs[i] == null) continue;
gameMap[line][i] = newBlobs[i];
}
if (Arrays.stream(gameMap[line]).noneMatch(Objects::isNull)) { // Line completed
combo += 1;
completedLines.add(line);
combo *= completedLines.size();
}
}
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];
}
}
return combo;
}
}

View File

@@ -0,0 +1,40 @@
package eu.midnightdust.yaytris.game;
import eu.midnightdust.yaytris.util.Vec2i;
import java.awt.*;
public class Tetromino {
private final TetrominoShape shape;
private int[][] collision;
private Vec2i centerPos;
public Tetromino(TetrominoShape shape) {
this.shape = shape;
this.collision = shape.boundary;
this.centerPos = Vec2i.of(0, 0);
}
public void fall(int length) {
centerPos = centerPos.offset(Vec2i.of(0, length));
}
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];
}
}
this.collision = newCollision;
}
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;
}
return l;
}
}

View File

@@ -0,0 +1,48 @@
package eu.midnightdust.yaytris.game;
import java.awt.Color;
public enum TetrominoShape {
SQUARE(new int[][]{
{1, 1},
{1, 2}
}, Color.YELLOW),
LINE(new int[][]{
{1},
{2},
{1},
{1}
}, Color.BLUE),
T(new int[][]{
{0, 1, 0},
{1, 2, 1}
}, Color.RED),
L_LEFT(new int[][]{
{0, 1},
{0, 2},
{1, 1}
}, Color.MAGENTA),
L_RIGHT(new int[][]{
{1, 0},
{2, 0},
{1, 1}
}, Color.GREEN),
ZAP_LEFT(new int[][]{
{0, 1},
{1, 2},
{1, 0}
}, Color.CYAN),
ZAP_RIGHT(new int[][]{
{1, 0},
{2, 1},
{0, 1}
}, Color.PINK);
;
final int[][] boundary;
final Color color;
TetrominoShape(int[][] boundary, Color color) {
this.boundary = boundary;
this.color = color;
}
}