-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
657 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/DropSpeed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
public enum DropSpeed { | ||
NORMAL, | ||
FAST | ||
} |
7 changes: 7 additions & 0 deletions
7
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/GameState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
public enum GameState { | ||
PLAYING, | ||
PAUSED, | ||
GAME_OVER | ||
} |
40 changes: 40 additions & 0 deletions
40
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/KeyRepeat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
public final class KeyRepeat { | ||
private static final int repeatDelay = 175; | ||
private static final int repeatInterval = 60; | ||
|
||
private long downTime, repeatTime; | ||
private boolean down = false; | ||
private boolean justPressed = false; | ||
|
||
public void setDown(boolean down) { | ||
this.down = down; | ||
if (down) { | ||
downTime = System.currentTimeMillis(); | ||
repeatTime = downTime + repeatDelay - repeatInterval; | ||
justPressed = true; | ||
} | ||
} | ||
|
||
public int getPressed() { | ||
if (justPressed) { | ||
justPressed = false; | ||
return 1; | ||
} | ||
|
||
if (!down) | ||
return 0; | ||
|
||
long time = System.currentTimeMillis() - downTime - repeatDelay; | ||
if (time < 0) | ||
return 0; | ||
|
||
int repeats = 0; | ||
while (System.currentTimeMillis() - repeatTime >= repeatInterval) { | ||
repeatTime += repeatInterval; | ||
repeats++; | ||
} | ||
return repeats; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/Kick.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
public final class Kick { | ||
public final int dx, dy; | ||
|
||
public Kick(int dx, int dy) { | ||
this.dx = dx; | ||
this.dy = dy; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/Matrix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
import processing.core.PGraphics; | ||
|
||
public final class Matrix { | ||
public static final int matrixWidth = 10; | ||
public static final int matrixHeight = 20; | ||
|
||
private final TileState[][] tiles = new TileState[matrixWidth][matrixHeight]; | ||
|
||
public Matrix() { | ||
for (int x = 0; x < matrixWidth; x++) { | ||
for (int y = 0; y < matrixHeight; y++) { | ||
tiles[x][y] = TileState.EMPTY; | ||
} | ||
} | ||
} | ||
|
||
private boolean inBounds(int x, int y) { | ||
return x >= 0 && x < matrixWidth && y >= 0 && y < matrixHeight; | ||
} | ||
|
||
private void set(int x, int y, TileState state) { | ||
if (inBounds(x, y)) { | ||
tiles[x][y] = state; | ||
} | ||
} | ||
|
||
private TileState get(int x, int y) { | ||
return tiles[x][y]; | ||
} | ||
|
||
public int plot(TetrominoContext ctx) { | ||
int[] minY = {Integer.MAX_VALUE}; | ||
int[] maxY = {0}; | ||
ctx.forEachMino((mx, my) -> { | ||
set(mx, my, ctx.getShape().state); | ||
minY[0] = Math.min(minY[0], my); | ||
maxY[0] = Math.max(maxY[0], my); | ||
}); | ||
|
||
int linesCleared = 0; | ||
|
||
// Check for completed rows | ||
int count = maxY[0] - minY[0] + 1; | ||
int y = maxY[0]; | ||
row: for (int i = 0; i < count; i++) { | ||
for (int x = 0; x < matrixWidth; x++) { | ||
if (get(x, y) == TileState.EMPTY) { | ||
y--; | ||
continue row; | ||
} | ||
} | ||
|
||
linesCleared++; | ||
|
||
// Move above down | ||
for (int row = y; row > 0; row--) { | ||
for (int x = 0; x < matrixWidth; x++) { | ||
set(x, row, get(x, row - 1)); | ||
} | ||
} | ||
|
||
// Clear top row | ||
for (int x = 0; x < matrixWidth; x++) { | ||
set(x, 0, TileState.EMPTY); | ||
} | ||
} | ||
|
||
return linesCleared; | ||
} | ||
|
||
public boolean collides(TetrominoContext ctx) { | ||
boolean[] hit = {false}; | ||
ctx.forEachMino((mx, my) -> { | ||
hit[0] |= !inBounds(mx, my) || get(mx, my) != TileState.EMPTY; | ||
}); | ||
return hit[0]; | ||
} | ||
|
||
public void draw(PGraphics g) { | ||
for (int x = 0; x < matrixWidth; x++) { | ||
for (int y = 0; y < matrixHeight; y++) { | ||
TetrisTool.drawMino(g, x, y, tiles[x][y].color); | ||
} | ||
} | ||
} | ||
|
||
public void clear() { | ||
for (int y = 0; y < matrixHeight; y++) { | ||
for (int x = 0; x < matrixWidth; x++) { | ||
set(x, y, TileState.EMPTY); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/Mino.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
public final class Mino { | ||
public final int offsetX; | ||
public final int offsetY; | ||
|
||
public Mino(int x, int y) { | ||
this.offsetX = x; | ||
this.offsetY = y; | ||
} | ||
|
||
public Mino rotate(Rotation rot) { | ||
return switch (rot) { | ||
case NONE -> this; | ||
case CW_90 -> new Mino(-offsetY, offsetX); | ||
case R_180 -> new Mino(-offsetX, -offsetY); | ||
case CCW_90 -> new Mino(offsetY, -offsetX); | ||
}; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/PieceRandomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class PieceRandomizer { | ||
private final List<Integer> bag = new ArrayList<>(TetrisTool.SHAPES.length); | ||
|
||
public PieceRandomizer() { | ||
refill(); | ||
} | ||
|
||
public int getNext() { | ||
int idx = (int) (Math.random() * bag.size()); | ||
int shape = bag.remove(idx); | ||
if (bag.isEmpty()) | ||
refill(); | ||
return shape; | ||
} | ||
|
||
private void refill() { | ||
for (int i = 0; i < TetrisTool.SHAPES.length; i++) { | ||
bag.add(i); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ShuffleLog/src/main/java/com/swrobotics/shufflelog/tool/tetris/Rotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.swrobotics.shufflelog.tool.tetris; | ||
|
||
public enum Rotation { | ||
NONE(0), | ||
CW_90(1), | ||
R_180(2), | ||
CCW_90(3); | ||
|
||
static { | ||
NONE.next = CW_90; | ||
CW_90.next = R_180; | ||
R_180.next = CCW_90; | ||
CCW_90.next = NONE; | ||
} | ||
|
||
public final int kickIdx; | ||
private Rotation next; | ||
|
||
Rotation(int kickIdx) { | ||
this.kickIdx = kickIdx; | ||
} | ||
|
||
public Rotation next() { | ||
return next; | ||
} | ||
} |
Oops, something went wrong.