Skip to content

Commit

Permalink
Add an optional penalty shootout timer
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesbraun committed Jun 19, 2023
1 parent 7e63857 commit 0bba3a4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/rv/comm/rcssserver/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import org.magmaoffenburg.roboviz.configuration.Config;
import rv.comm.rcssserver.ServerComm.ServerChangeListener;
Expand Down Expand Up @@ -193,6 +194,10 @@ public HistoryItem(float time, String playMode)
private int half;
private List<Foul> fouls = new CopyOnWriteArrayList<>();

private boolean penaltyShootout = false;

private Float penaltyShotStartTime = null;

private final List<GameStateChangeListener> listeners = new CopyOnWriteArrayList<>();

private final List<ServerMessageReceivedListener> smListeners = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -404,6 +409,8 @@ public void reset()
passModeMinOppBallDist = 1;
passModeDuration = 4;
passModeValuesReported = false;

penaltyShotStartTime = null;
}

private boolean isTimeStopped()
Expand Down Expand Up @@ -607,6 +614,18 @@ public void parse(SExp exp, WorldModel world)
while (playModeHistory.size() > 2) {
playModeHistory.remove(0);
}

switch (playMode) {
case KICK_OFF_LEFT:
case KICK_OFF_RIGHT:
penaltyShotStartTime = time;
break;
case BEFORE_KICK_OFF:
case GOAL_LEFT:
case GOAL_RIGHT:
penaltyShotStartTime = null;
break;
}
}

initialized = true;
Expand Down Expand Up @@ -634,4 +653,19 @@ public void connectionChanged(ServerComm server)
server.getWorldModel().reset();
}
}

public boolean isPenaltyShootout()
{
return penaltyShootout;
}

public void setPenaltyShootout(boolean penaltyShootout)
{
this.penaltyShootout = penaltyShootout;
}

public Optional<Float> getPenaltyShotStartTime()
{
return Optional.ofNullable(penaltyShotStartTime);
}
}
25 changes: 25 additions & 0 deletions src/main/java/rv/ui/screens/GameStateOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ private class GameStateBar
private static final int Y_PAD = 4;
private static final int PLAYMODE_WIDTH = 2 * NAME_WIDTH + SCORE_BOX_WIDTH + TIME_WIDTH + 6;

/** the time a player has to execute a single penalty shot */
private static final float PENALTY_SHOT_TIME = 25.0f;

private final TextRenderer tr1;
private final TextRenderer tr2;
private final TextRenderer tr3;
Expand Down Expand Up @@ -160,6 +163,28 @@ void render(GL2 gl, GameState gs, int screenW, int screenH)
tr3.draw(String.format(Locale.US, "%.1f", passModeWaitToScoreTime), timeX, y - 20);
tr3.endRendering();
}

// Remaining time for penalty shot
if (gs.isPenaltyShootout()) {
gs.getPenaltyShotStartTime().ifPresent((it) -> {
final float remainingTime = Math.max(0.0f, PENALTY_SHOT_TIME - (gs.getTime() - it));
final int remainingPenaltyMinutes = (int) Math.floor(remainingTime / 60.0);
final int remainingPenaltySeconds = (int) Math.ceil(remainingTime - remainingPenaltyMinutes * 60);

gl.glBegin(GL2.GL_QUADS);
gl.glColor4f(0, 0, 0, 0.36f);
drawBox(gl, x - 3 + (2 * NAME_WIDTH + SCORE_BOX_WIDTH + TIME_WIDTH + 6), y - 3, TIME_WIDTH,
BAR_HEIGHT + 6);
gl.glEnd();

String remainingPenaltyTimeText =
String.format(Locale.US, "%02d:%02d", remainingPenaltyMinutes, remainingPenaltySeconds);
int penaltyTimeX = timeX + TIME_WIDTH;
tr1.beginRendering(screenW, screenH);
tr1.draw(remainingPenaltyTimeText, penaltyTimeX, y + Y_PAD);
tr1.endRendering();
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ class ViewActions {
}
}

fun togglePenaltyView() {
Renderer.world.gameState?.let {
it.isPenaltyShootout = !it.isPenaltyShootout
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ViewMenu : MenuBase() {
addItem("Toggle Field Overlay", KeyEvent.VK_F) { actions.toggleFieldOverlay() }
addItem("Toggle Drawings", KeyEvent.VK_T) { actions.toggleDrawings() }
addItem("Toggle Fouls", KeyEvent.VK_Q) { actions.toggleFouls() }
addItem("Toggle Penalty View", KeyEvent.VK_P, KeyEvent.CTRL_DOWN_MASK) { actions.togglePenaltyView() }
}

}

0 comments on commit 0bba3a4

Please sign in to comment.