made music and sfx volume adjustable

This commit is contained in:
2025-12-11 18:55:32 +01:00
parent e9fc671135
commit eba67d7cf8
4 changed files with 50 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ import de.miaurizius.jgame2d.tile.TileManager;
import de.miaurizius.jgame2d.tile.interactive.InteractiveTile; import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@@ -50,7 +51,7 @@ public class GamePanel extends JPanel implements Runnable {
public UI ui = new UI(this); public UI ui = new UI(this);
public EventHandler eventH = new EventHandler(this); public EventHandler eventH = new EventHandler(this);
public Sound se = new Sound(); public Sound se = new Sound();
Sound music = new Sound(); public Sound music = new Sound();
Thread gameThread; Thread gameThread;
// ENTITY AND OBJECT // ENTITY AND OBJECT
@@ -197,7 +198,9 @@ public class GamePanel extends JPanel implements Runnable {
// MUSIC // MUSIC
public void playMusic(int i) { public void playMusic(int i) {
Clip c = se.clips[i]; Clip c = music.clips[i];
music.fc = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
music.checkVolume();
if(c.isRunning()) c.stop(); if(c.isRunning()) c.stop();
c.setFramePosition(0); c.setFramePosition(0);
c.start(); c.start();
@@ -208,6 +211,8 @@ public class GamePanel extends JPanel implements Runnable {
} }
public void playSE(int i) { public void playSE(int i) {
Clip c = se.clips[i]; Clip c = se.clips[i];
se.fc = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
se.checkVolume();
if(c.isRunning()) c.stop(); if(c.isRunning()) c.stop();
c.setFramePosition(0); c.setFramePosition(0);
c.start(); c.start();

View File

@@ -381,11 +381,13 @@ public class UI {
// MUSIC VOLUME // MUSIC VOLUME
textY += panel.tileSize; textY += panel.tileSize;
graphics2d.drawRect(textX, textY, 120, panel.tileSize/2); graphics2d.drawRect(textX, textY, 120, panel.tileSize/2); // 120/5 = 24
graphics2d.fillRect(textX, textY, 24 * panel.music.volumeScale, panel.tileSize/2);
// SFX VOLUME // SFX VOLUME
textY += panel.tileSize; textY += panel.tileSize;
graphics2d.drawRect(textX, textY, 120, panel.tileSize/2); graphics2d.drawRect(textX, textY, 120, panel.tileSize/2); // 120/5 = 24
graphics2d.fillRect(textX, textY, 24 * panel.se.volumeScale, panel.tileSize/2);
} }
public void optionsFSNotify(int frameX, int frameY) { public void optionsFSNotify(int frameX, int frameY) {
int textX = frameX + panel.tileSize; int textX = frameX + panel.tileSize;

View File

@@ -85,6 +85,29 @@ public class KeyHandler implements KeyListener {
panel.ui.commandNum++; panel.ui.commandNum++;
panel.playSE(9); panel.playSE(9);
break; break;
case KeyEvent.VK_LEFT:
if(panel.ui.optionState == UI.OptionState.OVERVIEW)
if(panel.ui.commandNum == 1 && panel.music.volumeScale > 0) {
panel.music.volumeScale--;
panel.music.checkVolume();
panel.playSE(9);
} else if(panel.ui.commandNum == 2 && panel.se.volumeScale > 0) {
panel.se.volumeScale--;
panel.playSE(9);
}
break;
case KeyEvent.VK_RIGHT:
if(panel.ui.optionState == UI.OptionState.OVERVIEW) {
if(panel.ui.commandNum == 1 && panel.music.volumeScale < 5) {
panel.music.volumeScale++;
panel.music.checkVolume();
panel.playSE(9);
} else if(panel.ui.commandNum == 2 && panel.se.volumeScale < 5) {
panel.se.volumeScale++;
panel.playSE(9);
}
}
break;
} }
// EXIT STATE // EXIT STATE

View File

@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.Boot;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.util.logging.Level; import java.util.logging.Level;
@@ -14,6 +15,9 @@ public class Sound {
Clip clip; Clip clip;
URL[] soundURL = new URL[30]; URL[] soundURL = new URL[30];
public Clip[] clips = new Clip[30]; public Clip[] clips = new Clip[30];
public FloatControl fc;
public int volumeScale = 3;
float volume;
public Sound() { public Sound() {
load(0, "assets/sounds/BlueBoyAdventure.wav"); load(0, "assets/sounds/BlueBoyAdventure.wav");
@@ -55,4 +59,16 @@ public class Sound {
clip.stop(); clip.stop();
} }
public void checkVolume() {
switch(volumeScale) {
case 0 -> volume = -80f;
case 1 -> volume = -20f;
case 2 -> volume = -12f;
case 3 -> volume = -5f;
case 4 -> volume = 1f;
case 5 -> volume = 6f;
}
fc.setValue(volume);
}
} }