Skip to content

Commit

Permalink
[ShuffleLog] Just a little fun
Browse files Browse the repository at this point in the history
  • Loading branch information
rmheuer committed Mar 26, 2024
1 parent 9ad90d6 commit 4e6a53a
Show file tree
Hide file tree
Showing 14 changed files with 657 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void setup() {
NetworkTablesTool nt = new NetworkTablesTool(threadPool);
nt.addListener(smartDashboard);

tools.add(new MenuBarTool(smartDashboard));
tools.add(new MenuBarTool(this, smartDashboard));
MessengerTool msg = new MessengerTool(this);
tools.add(msg);
tools.add(new ShuffleLogProfilerTool(this));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.swrobotics.shufflelog.tool;

import com.swrobotics.shufflelog.ShuffleLog;
import com.swrobotics.shufflelog.Styles;
import com.swrobotics.shufflelog.tool.smartdashboard.SmartDashboard;

import com.swrobotics.shufflelog.tool.tetris.TetrisTool;
import imgui.ImGui;
import imgui.extension.implot.ImPlot;
import imgui.type.ImBoolean;
Expand All @@ -11,14 +13,20 @@ public final class MenuBarTool implements Tool {
private final SmartDashboard smartDashboard;
private final ImBoolean showDemo, showPlotDemo;
private final ImBoolean plotDemoOpen;
private final ImBoolean showTetris;

public MenuBarTool(SmartDashboard smartDashboard) {
private final TetrisTool tetris;

public MenuBarTool(ShuffleLog log, SmartDashboard smartDashboard) {
this.smartDashboard = smartDashboard;

showDemo = new ImBoolean(false);
showPlotDemo = new ImBoolean(false);

plotDemoOpen = new ImBoolean(true);
showTetris = new ImBoolean(false);

tetris = new TetrisTool(log);
}

@Override
Expand All @@ -27,6 +35,7 @@ public void process() {
if (ImGui.beginMenu("Debug")) {
ImGui.menuItem("Show demo", null, showDemo);
ImGui.menuItem("Show plot demo", null, showPlotDemo);
ImGui.menuItem("Show Tetris", null, showTetris);

ImGui.endMenu();
}
Expand All @@ -44,5 +53,6 @@ public void process() {

if (showDemo.get()) ImGui.showDemoWindow();
if (showPlotDemo.get()) ImPlot.showDemoWindow(plotDemoOpen);
if (showTetris.get()) tetris.process();
}
}
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
}
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
}
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;
}
}
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;
}
}
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);
}
}
}
}
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);
};
}
}
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);
}
}
}
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;
}
}
Loading

0 comments on commit 4e6a53a

Please sign in to comment.