made music and sfx volume adjustable
This commit is contained in:
@@ -8,6 +8,7 @@ import de.miaurizius.jgame2d.tile.TileManager;
|
||||
import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
|
||||
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -50,7 +51,7 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
public UI ui = new UI(this);
|
||||
public EventHandler eventH = new EventHandler(this);
|
||||
public Sound se = new Sound();
|
||||
Sound music = new Sound();
|
||||
public Sound music = new Sound();
|
||||
Thread gameThread;
|
||||
|
||||
// ENTITY AND OBJECT
|
||||
@@ -197,7 +198,9 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
|
||||
// MUSIC
|
||||
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();
|
||||
c.setFramePosition(0);
|
||||
c.start();
|
||||
@@ -208,6 +211,8 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
}
|
||||
public void playSE(int i) {
|
||||
Clip c = se.clips[i];
|
||||
se.fc = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
|
||||
se.checkVolume();
|
||||
if(c.isRunning()) c.stop();
|
||||
c.setFramePosition(0);
|
||||
c.start();
|
||||
|
||||
@@ -381,11 +381,13 @@ public class UI {
|
||||
|
||||
// MUSIC VOLUME
|
||||
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
|
||||
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) {
|
||||
int textX = frameX + panel.tileSize;
|
||||
|
||||
@@ -85,6 +85,29 @@ public class KeyHandler implements KeyListener {
|
||||
panel.ui.commandNum++;
|
||||
panel.playSE(9);
|
||||
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
|
||||
|
||||
@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.Boot;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
@@ -14,6 +15,9 @@ public class Sound {
|
||||
Clip clip;
|
||||
URL[] soundURL = new URL[30];
|
||||
public Clip[] clips = new Clip[30];
|
||||
public FloatControl fc;
|
||||
public int volumeScale = 3;
|
||||
float volume;
|
||||
|
||||
public Sound() {
|
||||
load(0, "assets/sounds/BlueBoyAdventure.wav");
|
||||
@@ -55,4 +59,16 @@ public class Sound {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user