From 084a85074936aea5742ce5d16f1a4993a2acef29 Mon Sep 17 00:00:00 2001 From: Moncef STITI Date: Sun, 24 Nov 2024 00:25:36 +0100 Subject: [PATCH] Modification du score --- ...d-BF63fc29ba8dac9.ttf => Contage-Bold.ttf} | Bin .../fr/monkhanny/dorfromantik/Options.java | 2 + .../monkhanny/dorfromantik/enums/Fonts.java | 28 ++++++++++- .../fr/monkhanny/dorfromantik/game/Board.java | 47 ++++++++++-------- .../dorfromantik/game/ScoreManager.java | 40 +++++++++++++++ .../fr/monkhanny/dorfromantik/game/Tile.java | 24 ++++----- .../listeners/GameResizeListener.java | 23 --------- 7 files changed, 106 insertions(+), 58 deletions(-) rename TestV2/ressources/fonts/{Contage-Bold-BF63fc29ba8dac9.ttf => Contage-Bold.ttf} (100%) create mode 100644 TestV2/src/fr/monkhanny/dorfromantik/game/ScoreManager.java delete mode 100644 TestV2/src/fr/monkhanny/dorfromantik/listeners/GameResizeListener.java diff --git a/TestV2/ressources/fonts/Contage-Bold-BF63fc29ba8dac9.ttf b/TestV2/ressources/fonts/Contage-Bold.ttf similarity index 100% rename from TestV2/ressources/fonts/Contage-Bold-BF63fc29ba8dac9.ttf rename to TestV2/ressources/fonts/Contage-Bold.ttf diff --git a/TestV2/src/fr/monkhanny/dorfromantik/Options.java b/TestV2/src/fr/monkhanny/dorfromantik/Options.java index 187fee1..f4e7a67 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/Options.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/Options.java @@ -57,4 +57,6 @@ public class Options { public static final int MAX_TILE_NUMBER = 50; public static boolean FULL_SCREEN = false; + + public static final float SCORE_SIZE = 30f; } diff --git a/TestV2/src/fr/monkhanny/dorfromantik/enums/Fonts.java b/TestV2/src/fr/monkhanny/dorfromantik/enums/Fonts.java index 4f7a722..8814054 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/enums/Fonts.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/enums/Fonts.java @@ -1,7 +1,12 @@ package fr.monkhanny.dorfromantik.enums; +import java.io.File; +import java.io.IOException; +import java.awt.Font; +import java.awt.FontFormatException; + public enum Fonts { - TITLE, BUTTON; + TITLE, BUTTON, SCORE; public String getFontPath() { switch (this) { @@ -9,8 +14,29 @@ public enum Fonts { return "./ressources/fonts/Contage-Black.ttf"; case BUTTON: return "./ressources/fonts/Contage-Regular.ttf"; + case SCORE: + return "./ressources/fonts/Contage-Bold.ttf"; default: throw new IllegalArgumentException("Unexpected value: " + this); } } + + public Font getFont(float size) { + try { + switch (this) { + case TITLE: + return Font.createFont(Font.TRUETYPE_FONT, new File("./ressources/fonts/Contage-Black.ttf")).deriveFont(size); + case BUTTON: + return Font.createFont(Font.TRUETYPE_FONT, new File("./ressources/fonts/Contage-Regular.ttf")).deriveFont(size); + case SCORE: + return Font.createFont(Font.TRUETYPE_FONT, new File("./ressources/fonts/Contage-Bold.ttf")).deriveFont(size); + default: + throw new IllegalArgumentException("Unexpected value: " + this); + } + } catch (IOException | FontFormatException e) { + e.printStackTrace(); + // Retourner une police de secours si le fichier est introuvable ou s'il y a une erreur + return new Font("Arial", Font.PLAIN, (int) size); + } + } } \ No newline at end of file diff --git a/TestV2/src/fr/monkhanny/dorfromantik/game/Board.java b/TestV2/src/fr/monkhanny/dorfromantik/game/Board.java index ddc0a8b..1451181 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/game/Board.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/game/Board.java @@ -1,6 +1,5 @@ package fr.monkhanny.dorfromantik.game; -import fr.monkhanny.dorfromantik.listeners.GameResizeListener; import fr.monkhanny.dorfromantik.listeners.GameZoomListener; import fr.monkhanny.dorfromantik.listeners.GameArrowKeyListener; import fr.monkhanny.dorfromantik.listeners.GameSpaceKeyListener; @@ -8,6 +7,7 @@ import fr.monkhanny.dorfromantik.listeners.GameMouseClickListener; import fr.monkhanny.dorfromantik.listeners.GameMouseWheelListener; import fr.monkhanny.dorfromantik.Options; import fr.monkhanny.dorfromantik.enums.Biome; +import fr.monkhanny.dorfromantik.enums.Fonts; import fr.monkhanny.dorfromantik.enums.TileOrientation; import java.util.ArrayList; @@ -19,13 +19,13 @@ import javax.swing.JFrame; import java.awt.Point; import java.awt.Color; import java.awt.Graphics2D; +import java.awt.Font; // TEMPORAIRE : import java.awt.event.MouseMotionAdapter; // Import pour MouseMotionAdapter import java.awt.AlphaComposite; import java.util.HashMap; import java.util.Map; -import java.awt.Point; /** @@ -44,6 +44,8 @@ public class Board extends JPanel{ private Tile nextTile; private Map biomeGroups; private Point mousePosition; + private ScoreManager scoreManager; + private int currentScore; // Constructeur avec seed public Board(JFrame gameFrame, long seed) { @@ -54,6 +56,8 @@ public class Board extends JPanel{ this.random = new Random(seed); this.game = new Game(seed); + this.scoreManager = new ScoreManager(biomeGroups); + for (Biome biome : Biome.values()) { biomeGroups.put(biome, new BiomeGroup()); } @@ -61,9 +65,6 @@ public class Board extends JPanel{ // Placer une tuile centrale au démarrage initializeCentralTile(); - // Ajouter un écouteur de redimensionnement pour redessiner les tuiles - gameFrame.addComponentListener(new GameResizeListener(this)); - // Ajouter un écouteur de molette de souris pour gérer le zoom gameFrame.addMouseWheelListener(new GameZoomListener(this)); @@ -191,18 +192,10 @@ public class Board extends JPanel{ initializeNextTile(); } - public void repositionCentralTile() { - int centerX = gameFrame.getWidth() / 2; - int centerY = gameFrame.getHeight() / 2; - - // Déplacer la tuile centrale vers le nouveau centre de la fenêtre - centralTile.setPosition(centerX, centerY); - } - public void addTile(Tile tile) { tiles.add(tile); updatePockets(tile); - displayCurrentScore(); + calculateCurrentScore(); } private void updatePockets(Tile newTile) { @@ -253,13 +246,13 @@ public class Board extends JPanel{ return false; } - // Affiche le score actuel - private void displayCurrentScore() { - int totalScore = 0; - for (BiomeGroup group : biomeGroups.values()) { - totalScore += group.calculateTotalScore(); - } - System.out.println("Score actuel : " + totalScore); + private void calculateCurrentScore() { + scoreManager.updateScore(); // Met à jour le score + currentScore = scoreManager.getCurrentScore(); // Récupère le score actuel + } + + public int getCurrentScore() { + return currentScore; } public Random getRandom() { return random; } @@ -427,6 +420,17 @@ public class Board extends JPanel{ g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); // Rétablir l'opacité } + // Obtenez la police SCORE avec une taille ajustée (par exemple, 30 points) + Font scoreFont = Fonts.SCORE.getFont(30f); // Vous pouvez ajuster la taille ici + g.setFont(scoreFont); + + // Calculer la position du score (avec zoom et décalage) + g.setColor(Color.BLACK); + int scoreX = (int) ((getWidth() / (2 * zoomFactor) - offsetX / zoomFactor) - 20); + int scoreY = (int) (40 / zoomFactor - offsetY / zoomFactor); + + // Dessiner le texte du score + g.drawString("Score : " + currentScore, scoreX, scoreY); if (nextTile != null) { // Calculer la position correcte de la nextTile (en tenant compte du zoom et des décalages) @@ -443,6 +447,7 @@ public class Board extends JPanel{ // Rétablir les transformations pour les autres éléments (tuiles existantes, etc.) g2d.translate(offsetX / zoomFactor, offsetY / zoomFactor); // Re-appliquer le décalage g2d.scale(zoomFactor, zoomFactor); // Re-appliquer le zoom + } } } diff --git a/TestV2/src/fr/monkhanny/dorfromantik/game/ScoreManager.java b/TestV2/src/fr/monkhanny/dorfromantik/game/ScoreManager.java new file mode 100644 index 0000000..0b0ba76 --- /dev/null +++ b/TestV2/src/fr/monkhanny/dorfromantik/game/ScoreManager.java @@ -0,0 +1,40 @@ +package fr.monkhanny.dorfromantik.game; + +import java.util.Map; +import fr.monkhanny.dorfromantik.enums.Biome; + +/** + * Classe responsable du calcul et de la gestion du score. + */ +public class ScoreManager { + private int currentScore = 0; + private Map biomeGroups; + + // Constructeur avec la référence aux groupes de biomes + public ScoreManager(Map biomeGroups) { + this.biomeGroups = biomeGroups; + } + + // Méthode pour calculer le score en fonction des groupes de biomes + public void calculateScore() { + int totalScore = 0; + + for (BiomeGroup group : biomeGroups.values()) { + totalScore += group.calculateTotalScore(); + } + + currentScore = totalScore; + } + + // Récupérer le score actuel + public int getCurrentScore() { + return currentScore; + } + + // Mettre à jour le score (si nécessaire) + public void updateScore() { + calculateScore(); + } + + +} diff --git a/TestV2/src/fr/monkhanny/dorfromantik/game/Tile.java b/TestV2/src/fr/monkhanny/dorfromantik/game/Tile.java index 94602d8..386da67 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/game/Tile.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/game/Tile.java @@ -140,22 +140,20 @@ public class Tile extends Cell { } public boolean isAdjacentTo(Tile otherTile) { - // Get the radius of the tiles - int tileRadius = this.getRadius(); + // Obtenir le rayon de la tuile + int tileRadius = this.getRadius(); - // Compute the distance between the center of this tile and the other tile - int deltaX = this.getX() - otherTile.getX(); - int deltaY = this.getY() - otherTile.getY(); - - // Calculate the Euclidean distance between the two tiles - double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); + // Compute the distance between the center of this tile and the other tile + int deltaX = this.getX() - otherTile.getX(); + int deltaY = this.getY() - otherTile.getY(); + + // Calculate the Euclidean distance between the two tiles + double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); - // In a hexagonal grid, two tiles are adjacent if their distance is equal to the diameter (2 * radius) - return distance <= (2 * tileRadius); -} + // In a hexagonal grid, two tiles are adjacent if their distance is equal to the diameter (2 * radius) + return distance <= (2 * tileRadius); + } - - private TileOrientation determineSide(int x, int y) { int radius = this.getRadius(); TileOrientation[] sides = TileOrientation.values(); diff --git a/TestV2/src/fr/monkhanny/dorfromantik/listeners/GameResizeListener.java b/TestV2/src/fr/monkhanny/dorfromantik/listeners/GameResizeListener.java deleted file mode 100644 index 016fcdb..0000000 --- a/TestV2/src/fr/monkhanny/dorfromantik/listeners/GameResizeListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package fr.monkhanny.dorfromantik.listeners; - -import fr.monkhanny.dorfromantik.game.Board; - -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; - -public class GameResizeListener extends ComponentAdapter { - private Board board; - - // Constructeur qui reçoit une instance de Board pour pouvoir accéder à ses méthodes - public GameResizeListener(Board board) { - this.board = board; - } - - // Cette méthode est appelée lorsque la taille de la fenêtre change - @Override - public void componentResized(ComponentEvent e) { - // Réajuster la position de la tuile centrale lorsque la fenêtre est redimensionnée - board.repositionCentralTile(); - board.repaint(); // Demander à Board de redessiner le panneau - } -}