Add dialogue sound

master
John 2022-06-20 18:21:47 -07:00
parent c29272e758
commit e330df0209
4 changed files with 19 additions and 11 deletions

View File

@ -1,4 +1,3 @@
$Villain
Oh you made it past the first real level Oh you made it past the first real level
Feeling acomplished aren't you... Feeling acomplished aren't you...

Binary file not shown.

View File

@ -19,6 +19,7 @@ public class DialogueMenu extends TextBox implements Serializable {
public int currentFrame = 0; public int currentFrame = 0;
public int frameCounter = 0; public int frameCounter = 0;
public boolean isNarrator; public boolean isNarrator;
public SoundWrapper currentSound;
public DialogueMenu(int y, int yHeight, Font font, BufferedImageWrapper portrait, boolean isNarrator) { public DialogueMenu(int y, int yHeight, Font font, BufferedImageWrapper portrait, boolean isNarrator) {
super(y, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*3, yHeight, 0, font, null, null); super(y, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*3, yHeight, 0, font, null, null);
@ -104,13 +105,15 @@ public class DialogueMenu extends TextBox implements Serializable {
if (frameCounter >= FREQUENCY) { if (frameCounter >= FREQUENCY) {
frameCounter -= FREQUENCY; frameCounter -= FREQUENCY;
currentFrame += 1; currentFrame += 1;
// play dialogue sound // play dialogue sound if it is not already playing
if (currentSound == null || !currentSound.sound.clip.isOpen()) {
try { try {
UtilityFunction.playSound("sound/pen.wav"); currentSound = UtilityFunction.playSound("sound/pen.wav");
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
}
// set font of string to be drawn // set font of string to be drawn
g.setFont(font); g.setFont(font);
drawCenteredTextBox(g, text, backgroundColor, textColor); drawCenteredTextBox(g, text, backgroundColor, textColor);
@ -119,6 +122,11 @@ public class DialogueMenu extends TextBox implements Serializable {
// if the text has been completely drawn (nothing left to animate), return true // if the text has been completely drawn (nothing left to animate), return true
if (currentFrame >= text.length()) { if (currentFrame >= text.length()) {
currentFrame = 0; currentFrame = 0;
// if the text is not being animated, there is no reason to play the sound either, so it is closed and then dereferenced
if (currentSound != null) {
currentSound.sound.close();
currentSound = null;
}
return true; return true;
} else { // otherwise, return false } else { // otherwise, return false
return false; return false;

View File

@ -44,14 +44,15 @@ public final class UtilityFunction {
} }
// start playing a sound that is located at filePath // start playing a sound that is located at filePath
public static void playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public static SoundWrapper playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
Sound sound = new Sound(filePath); SoundWrapper sound = new SoundWrapper(filePath);
sound.clip.addLineListener(e -> { sound.sound.clip.addLineListener(e -> {
// close clip after sound is done playing // close clip after sound is done playing
if (e.getType() == LineEvent.Type.STOP) { if (e.getType() == LineEvent.Type.STOP) {
sound.close(); sound.sound.close();
} }
}); });
sound.start(); sound.sound.start();
return sound;
} }
} }