Merge remote-tracking branch 'origin/master'

master
Chara1236 2022-06-20 21:22:53 -04:00
commit ca00976a96
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
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 frameCounter = 0;
public boolean isNarrator;
public SoundWrapper currentSound;
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);
@ -104,11 +105,13 @@ public class DialogueMenu extends TextBox implements Serializable {
if (frameCounter >= FREQUENCY) {
frameCounter -= FREQUENCY;
currentFrame += 1;
// play dialogue sound
try {
UtilityFunction.playSound("sound/pen.wav");
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
throw new RuntimeException(e);
// play dialogue sound if it is not already playing
if (currentSound == null || !currentSound.sound.clip.isOpen()) {
try {
currentSound = UtilityFunction.playSound("sound/pen.wav");
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
throw new RuntimeException(e);
}
}
}
// set font of string to be drawn
@ -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 (currentFrame >= text.length()) {
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;
} else { // otherwise, return false
return false;

View File

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