Coverage Summary for Class: GameScreen (io.github.unisim.ui)
| Class |
Method, %
|
Branch, %
|
Line, %
|
| GameScreen |
0%
(0/18)
|
0%
(0/76)
|
0%
(0/171)
|
| GameScreen$1 |
0%
(0/1)
|
0%
(0/1)
|
| Total |
0%
(0/19)
|
0%
(0/76)
|
0%
(0/172)
|
package io.github.unisim.ui;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import io.github.unisim.AchievementManager;
import io.github.unisim.GameState;
import io.github.unisim.Leaderboard;
import io.github.unisim.MoneyClass;
import io.github.unisim.ReputationClass;
import io.github.unisim.Score;
import io.github.unisim.Timer;
import io.github.unisim.building.BuildingType;
import io.github.unisim.world.UiInputProcessor;
import io.github.unisim.world.World;
import io.github.unisim.world.WorldInputProcessor;
/**
* Game screen where the main game is rendered and controlled.
* Supports pausing the game with a pause menu.
*/
public class GameScreen implements Screen {
private World world = new World(this);
private Stage stage = new Stage(new ScreenViewport());
private InfoBar infoBar;
private BuildingMenu buildingMenu;
private Timer timer;
private InputProcessor uiInputProcessor = new UiInputProcessor(stage);
private InputProcessor worldInputProcessor = new WorldInputProcessor(world);
private InputMultiplexer inputMultiplexer = new InputMultiplexer();
private Popups popups = new Popups(this);
private int prevTime;
private Leaderboard leaderboard = new Leaderboard();
private ShapeRenderer shapeRenderer;
private BitmapFont font;
private GlyphLayout layout;
private Score scoreClass = new Score("", 0);
private MoneyClass moneyClass;
private ReputationClass reputationClass;
private int prevSec = 0;
private BuildingType types;
/**
* Constructor for the GameScreen.
*/
public GameScreen() {
buildingMenu = new BuildingMenu(stage, world);
if (world.getMultiplier() == 1f) {
moneyClass = new MoneyClass(15000);
reputationClass = new ReputationClass(60);
} else if (world.getMultiplier() == 1.5f) {
moneyClass = new MoneyClass(20000);
reputationClass = new ReputationClass(70);
} else {
moneyClass = new MoneyClass(10000);
reputationClass = new ReputationClass(50);
}
timer = new Timer(300_000);// 300_000
infoBar = new InfoBar(stage, timer, world, scoreClass, moneyClass, reputationClass, this);
prevTime = timer.getCurrGameYear();
shapeRenderer = new ShapeRenderer();
font = new BitmapFont(Gdx.files.internal("ui/score.fnt"));
layout = new GlyphLayout();
inputMultiplexer.addProcessor(GameState.fullscreenInputProcessor);
inputMultiplexer.addProcessor(stage);
inputMultiplexer.addProcessor(uiInputProcessor);
inputMultiplexer.addProcessor(worldInputProcessor);
}
@Override
public void show() {
buildingMenu = new BuildingMenu(stage, world);
scoreClass.setPlayerName(GameState.settings.getPlayerName());
GameState.musicManager.stopMusic();
GameState.musicManager.resumeMusic();
GameState.musicManager.playGameMusic();
popups.resultPopup("Click to start the game!", true);
}
@Override
public void render(float delta) {
world.render();
timeScore();
if (this.scoreClass.getScore() >= 10000
&& AchievementManager.getInstance().getAchievements().get(1).isUnlocked() == false) {
addMoneyRep(4000, 10);
String reward = "<html><p>Achievement: Veteran builder</p><p>You have received $4000 and 10% rep</p><html>";
AchievementManager.getInstance().setReward(reward);
AchievementManager.getInstance().checkAchievement("VETERAN");
world.setPopup("reward");
}
checkPopup();
float dt = Gdx.graphics.getDeltaTime();
if (!GameState.paused && !GameState.gameOver) {
if (!timer.tick(dt * 1000) || reputationClass.getRep() == 0) {
GameState.gameOver = true;
}
}
stage.act(dt);
infoBar.update();
buildingMenu.update();
stage.draw();
if (GameState.paused) {
Gdx.gl.glEnable(GL20.GL_BLEND); // Enable blending for transparency
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
// Set the color to black with 50% opacity
shapeRenderer.setColor(0, 0, 0, 0.5f);
// Draw the rectangle covering half the screen (top or bottom half)
shapeRenderer.rect(0, 0, Gdx.graphics.getWidth() + 1000, Gdx.graphics.getHeight() + 1000);
shapeRenderer.end();
Gdx.gl.glDisable(GL20.GL_BLEND);
SpriteBatch batch = new SpriteBatch();
batch.begin();
String pauseText = "Paused!";
layout.setText(font, pauseText);
float textWidth = layout.width;
float textHeight = layout.height;
float centerX = (Gdx.graphics.getWidth() - textWidth) / 2;
float centerY = (Gdx.graphics.getHeight() - textHeight) / 2 + 50;
font.draw(batch, pauseText, centerX, centerY);
batch.end();
batch.dispose();
}
if (GameState.gameOver) {
GameState.gameOverData.setScore(scoreClass.getScore());
leaderboard.addScore(GameState.settings.getPlayerName(), scoreClass.getScore());
GameState.gameOverData.setScore(scoreClass.getScore());
GameState.currentScreen = GameState.gameOverScreen;
}
if (popups.getActive()) {
world.selectedBuilding = null;
GameState.paused = true;
Gdx.input.setInputProcessor(null);
} else {
Gdx.input.setInputProcessor(inputMultiplexer);
if (timer.getCurrGameYear() != 0 && timer.getCurrGameYear() != prevTime && timer.getCurrRemMinutes() != 0) {
prevTime = timer.getCurrGameYear();
popups.event();
}
}
if (GameState.paused && world.selectedBuilding != null) {
if (world.getLocation() != null) {
world.setBuildingNull(world.getLocation(), world.getFlipped(), world.getSize());
} else {
world.selectedBuilding = null;
world.selectedBuildingUpdated = true;
}
}
}
@Override
public void resize(int width, int height) {
world.resize(width, height);
stage.getViewport().update(width, height, true);
infoBar.resize(width, height);
buildingMenu.resize(width, height);
}
@Override
public void pause() {
GameState.paused = true;
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (Gdx.app != null) {
leaderboard.addScore(GameState.settings.getPlayerName(), scoreClass.getScore());
GameState.gameOverData.setScore(scoreClass.getScore());
}
}));
}
@Override
public void resume() {
Gdx.input.setInputProcessor(inputMultiplexer);
if (GameState.gameOver) {
GameState.gameOver = false;
GameState.paused = true;
timer.reset();
world.reset();
infoBar.reset();
buildingMenu.reset();
}
}
@Override
public void hide() {
GameState.musicManager.pauseMusic();
}
@Override
public void dispose() {
world.dispose();
stage.dispose();
shapeRenderer.dispose();
font.dispose();
}
public String getTimer() {
return timer.getRemTime();
}
public void calcBuild(String buildingType, Boolean newBuilding) {
int money = 0, rep = 0;
money = world.selectedBuilding.cost;
rep = world.selectedBuilding.rep;
if (newBuilding == true) { // Placing a new building
this.scoreClass.addScore((int) ((money + rep) / 3));
} else {// Moving an already placed building
money = money / 2;
rep = 0;
}
moneyClass.remMoney(money);
reputationClass.addRep(rep);
}
public boolean buildCheck() {
if (this.moneyClass.getMoney() < world.selectedBuilding.cost) {
popups.resultPopup("You don't have enough money to build this!", true);
return false;
}
return true;
}
public boolean buildCheckOld() {
int cost = (int) (world.selectedBuilding.cost / 2);
if (this.moneyClass.getMoney() < cost && this.moneyClass.getMoney() != cost) {
popups.resultPopup("You don't have enough money to move this!", true);
return false;
}
return true;
}
public boolean checkMoney(int money) {
if (this.moneyClass.getMoney() < money) {
return false;
}
return true;
}
public boolean checkRep(int rep) {
if (this.reputationClass.getRep() < rep) {
return false;
}
return true;
}
public void addMoneyRep(int money, int rep) {
this.moneyClass.addMoney(money);
this.reputationClass.addRep(rep);
}
@SuppressWarnings("static-access")
public void timeScore() {
int sec = timer.getCurrRemSeconds();
if (sec != prevSec) {
if ((sec % 15 == 0)) {
prevSec = sec;
int remove = (int) (15 * (2 - world.getMultiplier()));
reputationClass.remRep(remove);
}
// Add money based on amount of buildings and their type every 30 seconds
if (sec % 30 == 0) {
float multiplier = world.getMultiplier();
for (BuildingType type : types.values()) {
int money = (int) (world.getBuildingCount(type) * 100 * multiplier);
scoreClass.addScore((int) (money * reputationClass.getRep() / 10));
switch (type) {
case RECREATION:
moneyClass.addMoney(money);
break;
case LEARNING:
moneyClass.addMoney(money);
break;
case SLEEPING:
moneyClass.addMoney(money);
break;
case EATING:
moneyClass.addMoney(money);
break;
default:
break;
}
}
}
if (sec % 45 == 0) {
moneyClass.addMoney(30 * reputationClass.getRep());
}
}
}
public void checkPopup() {
if (world.getPopup() != "null") {
if (world.getPopup() == "paused") {
popups.resultPopup("You can't build while paused!", false);
} else if (world.getPopup() == "building") {
if (world.getLocation() != null) {
world.setBuildingNull(world.getLocation(), world.getFlipped(), world.getSize());
} else {
world.selectedBuilding = null;
world.selectedBuildingUpdated = true;
}
popups.resultPopup("You have to deselect your building!", true);
} else if (world.getPopup() == "invalid") {
popups.resultPopup("You can't build here!", true);
} else if (world.getPopup() == "reward") {
String result = AchievementManager.getInstance().getReward();
popups.resultPopup(result, true);
}
world.setPopup("null");
}
}
}