From 238de4358c7b51ced2e6eb8a9fbf7e5816a8177a Mon Sep 17 00:00:00 2001 From: Spikatrix <12792882+Spikatrix@users.noreply.github.com> Date: Thu, 10 Sep 2020 15:14:29 +0530 Subject: [PATCH] TeamData capture percentage cleanups --- core/src/com/cg/zoned/Map.java | 14 ++++++- core/src/com/cg/zoned/TeamData.java | 20 ++++++++++ core/src/com/cg/zoned/screens/GameScreen.java | 11 ++++-- .../com/cg/zoned/screens/VictoryScreen.java | 37 ++++++++----------- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/core/src/com/cg/zoned/Map.java b/core/src/com/cg/zoned/Map.java index 2c8fbbf..de74d9f 100644 --- a/core/src/com/cg/zoned/Map.java +++ b/core/src/com/cg/zoned/Map.java @@ -254,6 +254,7 @@ public void update(PlayerManager playerManager, Player[] players, float delta) { setMapWeights(players); setMapColors(playerManager, players); + updateCapturePercentage(playerManager); } } } @@ -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 = 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++) { @@ -516,7 +528,7 @@ public boolean gameComplete(Array 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; } } diff --git a/core/src/com/cg/zoned/TeamData.java b/core/src/com/cg/zoned/TeamData.java index 92e3640..f0073a1 100644 --- a/core/src/com/cg/zoned/TeamData.java +++ b/core/src/com/cg/zoned/TeamData.java @@ -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; } diff --git a/core/src/com/cg/zoned/screens/GameScreen.java b/core/src/com/cg/zoned/screens/GameScreen.java index fbcbc87..9837368 100644 --- a/core/src/com/cg/zoned/screens/GameScreen.java +++ b/core/src/com/cg/zoned/screens/GameScreen.java @@ -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); @@ -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)); } }); } diff --git a/core/src/com/cg/zoned/screens/VictoryScreen.java b/core/src/com/cg/zoned/screens/VictoryScreen.java index 2beb873..387286b 100644 --- a/core/src/com/cg/zoned/screens/VictoryScreen.java +++ b/core/src/com/cg/zoned/screens/VictoryScreen.java @@ -57,14 +57,13 @@ public class VictoryScreen extends ScreenAdapter implements InputProcessor { private ParticleEffect trailEffect; private Array teamData; - private double[] capturePercentage; private Actor[][] scoreboardActors; private Container