Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoWS committed Jun 26, 2024
1 parent 8d2b783 commit 8bcfb9c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 31 deletions.
19 changes: 18 additions & 1 deletion core/src/main/java/com/space/game/managers/InputManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@ public boolean keyDown(int keycode) {
return false;
}
switch (keycode) {
//virar caso o jogador pressione a seta pra esquerda
case Keys.LEFT:
turningLeft = true;
break;
//virar caso o jogador pressione a seta pra direita
case Keys.RIGHT:
turningRight = true;
break;

case Keys.A:
turningLeft = true;
break;
case Keys.D:
turningRight = true;
break;
case Keys.SPACE:
spaceship.fire();
if(SpaceGame.getGame().getMapManager().isWaveActive()){
spaceship.fire();
}
break;
case Keys.P:
if(SpaceGame.getGame().getMapManager().isWaveActive()){
Expand Down Expand Up @@ -62,6 +73,12 @@ public boolean keyUp(int keycode) {
return false;
}
switch (keycode) {
case Keys.LEFT:
turningLeft = false;
break;
case Keys.RIGHT:
turningRight = false;
break;
case Keys.A:
turningLeft = false;
break;
Expand Down
73 changes: 46 additions & 27 deletions core/src/main/java/com/space/game/managers/ScoreManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class ScoreManager {

// pegar password no drive
private static final String CONNECTION_STRING = "mongodb+srv://eduardows:<password>@cluster0.bt0tzst.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
private static final String CONNECTION_STRING = "---";
private static final String DATABASE_NAME = "game_database";
private static final String COLLECTION_NAME = "scores";
private static final String FILE_PATH = "data/scores.csv";
Expand All @@ -40,6 +40,8 @@ public class ScoreManager {
private MongoDatabase database;
private MongoCollection<Document> collection;

private boolean error;

public static class ScoreEntry {
public String playerName;
public int score;
Expand All @@ -64,46 +66,59 @@ public ScoreManager() {
database = mongoClient.getDatabase(DATABASE_NAME);
collection = database.getCollection(COLLECTION_NAME);
System.out.println("Successfully connected to MongoDB!");
this.error = false;
} catch (MongoException e) {
e.printStackTrace();
this.error = true;
}
}

public void saveGlobalScore(String playerName, int score) {
List<ScoreEntry> scoresList = loadGlobalScores();
try{
List<ScoreEntry> scoresList = loadGlobalScores();

if (scoresList.size() < 10 || score > scoresList.get(scoresList.size() - 1).score) {
scoresList.add(new ScoreEntry(playerName, score));
}
if (scoresList.size() < 10 || score > scoresList.get(scoresList.size() - 1).score) {
scoresList.add(new ScoreEntry(playerName, score));
}

scoresList.sort(Comparator.comparingInt(o -> -o.score));
scoresList.sort(Comparator.comparingInt(o -> -o.score));

if (scoresList.size() > 10) {
scoresList = scoresList.subList(0, 10);
}
if (scoresList.size() > 10) {
scoresList = scoresList.subList(0, 10);
}

collection.drop(); // Clear the collection before inserting new scores
for (ScoreEntry entry : scoresList) {
Document doc = new Document("playerName", entry.playerName)
.append("score", entry.score);
collection.insertOne(doc);
collection.drop(); // Clear the collection before inserting new scores
for (ScoreEntry entry : scoresList) {
Document doc = new Document("playerName", entry.playerName)
.append("score", entry.score);
collection.insertOne(doc);
}
System.out.println("Global score of " + score + " saved for player " + playerName);
} catch (MongoException e) {
e.printStackTrace();
}
System.out.println("Global score of " + score + " saved for player " + playerName);
}

public List<ScoreEntry> loadGlobalScores() {
SpaceGame.getLogger().debug("Loading global scores");
List<ScoreEntry> scoresList = new ArrayList<>();
Consumer<Document> processDocument = document -> {
String playerName = document.getString("playerName");
int score = document.getInteger("score");
scoresList.add(new ScoreEntry(playerName, score));
};
collection.find().forEach(processDocument);
scoresList.sort(Comparator.comparingInt(o -> -o.score)); // Ordenate in descending order
SpaceGame.getLogger().debug("Global scores loaded");
// SpaceGame.getLogger().error("Error loading global scores", new Exception("Test exception"));
return scoresList;
try{
SpaceGame.getLogger().debug("Loading global scores");
List<ScoreEntry> scoresList = new ArrayList<>();
Consumer<Document> processDocument = document -> {
String playerName = document.getString("playerName");
int score = document.getInteger("score");
scoresList.add(new ScoreEntry(playerName, score));
};
collection.find().forEach(processDocument);
scoresList.sort(Comparator.comparingInt(o -> -o.score)); // Ordenate in descending order
SpaceGame.getLogger().debug("Global scores loaded");
// SpaceGame.getLogger().error("Error loading global scores", new Exception("Test exception"));
this.error = false;
return scoresList;
} catch (MongoException e) {
e.printStackTrace();
this.error = true;
return new ArrayList<>();
}
}

public void saveLocalScore(String playerName, int score) {
Expand Down Expand Up @@ -176,4 +191,8 @@ public void close() {
mongoClient.close();
}
}

public boolean isError() {
return error;
}
}
18 changes: 16 additions & 2 deletions core/src/main/java/com/space/game/managers/UIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void displayGameControls() {

// Controles do jogo
String[] actions = { "Turn Left", "Turn Right", "Shoot", "Pause Game", "Prev. Song", "Pause Song", "Next Song"};
String[] controls = { "A", "D", "Spacebar", "P", "Q", "W", "E"};
String[] controls = { "A | Left Arrow", "D | Right Arrow", "Spacebar", "P", "Q", "W", "E"};

// Desenhar controles
float y = startY;
Expand Down Expand Up @@ -156,7 +156,7 @@ public void displayGameControls() {
public void displayGameInfo(Spaceship spaceship) {
// Exibir informações do jogo como munição e hordas
// Colocar cor branca
font30.setColor(Color.WHITE);
font30.setColor(cian_color);
String ammoText = "AMMO: " + spaceship.getAmmunitions();
GlyphLayout ammoLayout = new GlyphLayout(font30, ammoText);
float ammo_x = game.getWorldWidth() / const_larg;
Expand Down Expand Up @@ -184,6 +184,20 @@ public void displayGameInfo(Spaceship spaceship) {

}

public void displayError(String error) {
GlyphLayout errorLayout = new GlyphLayout(font30, error);
float error_x = game.getWorldWidth() / 2 - errorLayout.width / 2;
float error_y = game.getWorldHeight() / 2 + errorLayout.height;
font30.draw(batch, error, error_x, error_y);

String backText = "Backspace. Back";
GlyphLayout backLayout = new GlyphLayout(font30, backText);
// float back_x = game.getWorldWidth() / 2 - game.getWorldWidth() / 4 - backLayout.width / 2;
float back_x = game.getWorldWidth() / 2 - backLayout.width / 2;
float back_y = game.getWorldHeight() * 0.1f;
font30.draw(batch, backText, back_x, back_y);
}

public void displayGameOverInfo(Spaceship spaceship, float gameoverTimer, float TIME_TO_GAMEOVER) {
// Calcular a porcentagem do tempo decorrido
float progress = gameoverTimer / TIME_TO_GAMEOVER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public void enter() {

@Override
public void update(SpriteBatch batch) {
uiManager.displayScores(scoresList, true);
if (scoreManager.isError()) {
uiManager.displayError("Error loading global scores, please contact the developer: [email protected]");

}
else {
uiManager.displayScores(scoresList, true);
}
handleInput();
}

Expand Down

0 comments on commit 8bcfb9c

Please sign in to comment.