Skip to content

Commit 6b2a3c3

Browse files
authored
Merge pull request #207 from SakshamKaundal/fixing-audio
Added new Audio Manager to manage Audio accross the game
2 parents 1e43ef6 + d6c9ba3 commit 6b2a3c3

File tree

11 files changed

+176
-93
lines changed

11 files changed

+176
-93
lines changed

src/main/java/com/dinosaur/dinosaurexploder/components/GreenDinoComponent.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.dinosaur.dinosaurexploder.utils.LevelManager;
1111
import javafx.geometry.Point2D;
1212
import javafx.util.Duration;
13+
import com.dinosaur.dinosaurexploder.utils.AudioManager;
1314

1415
import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
1516
/**
@@ -20,16 +21,13 @@ public class GreenDinoComponent extends Component implements Dinosaur {
2021
double verticalSpeed = 1.5;
2122
private final LocalTimer timer = FXGL.newLocalTimer();
2223
private boolean isPaused = false;
23-
private boolean isMuted = false;
24+
2425

2526
public void setPaused(boolean paused) {
2627
isPaused = paused;
2728
}
2829

29-
public void setMuted(boolean muted) {
30-
isMuted = muted;
31-
}
32-
30+
3331
@Override
3432
public void onAdded(){
3533
//Get the current enemy speed from the level manager
@@ -61,9 +59,9 @@ public void onUpdate(double ptf) {
6159
*/
6260
@Override
6361
public void shoot() {
64-
if(!isMuted) {
65-
FXGL.play(GameConstants.ENEMY_SHOOT_SOUND);
66-
}
62+
63+
AudioManager.getInstance().playSound(GameConstants.SHOOT_SOUND);
64+
6765
Point2D center = entity.getCenter();
6866
Vec2 direction = Vec2.fromAngle(entity.getRotation() +90);
6967
spawn("basicEnemyProjectile",

src/main/java/com/dinosaur/dinosaurexploder/components/PlayerComponent.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.dinosaur.dinosaurexploder.interfaces.Player;
1313
import com.dinosaur.dinosaurexploder.model.GameData;
1414
import com.dinosaur.dinosaurexploder.view.DinosaurGUI;
15+
import com.dinosaur.dinosaurexploder.utils.AudioManager;
1516

1617
import javafx.geometry.Point2D;
1718
import javafx.scene.image.Image;
@@ -102,10 +103,8 @@ public void moveLeft() {
102103
* This method is overriding the superclass method to the shooting from the
103104
* player and spawning of the new bullet
104105
*/
105-
public void shoot(boolean muted) {
106-
if (!muted) {
107-
FXGL.play(GameConstants.SHOOT_SOUND);
108-
}
106+
public void shoot() {
107+
AudioManager.getInstance().playSound(GameConstants.SHOOT_SOUND);
109108
Point2D center = entity.getCenter();
110109
Vec2 direction = Vec2.fromAngle(entity.getRotation() - 90);
111110
System.out.println("Shoot with selected weapon: " + selectedWeapon);

src/main/java/com/dinosaur/dinosaurexploder/components/RedDinoComponent.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.dinosaur.dinosaurexploder.constants.GameConstants;
1212
import javafx.geometry.Point2D;
1313
import javafx.util.Duration;
14+
import com.dinosaur.dinosaurexploder.utils.AudioManager;
1415

1516
import static com.almasb.fxgl.dsl.FXGLForKtKt.*;
1617

@@ -28,7 +29,7 @@ public RedDinoComponent(GameTimer gameTimer) {
2829
}
2930

3031
private boolean isPaused = false;
31-
private boolean isMuted = false;
32+
3233

3334
boolean firstTime = true;
3435
private LevelManager levelManager;
@@ -58,9 +59,7 @@ public void setPaused(boolean paused) {
5859
isPaused = paused;
5960
}
6061

61-
public void setMuted(boolean muted) {
62-
isMuted = muted;
63-
}
62+
6463

6564
@Override
6665
public void onAdded() {
@@ -106,9 +105,9 @@ public void onUpdate(double ptf) {
106105
*/
107106
@Override
108107
public void shoot() {
109-
if (!isMuted) {
110-
FXGL.play(GameConstants.ENEMY_SHOOT_SOUND);
111-
}
108+
109+
AudioManager.getInstance().playSound(GameConstants.SHOOT_SOUND);
110+
112111
Point2D center = entity.getCenter();
113112
Vec2 direction = Vec2.fromAngle(entity.getRotation() + 90 + random(-45, 45));
114113
spawn("basicEnemyProjectile",

src/main/java/com/dinosaur/dinosaurexploder/constants/GameConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class GameConstants {
3939
*/
4040
public static final String ENEMY_SHOOT_SOUND = "enemyShoot.wav";
4141
public static final String SHOOT_SOUND = "shoot.wav";
42-
public static final String MAIN_MENU_SOUND = "/assets/sounds/mainMenu.wav";
42+
public static final String MAIN_MENU_SOUND = "mainMenu.wav";
4343
public static final String BACKGROUND_SOUND ="gameBackground.wav";
4444
public static final String ENEMY_EXPLODE_SOUND = "enemyExplode.wav";
4545
public static final String PLAYER_HIT_SOUND = "playerHit.wav";

src/main/java/com/dinosaur/dinosaurexploder/controller/BossSpawner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.dinosaur.dinosaurexploder.components.RedDinoComponent;
66
import com.dinosaur.dinosaurexploder.model.Settings;
77
import com.dinosaur.dinosaurexploder.utils.LevelManager;
8-
8+
import com.dinosaur.dinosaurexploder.utils.AudioManager;
99
import static com.almasb.fxgl.dsl.FXGL.getAppCenter;
1010
import static com.almasb.fxgl.dsl.FXGL.getAppWidth;
1111
import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
@@ -24,7 +24,7 @@ public BossSpawner(Settings settings, LevelManager levelManager){
2424

2525
public void spawnNewBoss(){
2626
redDino = spawn("redDino", getAppCenter().getX() - 45, 50);
27-
redDino.getComponent(RedDinoComponent.class).setMuted(settings.isMuted());
27+
2828
redDino.getComponent(RedDinoComponent.class).setLevelManager(levelManager);
2929

3030
healthBar = spawn("healthBar", getAppWidth()-215, 15);

src/main/java/com/dinosaur/dinosaurexploder/controller/DinosaurController.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.dinosaur.dinosaurexploder.view.DinosaurGUI;
1414
import com.dinosaur.dinosaurexploder.utils.LanguageManager;
1515
import com.dinosaur.dinosaurexploder.view.GameOverDialog;
16+
import com.dinosaur.dinosaurexploder.utils.AudioManager; // <-- ADD THIS IMPORT
1617
import javafx.scene.input.KeyCode;
1718
import javafx.scene.paint.Color;
1819
import javafx.scene.shape.Rectangle;
@@ -78,7 +79,7 @@ public void initInput() {
7879
onKey(KeyCode.LEFT, () -> player.getComponent(PlayerComponent.class).moveLeft());
7980
onKey(KeyCode.RIGHT, () -> player.getComponent(PlayerComponent.class).moveRight());
8081

81-
onKeyDown(KeyCode.SPACE, () -> player.getComponent(PlayerComponent.class).shoot(settings.isMuted()));
82+
onKeyDown(KeyCode.SPACE, () -> player.getComponent(PlayerComponent.class).shoot());
8283

8384
onKey(KeyCode.W, () -> player.getComponent(PlayerComponent.class).moveUp());
8485
onKey(KeyCode.S, () -> player.getComponent(PlayerComponent.class).moveDown());
@@ -95,9 +96,7 @@ public void initGame() {
9596
bossSpawner = new BossSpawner(settings, levelManager);
9697
CoinSpawner coinSpawner = new CoinSpawner(10, 1.0);
9798

98-
if (!settings.isMuted()) {
99-
FXGL.play(GameConstants.BACKGROUND_SOUND);
100-
}
99+
AudioManager.getInstance().playMusic(GameConstants.BACKGROUND_SOUND);
101100

102101
new CountdownAnimation(3).startCountdown(() -> {
103102
resumeEnemySpawning();
@@ -136,7 +135,6 @@ private void spawnEnemies() {
136135
} else {
137136
if (!isSpawningPaused && random(0, 2) < 2) {
138137
Entity greenDino = spawn("greenDino", random(0, getAppWidth() - 80), -50);
139-
greenDino.getComponent(GreenDinoComponent.class).setMuted(settings.isMuted());
140138
}
141139
}
142140
}, seconds(levelManager.getEnemySpawnRate()));
@@ -261,9 +259,7 @@ public void initPhysics() {
261259
if (random(0, 100) < 5) {
262260
spawn("heart", greenDino.getX(), greenDino.getY());
263261
}
264-
if (!settings.isMuted()) {
265-
FXGL.play(GameConstants.ENEMY_EXPLODE_SOUND);
266-
}
262+
AudioManager.getInstance().playSound(GameConstants.ENEMY_EXPLODE_SOUND);
267263
projectile.removeFromWorld();
268264
greenDino.removeFromWorld();
269265
if (collisionHandler.isLevelUpAfterHitDino(
@@ -286,9 +282,7 @@ public void initPhysics() {
286282
onCollisionBegin(EntityType.PROJECTILE, EntityType.RED_DINO, (projectile, redDino) -> {
287283
spawn("explosion", redDino.getX() - 25, redDino.getY() - 30);
288284
projectile.removeFromWorld();
289-
if (!settings.isMuted()) {
290-
FXGL.play(GameConstants.ENEMY_EXPLODE_SOUND);
291-
}
285+
AudioManager.getInstance().playSound(GameConstants.ENEMY_EXPLODE_SOUND);
292286
collisionHandler.handleHitBoss(redDino.getComponent(RedDinoComponent.class));
293287

294288
if (redDino.getComponent(RedDinoComponent.class).getLives() == 0) {
@@ -312,43 +306,33 @@ public void initPhysics() {
312306

313307
onCollisionBegin(EntityType.PROJECTILE, EntityType.ENEMY_PROJECTILE, (projectile, enemyProjectile) -> {
314308
spawn("explosion", enemyProjectile.getX() - 25, enemyProjectile.getY() - 30);
315-
if (!settings.isMuted()) {
316-
FXGL.play(GameConstants.ENEMY_EXPLODE_SOUND);
317-
}
309+
AudioManager.getInstance().playSound(GameConstants.ENEMY_EXPLODE_SOUND);
318310
projectile.removeFromWorld();
319311
enemyProjectile.removeFromWorld();
320312
});
321313

322314
onCollisionBegin(EntityType.ENEMY_PROJECTILE, EntityType.PLAYER, (projectile, player) -> {
323-
if (!settings.isMuted()) {
324-
FXGL.play(GameConstants.PLAYER_HIT_SOUND);
325-
}
315+
AudioManager.getInstance().playSound(GameConstants.PLAYER_HIT_SOUND);
326316
projectile.removeFromWorld();
327317
System.out.println("You got hit !\n");
328318
damagePlayer();
329319
});
330320

331321
onCollisionBegin(EntityType.PLAYER, EntityType.GREEN_DINO, (player, greenDino) -> {
332-
if (!settings.isMuted()) {
333-
FXGL.play(GameConstants.PLAYER_HIT_SOUND);
334-
}
322+
AudioManager.getInstance().playSound(GameConstants.PLAYER_HIT_SOUND);
335323
greenDino.removeFromWorld();
336324
System.out.println("You touched a dino !");
337325
damagePlayer();
338326
});
339327

340328
onCollisionBegin(EntityType.PLAYER, EntityType.RED_DINO, (player, redDino) -> {
341-
if (!settings.isMuted()) {
342-
FXGL.play(GameConstants.PLAYER_HIT_SOUND);
343-
}
329+
AudioManager.getInstance().playSound(GameConstants.PLAYER_HIT_SOUND);
344330
System.out.println("You touched a red dino !");
345331
damagePlayer();
346332
});
347333

348334
onCollisionBegin(EntityType.PLAYER, EntityType.COIN, (player, coin) -> {
349-
if (!settings.isMuted()) {
350-
FXGL.play(GameConstants.COIN_GAIN);
351-
}
335+
AudioManager.getInstance().playSound(GameConstants.COIN_GAIN);
352336
coin.removeFromWorld();
353337
System.out.println("You touched a coin!");
354338
BombComponent bombComponent = null;
@@ -357,9 +341,7 @@ public void initPhysics() {
357341
});
358342

359343
onCollisionBegin(EntityType.PLAYER, EntityType.HEART, (player, heart) -> {
360-
if (!settings.isMuted()) {
361-
FXGL.play(GameConstants.HEART_HIT_SOUND);
362-
}
344+
AudioManager.getInstance().playSound(GameConstants.HEART_HIT_SOUND);
363345
heart.removeFromWorld();
364346
System.out.println("You touched a heart!");
365347
life.getComponent(LifeComponent.class).increaseLife(1);

src/main/java/com/dinosaur/dinosaurexploder/interfaces/Player.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public interface Player {
2828
* Summary :
2929
* This handles with the shooting from the player and spawning of the new bullet
3030
*/
31-
void shoot(boolean muted);
31+
void shoot();
3232
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.dinosaur.dinosaurexploder.utils;
2+
3+
import javafx.scene.media.Media;
4+
import javafx.scene.media.MediaPlayer;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class AudioManager {
9+
private static AudioManager instance;
10+
private boolean isMuted = false;
11+
private double volume = 1.0;
12+
private final List<MediaPlayer> activePlayers = new ArrayList<>();
13+
private MediaPlayer backgroundPlayer;
14+
15+
private AudioManager() {}
16+
17+
public static AudioManager getInstance() {
18+
if (instance == null) {
19+
instance = new AudioManager();
20+
}
21+
return instance;
22+
}
23+
24+
public void setMuted(boolean muted) {
25+
this.isMuted = muted;
26+
if (backgroundPlayer != null) backgroundPlayer.setMute(muted);
27+
// Mute/unmute all active sound effects
28+
for (MediaPlayer player : activePlayers) {
29+
player.setMute(muted);
30+
}
31+
}
32+
33+
public boolean isMuted() {
34+
return isMuted;
35+
}
36+
37+
public void setVolume(double volume) {
38+
this.volume = volume;
39+
if (backgroundPlayer != null) backgroundPlayer.setVolume(volume);
40+
// Update volume for all currently playing sound effects
41+
for (MediaPlayer player : activePlayers) {
42+
player.setVolume(volume);
43+
}
44+
}
45+
46+
public double getVolume() {
47+
return volume;
48+
}
49+
50+
public void playSound(String soundFile) {
51+
if (isMuted) return;
52+
try {
53+
String resourcePath = "/assets/sounds/" + soundFile;
54+
var url = getClass().getResource(resourcePath);
55+
if (url == null) {
56+
System.err.println("Sound resource not found: " + resourcePath);
57+
return;
58+
}
59+
MediaPlayer player = new MediaPlayer(new Media(url.toExternalForm()));
60+
player.setVolume(volume);
61+
player.setMute(isMuted);
62+
player.play();
63+
activePlayers.add(player);
64+
player.setOnEndOfMedia(() -> {
65+
player.dispose();
66+
activePlayers.remove(player);
67+
});
68+
} catch (Exception e) {
69+
System.err.println("Could not play sound: " + soundFile);
70+
e.printStackTrace();
71+
}
72+
}
73+
74+
public void playMusic(String soundFile) {
75+
stopMusic();
76+
try {
77+
String resourcePath = "/assets/sounds/" + soundFile;
78+
var url = getClass().getResource(resourcePath);
79+
if (url == null) {
80+
System.err.println("Music resource not found: " + resourcePath);
81+
return;
82+
}
83+
backgroundPlayer = new MediaPlayer(new Media(url.toExternalForm()));
84+
backgroundPlayer.setMute(isMuted);
85+
backgroundPlayer.setVolume(volume);
86+
backgroundPlayer.setCycleCount(MediaPlayer.INDEFINITE);
87+
backgroundPlayer.play();
88+
} catch (Exception e) {
89+
System.err.println("Could not play music: " + soundFile);
90+
e.printStackTrace();
91+
}
92+
}
93+
94+
public void stopMusic() {
95+
if (backgroundPlayer != null) {
96+
backgroundPlayer.stop();
97+
backgroundPlayer.dispose();
98+
backgroundPlayer = null;
99+
}
100+
}
101+
102+
public void stopAllSounds() {
103+
stopMusic();
104+
for (MediaPlayer player : new ArrayList<>(activePlayers)) {
105+
player.stop();
106+
player.dispose();
107+
}
108+
activePlayers.clear();
109+
}
110+
}

0 commit comments

Comments
 (0)