Skip to content

Commit

Permalink
TeamData capture percentage cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Spikatrix committed Sep 10, 2020
1 parent 258bb8b commit 238de43
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
14 changes: 13 additions & 1 deletion core/src/com/cg/zoned/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ public void update(PlayerManager playerManager, Player[] players, float delta) {

setMapWeights(players);
setMapColors(playerManager, players);
updateCapturePercentage(playerManager);
}
}
}
Expand Down Expand Up @@ -293,6 +294,17 @@ private void setMapColors(PlayerManager playerManager, Player[] players) {
fillSurroundedCells(playerManager, players);
}

private void updateCapturePercentage(PlayerManager playerManager) {
if (playerManager == null) {
return;
}

Array<TeamData> teamData = playerManager.getTeamData();
for (TeamData td : teamData) {
td.setCapturePercentage((this.rows * this.cols) - this.wallCount);
}
}

private void fillSurroundedCells(PlayerManager playerManager, Player[] players) {
FloodFillGridState[][] gridState = new FloodFillGridState[rows][cols];
for (int i = 0; i < rows; i++) {
Expand Down Expand Up @@ -516,7 +528,7 @@ public boolean gameComplete(Array<TeamData> teamData) {
if (teamData.size == 2) {
for (TeamData td : teamData) {
// For two team games, end the game when a team has captured more than 50% of the cells
if (100 * (td.getScore() / (((double) this.rows * this.cols) - this.wallCount)) > 50.0) {
if (td.getCapturePercentage() > 50.0) {
return true;
}
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/com/cg/zoned/TeamData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,43 @@

import com.badlogic.gdx.graphics.Color;

import java.text.DecimalFormat;

public class TeamData {
private Color color;
private int score;
private double capturePercentage;

public TeamData(Color color) {
this.color = color;
this.score = 0;
this.capturePercentage = 0f;
}

public void incrementScore() {
score++;
}

public void incrementScore(int amount) {
score += amount;
}

public void resetScore() {
score = 0;
}

public void setCapturePercentage(int total) {
capturePercentage = 100 * ((double) score / total);
}

public void roundCapturePercentage(DecimalFormat df) {
capturePercentage = Double.parseDouble(df.format(capturePercentage));
}

public double getCapturePercentage() {
return capturePercentage;
}

public Color getColor() {
return color;
}
Expand Down
11 changes: 7 additions & 4 deletions core/src/com/cg/zoned/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,16 @@ public void render(float delta) {
}

if (showFPSCounter) {
UITextDisplayer.displayFPS(fullScreenStage.getViewport(), fullScreenStage.getBatch(), font, UITextDisplayer.padding, scoreBars.scoreBarHeight + UITextDisplayer.padding);
UITextDisplayer.displayFPS(fullScreenStage.getViewport(), fullScreenStage.getBatch(), font,
UITextDisplayer.padding, scoreBars.scoreBarHeight + UITextDisplayer.padding);
}
if (gameManager.gameConnectionManager.isActive) {
float yOffset = scoreBars.scoreBarHeight + UITextDisplayer.padding;
if (!showFPSCounter) {
yOffset = -yOffset + scoreBars.scoreBarHeight + UITextDisplayer.padding;
}
UITextDisplayer.displayPing(fullScreenStage.getViewport(), fullScreenStage.getBatch(), font, gameManager.gameConnectionManager.getPing(), UITextDisplayer.padding, yOffset);
UITextDisplayer.displayPing(fullScreenStage.getViewport(), fullScreenStage.getBatch(), font,
gameManager.gameConnectionManager.getPing(), UITextDisplayer.padding, yOffset);
}

fullScreenStage.act(delta);
Expand All @@ -285,11 +287,12 @@ private void fadeOutScreen(float delta) {
gameManager.gameConnectionManager.close();
}

Gdx.app.postRunnable(new Runnable() { // Hopefully fixes the occasional SIGSEGVs around 1 second after transitioning to VictoryScreen
// Transition to VictoryScreen after completing rendering the current frame to avoid SIGSEGV crashes
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
dispose();
game.setScreen(new VictoryScreen(game, gameManager.playerManager, map.rows, map.cols, map.wallCount));
game.setScreen(new VictoryScreen(game, gameManager.playerManager));
}
});
}
Expand Down
37 changes: 16 additions & 21 deletions core/src/com/cg/zoned/screens/VictoryScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ public class VictoryScreen extends ScreenAdapter implements InputProcessor {
private ParticleEffect trailEffect;

private Array<TeamData> teamData;
private double[] capturePercentage;

private Actor[][] scoreboardActors;
private Container<Label> scoreBoardTitleContainer;
private float rowHeightScale = 1.5f;
private float padding;

public VictoryScreen(final Zoned game, PlayerManager playerManager, int rows, int cols, int wallCount) {
public VictoryScreen(final Zoned game, PlayerManager playerManager) {
this.game = game;
game.discordRPCManager.updateRPC("Post match");

Expand All @@ -76,7 +75,17 @@ public VictoryScreen(final Zoned game, PlayerManager playerManager, int rows, in
this.animationManager = new AnimationManager(this.game, this);
this.font = game.skin.getFont(Assets.FontManager.SMALL.getFontName());

getVictoryStrings(playerManager, rows, cols, wallCount);
finalizeTeamData(playerManager);
}

private void finalizeTeamData(PlayerManager playerManager) {
teamData = playerManager.getTeamData();
new Sort().sort(teamData, new TeamDataComparator());

DecimalFormat df = new DecimalFormat("#.##");
for (TeamData td : teamData) {
td.roundCapturePercentage(df);
}
}

@Override
Expand Down Expand Up @@ -131,14 +140,14 @@ private void setUpVictoryUI() {
new ScoreboardHeaderData("SCORE", 2.5f),
};

RankData[] rankData = new RankData[] {
RankData[] rankData = new RankData[]{
new RankData(Color.GOLD, "icons/rank_icons/ic_no1.png"),
new RankData(Color.LIGHT_GRAY, "icons/rank_icons/ic_no2.png"),
new RankData(Color.BROWN, "icons/rank_icons/ic_no3.png"),
new RankData(Color.GRAY, null),
};

scoreboardActors = new Actor[capturePercentage.length + 1][];
scoreboardActors = new Actor[teamData.size + 1][];
float rowHeight = getRowHeight();

// Setting background drawable on a container since setting it directly to the label means
Expand Down Expand Up @@ -169,7 +178,7 @@ private void setUpVictoryUI() {
table.row();

int rankIndex = 0;
for (int i = 1; i < capturePercentage.length + 1; i++, rankIndex++) {
for (int i = 1; i < teamData.size + 1; i++, rankIndex++) {
if (i > 1 && teamData.get(i - 2).getScore() == teamData.get(i - 1).getScore()) {
rankIndex--;
}
Expand Down Expand Up @@ -204,7 +213,7 @@ private void setUpVictoryUI() {

scoreboardActors[i][rowIndex++] = nameLabel;

Label victoryLabel = new Label(teamData.get(i - 1).getScore() + " (" + capturePercentage[i - 1] + "%)", game.skin,
Label victoryLabel = new Label(teamData.get(i - 1).getScore() + " (" + teamData.get(i - 1).getCapturePercentage() + "%)", game.skin,
Assets.FontManager.REGULAR.getFontName(), rankData[rankDataIndex].rankColor);
victoryLabel.setAlignment(Align.center);

Expand Down Expand Up @@ -246,20 +255,6 @@ private float getRowHeight() {
return dummyLabel.getPrefHeight() * rowHeightScale;
}

private void getVictoryStrings(PlayerManager playerManager, int rows, int cols, int wallCount) {
teamData = playerManager.getTeamData();
new Sort().sort(teamData, new TeamDataComparator());
this.capturePercentage = new double[teamData.size];

DecimalFormat df = new DecimalFormat("#.##");
for (int i = 0; i < teamData.size; i++) {
double capturePercentage = 100 * (teamData.get(i).getScore() / (((double) rows * cols) - wallCount));
capturePercentage = Double.parseDouble(df.format(capturePercentage));

this.capturePercentage[i] = capturePercentage;
}
}

private void setUpNextButton() {
UIButtonManager uiButtonManager = new UIButtonManager(stage, game.getScaleFactor(), usedTextures);
HoverImageButton nextButton = uiButtonManager.addNextButtonToStage(game.assets.getBackButtonTexture());
Expand Down

0 comments on commit 238de43

Please sign in to comment.