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

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