-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisWorld.java
More file actions
248 lines (205 loc) · 5.7 KB
/
Copy pathTetrisWorld.java
File metadata and controls
248 lines (205 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import core.Entity;
import core.Relation;
public class TetrisWorld extends Entity<Relation> {
public static final int WORLD_ID = 200;
public static final int BOARD_ID = 201;
public static final int ACTIVE_PIECE_ID = 202;
private static final int ROWS = 20;
private static final int COLS = 10;
private static final int SPAWN_ROW = 0;
private static final int SPAWN_COL = 3;
private final Random random;
private final List<TetrominoType> bag = new ArrayList<>();
private final TetrisBoard board;
private final FallingPiece activePiece;
private TetrominoType nextType = TetrominoType.I;
private int score = 0;
private int linesCleared = 0;
private int level = 1;
private boolean paused = false;
private boolean gameOver = false;
public TetrisWorld() {
this(new Random());
}
public TetrisWorld(Random random) {
super(WORLD_ID, "blockfall-world");
this.random = random;
this.board = new TetrisBoard(BOARD_ID, "blockfall-board", ROWS, COLS);
this.activePiece = new FallingPiece(ACTIVE_PIECE_ID, "active-piece");
addChildEntity(board);
addChildEntity(activePiece);
restart();
}
@Override
public void execute() {
tick();
}
public void restart() {
board.clear();
bag.clear();
score = 0;
linesCleared = 0;
level = 1;
paused = false;
gameOver = false;
nextType = drawFromBag();
spawnPiece();
}
public void tick() {
if (paused || gameOver) {
return;
}
if (!tryMove(0, 1)) {
lockPiece();
}
}
public void moveHorizontal(int dx) {
if (paused || gameOver) {
return;
}
tryMove(dx, 0);
}
public void softDrop() {
if (paused || gameOver) {
return;
}
if (tryMove(0, 1)) {
score += 1;
} else {
lockPiece();
}
}
public void hardDrop() {
if (paused || gameOver) {
return;
}
int droppedRows = 0;
while (tryMove(0, 1)) {
droppedRows++;
}
score += droppedRows * 2;
lockPiece();
}
public void rotateClockwise() {
rotate(1);
}
public void rotateCounterClockwise() {
rotate(-1);
}
public void togglePause() {
if (!gameOver) {
paused = !paused;
}
}
private boolean tryMove(int dx, int dy) {
int nextRow = activePiece.getRow() + dy;
int nextCol = activePiece.getCol() + dx;
if (board.canPlace(activePiece, nextRow, nextCol, activePiece.getRotation())) {
activePiece.moveTo(nextRow, nextCol);
return true;
}
return false;
}
private void rotate(int delta) {
if (paused || gameOver) {
return;
}
int nextRotation = activePiece.getRotation() + delta;
int[][] kicks = {
{0, 0},
{1, 0},
{-1, 0},
{2, 0},
{-2, 0},
{0, -1}
};
for (int[] kick : kicks) {
int nextCol = activePiece.getCol() + kick[0];
int nextRow = activePiece.getRow() + kick[1];
if (board.canPlace(activePiece, nextRow, nextCol, nextRotation)) {
activePiece.moveTo(nextRow, nextCol);
activePiece.setRotation(nextRotation);
return;
}
}
}
private void lockPiece() {
board.lock(activePiece);
int cleared = board.clearFullLines();
if (cleared > 0) {
score += switch (cleared) {
case 1 -> 100 * level;
case 2 -> 300 * level;
case 3 -> 500 * level;
default -> 800 * level;
};
linesCleared += cleared;
level = 1 + (linesCleared / 10);
}
spawnPiece();
}
private void spawnPiece() {
TetrominoType currentType = nextType;
nextType = drawFromBag();
activePiece.reset(currentType, SPAWN_ROW, SPAWN_COL);
if (!board.canPlace(activePiece)) {
gameOver = true;
}
}
private TetrominoType drawFromBag() {
if (bag.isEmpty()) {
bag.addAll(Arrays.asList(TetrominoType.values()));
Collections.shuffle(bag, random);
}
return bag.remove(bag.size() - 1);
}
public List<Point> getGhostCells() {
int dropDistance = 0;
while (board.canPlace(
activePiece,
activePiece.getRow() + dropDistance + 1,
activePiece.getCol(),
activePiece.getRotation()
)) {
dropDistance++;
}
return activePiece.cellsFor(
activePiece.getRow() + dropDistance,
activePiece.getCol(),
activePiece.getRotation()
);
}
public TetrisBoard getBoard() {
return board;
}
public FallingPiece getActivePiece() {
return activePiece;
}
public TetrominoType getNextType() {
return nextType;
}
public int getScore() {
return score;
}
public int getLinesCleared() {
return linesCleared;
}
public int getLevel() {
return level;
}
public boolean isPaused() {
return paused;
}
public boolean isGameOver() {
return gameOver;
}
public int getTickDelay() {
return Math.max(120, 620 - ((level - 1) * 45));
}
}