Un peu de ménage dans le git
This commit is contained in:
273
src/fr/monkhanny/dorfromantik/utils/Database.java
Normal file
273
src/fr/monkhanny/dorfromantik/utils/Database.java
Normal file
@@ -0,0 +1,273 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.Reward;
|
||||
|
||||
public class Database {
|
||||
// Chargement des variables d'environnement
|
||||
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/stiti";
|
||||
private static final String LOGIN = "stiti";
|
||||
private static final String PASSWORD = "stiti1234";
|
||||
|
||||
// Variable de passerelle entre le programme et la base de données
|
||||
private Connection database;
|
||||
|
||||
/**
|
||||
* Ouvre la connexion avec la base de données
|
||||
*/
|
||||
public Database() throws SQLException {
|
||||
try {
|
||||
// Chargement du driver MariaDB
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
|
||||
try {
|
||||
// Connexion à la base de données
|
||||
this.database = DriverManager.getConnection(URL, LOGIN, PASSWORD);
|
||||
}catch (SQLException e) {
|
||||
// Gestion de l'erreur de connexion
|
||||
throw new SQLException("Échec de la connexion à la base de données: " + e.getMessage(), e);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
// Si le driver n'est pas trouvé
|
||||
throw new SQLException("Driver MariaDB introuvable dans le classpath", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Connection getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Récupère le seed correspondant au mode de jeu (series_id)
|
||||
* @param seriesId L'ID de la série (mode de jeu)
|
||||
* @return Le seed associé à ce mode de jeu
|
||||
* @throws SQLException Si une erreur se produit lors de la récupération du seed
|
||||
*/
|
||||
public long getSeedBySeriesId(long seriesId) throws SQLException {
|
||||
String query = "SELECT series_id FROM Series WHERE series_id = " + seriesId;
|
||||
long seed = -1; // Valeur par défaut si le seed n'est pas trouvé
|
||||
|
||||
try (Statement stmt = this.database.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(query)) {
|
||||
if (rs.next()) {
|
||||
seed = rs.getLong("series_id");
|
||||
}
|
||||
}
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
||||
public List<PlayerScore> getAllScores(long seriesId) throws SQLException {
|
||||
List<PlayerScore> allScores = new ArrayList<>();
|
||||
|
||||
String query = "SELECT username, score FROM Scores WHERE series_id = ? ORDER BY score DESC";
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
|
||||
stmt.setLong(1, seriesId);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String username = rs.getString("username");
|
||||
if (username == null || username.trim().isEmpty()) {
|
||||
username = "Joueur Anonyme"; // Default name if empty
|
||||
}
|
||||
int score = rs.getInt("score");
|
||||
allScores.add(new PlayerScore(username, score));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allScores;
|
||||
}
|
||||
|
||||
|
||||
public long getSeedByName(String name) throws SQLException {
|
||||
String query = "SELECT series_id FROM Series WHERE name = " + "\'" + name + "\'" +";";
|
||||
long seed = -1; // Valeur par défaut si le seed n'est pas trouvé
|
||||
|
||||
try (Statement stmt = this.database.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(query)) {
|
||||
if (rs.next()) {
|
||||
seed = rs.getLong("series_id");
|
||||
}
|
||||
}
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
||||
public void addScore(String username, long seriesId, int score) throws SQLException {
|
||||
String insertQuery = "INSERT INTO Scores (username, series_id, score) VALUES (?, ?, ?)";
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(insertQuery)) {
|
||||
stmt.setString(1, username);
|
||||
stmt.setLong(2, seriesId);
|
||||
stmt.setInt(3, score);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de l'ajout du score: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void addCustomSeed(String name, long customSeed) throws SQLException {
|
||||
// Vérifier si la seed existe déjà
|
||||
String checkQuery = "SELECT COUNT(*) FROM Series WHERE series_id = ?";
|
||||
try (PreparedStatement checkStmt = this.database.prepareStatement(checkQuery)) {
|
||||
checkStmt.setLong(1, customSeed);
|
||||
ResultSet rs = checkStmt.executeQuery();
|
||||
if (rs.next() && rs.getInt(1) > 0) {
|
||||
// la seed existe déjà
|
||||
return; // Ne pas insérer si la seed existe déjà
|
||||
}
|
||||
}
|
||||
|
||||
// Si la seed n'existe pas, procéder à l'insertion
|
||||
String insertQuery = "INSERT INTO Series (name, series_id, creation_date) VALUES (?, ?, CURRENT_TIMESTAMP)";
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(insertQuery)) {
|
||||
stmt.setString(1, name);
|
||||
stmt.setLong(2, customSeed);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de l'ajout de la seed custom: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Récupère les meilleurs scores des joueurs (limite de 10 scores)
|
||||
* @return une liste de résultats sous forme de tableau d'objets contenant le nom du joueur et son score
|
||||
*/
|
||||
public List<PlayerScore> getTopPlayers() throws SQLException {
|
||||
List<PlayerScore> topPlayers = new ArrayList<>();
|
||||
|
||||
String query = "SELECT username, score FROM Scores ORDER BY score DESC LIMIT 10";
|
||||
try (Statement stmt = this.database.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(query)) {
|
||||
|
||||
while (rs.next()) {
|
||||
String username = rs.getString("username");
|
||||
if (username == null || username.trim().isEmpty()) {
|
||||
username = "Joueur Anonyme"; // Remplacer par "Joueur Anonyme" si le pseudo est vide ou nul
|
||||
}
|
||||
int score = rs.getInt("score");
|
||||
topPlayers.add(new PlayerScore(username, score));
|
||||
}
|
||||
}
|
||||
|
||||
return topPlayers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les scores d'une série spécifique, triés en ordre décroissant (du plus élevé au plus bas)
|
||||
* @param seriesId L'ID de la série
|
||||
* @return Liste des scores pour la série donnée
|
||||
* @throws SQLException En cas d'erreur lors de la récupération des scores
|
||||
*/
|
||||
public List<Integer> getScoresBySeriesId(long seriesId) throws SQLException {
|
||||
List<Integer> scores = new ArrayList<>();
|
||||
|
||||
String query = "SELECT score FROM Scores WHERE series_id = ? ORDER BY score DESC";
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
|
||||
stmt.setLong(1, seriesId);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
scores.add(rs.getInt("score"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If you want the scores to be in descending order (from highest to lowest)
|
||||
Collections.sort(scores, Collections.reverseOrder());
|
||||
|
||||
return scores;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les récompenses d'un utilisateur spécifique
|
||||
* @param username Le nom d'utilisateur pour lequel récupérer les récompenses
|
||||
* @return Liste des récompenses de l'utilisateur
|
||||
* @throws SQLException En cas d'erreur lors de la récupération des récompenses
|
||||
*/
|
||||
public List<Reward> getRewardsByUsername(String username) throws SQLException {
|
||||
List<Reward> rewards = new ArrayList<>();
|
||||
String query = "SELECT r.name, r.description, r.icon_path, ur.is_unlocked " +
|
||||
"FROM UserRewards ur " +
|
||||
"JOIN Rewards r ON ur.reward_id = r.reward_id " +
|
||||
"WHERE ur.username = ?";
|
||||
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
|
||||
stmt.setString(1, username);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String name = rs.getString("name");
|
||||
String description = rs.getString("description");
|
||||
String iconPath = rs.getString("icon_path");
|
||||
boolean isUnlocked = rs.getBoolean("is_unlocked");
|
||||
|
||||
ImageIcon icon = (iconPath != null && !iconPath.isEmpty()) ? new ImageIcon(iconPath) : null;
|
||||
Reward reward = new Reward(name, description, isUnlocked, icon);
|
||||
rewards.add(reward);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rewards;
|
||||
}
|
||||
|
||||
public void unlockRewards(String username, int score) throws SQLException {
|
||||
// Vérifier les récompenses possibles en fonction du score du joueur
|
||||
String query = "SELECT reward_id FROM Rewards WHERE score_threshold <= ?";
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
|
||||
stmt.setInt(1, score);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
long rewardId = rs.getLong("reward_id");
|
||||
|
||||
// Ajouter la récompense à la table UserRewards si elle n'est pas déjà débloquée
|
||||
String checkQuery = "SELECT COUNT(*) FROM UserRewards WHERE username = ? AND reward_id = ?";
|
||||
try (PreparedStatement checkStmt = this.database.prepareStatement(checkQuery)) {
|
||||
checkStmt.setString(1, username);
|
||||
checkStmt.setLong(2, rewardId);
|
||||
|
||||
ResultSet checkRs = checkStmt.executeQuery();
|
||||
if (checkRs.next() && checkRs.getInt(1) == 0) {
|
||||
// Si la récompense n'est pas encore débloquée pour cet utilisateur, l'ajouter
|
||||
String insertQuery = "INSERT INTO UserRewards (username, reward_id, is_unlocked) VALUES (?, ?, 1)";
|
||||
try (PreparedStatement insertStmt = this.database.prepareStatement(insertQuery)) {
|
||||
insertStmt.setString(1, username);
|
||||
insertStmt.setLong(2, rewardId);
|
||||
insertStmt.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
if (this.database != null && !this.database.isClosed()) {
|
||||
this.database.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de la fermeture de la base de données : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
33
src/fr/monkhanny/dorfromantik/utils/FontLoader.java
Normal file
33
src/fr/monkhanny/dorfromantik/utils/FontLoader.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Fonts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Classe utilitaire pour charger des polices à partir de fichiers.
|
||||
* @version 1.0
|
||||
* @author Moncef STITI
|
||||
* @see Fonts
|
||||
* @see Font
|
||||
*/
|
||||
public class FontLoader {
|
||||
|
||||
/**
|
||||
* Charge une police à partir du fichier spécifié.
|
||||
* @param fontEnumName Enumération de la police à charger.
|
||||
* @return La police chargée.
|
||||
* @throws IOException Si une erreur se produit lors de la lecture du fichier.
|
||||
* @throws FontFormatException Si une erreur se produit lors de la création de la police.
|
||||
*/
|
||||
public static Font loadFont(Fonts fontEnumName) throws IOException, FontFormatException {
|
||||
String fontFilePath = fontEnumName.getFontPath();
|
||||
File fontFile = new File(fontFilePath);
|
||||
Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
ge.registerFont(customFont);
|
||||
return customFont;
|
||||
}
|
||||
}
|
70
src/fr/monkhanny/dorfromantik/utils/FontManager.java
Normal file
70
src/fr/monkhanny/dorfromantik/utils/FontManager.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Fonts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FontManager {
|
||||
|
||||
private static Font titleFont;
|
||||
private static Font buttonFont;
|
||||
|
||||
// Charge et applique la police spécifique en fonction de Fonts
|
||||
public static void loadCustomFont(Fonts fontEnum) {
|
||||
try {
|
||||
Font loadedFont = FontLoader.loadFont(fontEnum);
|
||||
if (fontEnum == Fonts.TITLE) {
|
||||
titleFont = loadedFont;
|
||||
} else if (fontEnum == Fonts.BUTTON) {
|
||||
buttonFont = loadedFont;
|
||||
}
|
||||
} catch (IOException | FontFormatException e) {
|
||||
throw new RuntimeException("Failed to load font: " + fontEnum, e);
|
||||
}
|
||||
}
|
||||
|
||||
// Obtient la police du titre avec une taille spécifique
|
||||
public static Font getTitleFont(float size) {
|
||||
if (titleFont == null) {
|
||||
throw new IllegalStateException("Title font not loaded. Please load the font first.");
|
||||
}
|
||||
return titleFont.deriveFont(size);
|
||||
}
|
||||
|
||||
// Obtient la police du bouton avec une taille spécifique
|
||||
public static Font getButtonFont(float size) {
|
||||
if (buttonFont == null) {
|
||||
throw new IllegalStateException("Button font not loaded. Please load the font first.");
|
||||
}
|
||||
return buttonFont.deriveFont(size);
|
||||
}
|
||||
|
||||
// Ajuste la taille de la police du titre selon la taille du composant sans la modifier directement
|
||||
public static Font getAdjustedTitleFont(Component component, float minSize, float maxSize) {
|
||||
if (titleFont == null) {
|
||||
throw new IllegalStateException("Title font not loaded. Please load the font first.");
|
||||
}
|
||||
float newSize = Math.max(minSize, Math.min(maxSize, component.getWidth() / 12f));
|
||||
return titleFont.deriveFont(newSize);
|
||||
}
|
||||
|
||||
// Ajuste la taille de la police du bouton selon la taille du composant sans la modifier directement
|
||||
public static Font getAdjustedButtonFont(Component component, float minSize, float maxSize) {
|
||||
if (buttonFont == null) {
|
||||
throw new IllegalStateException("Button font not loaded. Please load the font first.");
|
||||
}
|
||||
float newSize = Math.max(minSize, Math.min(maxSize, component.getHeight() / 20f));
|
||||
return buttonFont.deriveFont(newSize);
|
||||
}
|
||||
|
||||
// Définir manuellement une police de titre personnalisée
|
||||
public static void setTitleFont(Font font) {
|
||||
titleFont = font;
|
||||
}
|
||||
|
||||
// Définir manuellement une police de bouton personnalisée
|
||||
public static void setButtonFont(Font font) {
|
||||
buttonFont = font;
|
||||
}
|
||||
}
|
74
src/fr/monkhanny/dorfromantik/utils/Hexagon.java
Normal file
74
src/fr/monkhanny/dorfromantik/utils/Hexagon.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.awt.Polygon;
|
||||
|
||||
|
||||
public class Hexagon extends Polygon {
|
||||
private static final int ANGLE_BETWEEN_VERTICES = 60;
|
||||
|
||||
/**
|
||||
* Constructeur d'un hexagone
|
||||
*
|
||||
* @param x Position x du centre de l'hexagone
|
||||
* @param y Position y du centre de l'hexagone
|
||||
* @param radius Rayon de l'hexagone
|
||||
* @param startAngle Angle de départ de l'hexagone
|
||||
*/
|
||||
public Hexagon(int x, int y, int radius, double startAngle) {
|
||||
if (radius <= 0) {
|
||||
throw new IllegalArgumentException("Le rayon doit être supérieur à zéro.");
|
||||
}
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
double angleRad = calculateAngle(i, startAngle);
|
||||
this.addPoint(
|
||||
(int) (x + radius * Math.cos(angleRad)),
|
||||
(int) (y + radius * Math.sin(angleRad))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcule l'angle en radians pour un sommet donné
|
||||
*
|
||||
* @param vertexIndex Index du sommet (0 à 5)
|
||||
* @param startAngle Angle de départ
|
||||
* @return Angle en radians
|
||||
*/
|
||||
private double calculateAngle(int vertexIndex, double startAngle) {
|
||||
return Math.toRadians(vertexIndex * ANGLE_BETWEEN_VERTICES + startAngle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructeur d'un hexagone
|
||||
*
|
||||
* @param center Centre de l'hexagone
|
||||
* @param radius Rayon de l'hexagone
|
||||
* @param startAngle Angle de départ de l'hexagone
|
||||
*/
|
||||
public Hexagon(Point center, int radius, double startAngle) {
|
||||
this(center.x, center.y, radius, startAngle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructeur d'un hexagone
|
||||
*
|
||||
* @param x Position x du centre de l'hexagone
|
||||
* @param y Position y du centre de l'hexagone
|
||||
* @param radius Rayon de l'hexagone
|
||||
*/
|
||||
public Hexagon(int x, int y, int radius) {
|
||||
this(x, y, radius, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructeur d'un hexagone
|
||||
*
|
||||
* @param center Centre de l'hexagone
|
||||
* @param radius Rayon de l'hexagone
|
||||
*/
|
||||
public Hexagon(Point center, int radius) {
|
||||
this(center.x, center.y, radius, 0);
|
||||
}
|
||||
}
|
35
src/fr/monkhanny/dorfromantik/utils/ImageLoader.java
Normal file
35
src/fr/monkhanny/dorfromantik/utils/ImageLoader.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
/**
|
||||
* Classe utilitaire pour charger des images à partir de fichiers.
|
||||
*
|
||||
* @version 1.0
|
||||
* @author Moncef STITI
|
||||
*/
|
||||
public class ImageLoader {
|
||||
/**
|
||||
* Icône de l'application.
|
||||
*/
|
||||
public static final Image APPLICATION_ICON = ImageLoader.loadImage("./ressources/images/Application/Application_Icon.jpg");
|
||||
|
||||
/**
|
||||
* Charge une image à partir du fichier spécifié.
|
||||
*
|
||||
* @param filePath Chemin du fichier image à charger.
|
||||
* @return L'image chargée, ou null si une erreur se produit.
|
||||
*/
|
||||
public static Image loadImage(String filePath) {
|
||||
try {
|
||||
File imageFile = new File(filePath);
|
||||
return ImageIO.read(imageFile);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Erreur lors du chargement de l'image : " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
94
src/fr/monkhanny/dorfromantik/utils/MusicPlayer.java
Normal file
94
src/fr/monkhanny/dorfromantik/utils/MusicPlayer.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Musics;
|
||||
import fr.monkhanny.dorfromantik.enums.Sounds;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
|
||||
public class MusicPlayer {
|
||||
private static Clip musicClip;
|
||||
private static Clip soundClip; // Clip séparé pour les bruitages
|
||||
private static boolean isPlayingMusic = false;
|
||||
private static boolean isPlayingSound = false;
|
||||
|
||||
public static void loadMusic(Musics music) {
|
||||
if (music == Musics.MAIN_MENU_MUSIC) {
|
||||
musicClip = SoundLoader.loadMusic(Musics.MAIN_MENU_MUSIC.getSoundsPath());
|
||||
if (musicClip != null) {
|
||||
setVolume(musicClip, Options.MUSIC_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadSound(Sounds sound) {
|
||||
if (sound == Sounds.SOUNDS1) {
|
||||
soundClip = SoundLoader.loadMusic(Sounds.SOUNDS1.getSoundsPath()); // Utilise soundClip pour les bruitages
|
||||
if (soundClip != null) {
|
||||
setVolume(soundClip, Options.SOUNDS_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void playMusic() {
|
||||
if (musicClip != null && !isPlayingMusic && !Options.MUSIC_MUTED) {
|
||||
musicClip.start();
|
||||
isPlayingMusic = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void playSound() {
|
||||
if (soundClip != null && !isPlayingSound && !Options.SOUNDS_MUTED) {
|
||||
soundClip.start();
|
||||
isPlayingSound = true;
|
||||
soundClip.addLineListener(event -> { // Réinitialiser isPlayingSound à la fin du son
|
||||
if (event.getType() == LineEvent.Type.STOP) {
|
||||
soundClip.setFramePosition(0); // Retour au début du son pour rejouer si nécessaire
|
||||
isPlayingSound = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void pauseMusic() {
|
||||
if (musicClip != null && isPlayingMusic) {
|
||||
musicClip.stop();
|
||||
isPlayingMusic = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void stopMusic() {
|
||||
if (musicClip != null) {
|
||||
musicClip.stop();
|
||||
musicClip.setFramePosition(0);
|
||||
isPlayingMusic = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setVolume(Clip clip, int volume) {
|
||||
if (clip != null) {
|
||||
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
|
||||
float range = volumeControl.getMaximum() - volumeControl.getMinimum();
|
||||
float gain = (range * volume / 100f) + volumeControl.getMinimum();
|
||||
volumeControl.setValue(gain);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPlayingMusic() {
|
||||
return isPlayingMusic;
|
||||
}
|
||||
|
||||
public static boolean isPlayingSound() {
|
||||
return isPlayingSound;
|
||||
}
|
||||
|
||||
public static Clip getMusicClip() {
|
||||
return musicClip;
|
||||
}
|
||||
|
||||
public static Clip getSoundClip() {
|
||||
return soundClip;
|
||||
}
|
||||
}
|
22
src/fr/monkhanny/dorfromantik/utils/PlayerScore.java
Normal file
22
src/fr/monkhanny/dorfromantik/utils/PlayerScore.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
/**
|
||||
* Représente un score d'un joueur avec son nom et son score.
|
||||
*/
|
||||
public class PlayerScore {
|
||||
private String username;
|
||||
private int score;
|
||||
|
||||
public PlayerScore(String username, int score) {
|
||||
this.username = username;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
}
|
23
src/fr/monkhanny/dorfromantik/utils/SoundLoader.java
Normal file
23
src/fr/monkhanny/dorfromantik/utils/SoundLoader.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SoundLoader {
|
||||
|
||||
public static Clip loadMusic(String filePath) {
|
||||
try {
|
||||
File soundFile = new File(filePath);
|
||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundFile);
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(audioStream);
|
||||
return clip;
|
||||
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Failed to load sound at path: " + filePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user