feat: day 1 progress
This commit is contained in:
47
src/main/java/eu/midnightdust/yaytris/game/Space.java
Normal file
47
src/main/java/eu/midnightdust/yaytris/game/Space.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user