optimized sound management (fixed #4)
This commit is contained in:
@@ -6,6 +6,7 @@ import de.miaurizius.jgame2d.entity.Entity;
|
||||
import de.miaurizius.jgame2d.entity.Player;
|
||||
import de.miaurizius.jgame2d.tile.TileManager;
|
||||
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
@@ -158,16 +159,20 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
|
||||
// MUSIC
|
||||
public void playMusic(int i) {
|
||||
music.setFile(i);
|
||||
music.play();
|
||||
music.loop();
|
||||
Clip c = se.clips[i];
|
||||
if(c.isRunning()) c.stop();
|
||||
c.setFramePosition(0);
|
||||
c.start();
|
||||
c.loop(Clip.LOOP_CONTINUOUSLY);
|
||||
}
|
||||
public void stopMusic() {
|
||||
music.stop();
|
||||
}
|
||||
public void playSE(int i) {
|
||||
se.setFile(i);
|
||||
se.play();
|
||||
Clip c = se.clips[i];
|
||||
if(c.isRunning()) c.stop();
|
||||
c.setFramePosition(0);
|
||||
c.start();
|
||||
}
|
||||
|
||||
// SETTING THINGS UP
|
||||
|
||||
@@ -74,6 +74,7 @@ public class EventHandler {
|
||||
|
||||
public void damagePit(GameState gameState) {
|
||||
panel.gameState = gameState;
|
||||
panel.playSE(6);
|
||||
panel.ui.currentDialogue = "You have fallen into a pit!";
|
||||
panel.player.life -= 1;
|
||||
canTouchEvent = false;
|
||||
@@ -82,6 +83,8 @@ public class EventHandler {
|
||||
public void healingPool(GameState gameState) {
|
||||
if(!panel.keyH.spacePressed) return;
|
||||
panel.gameState = gameState;
|
||||
panel.player.attackCancel = true;
|
||||
panel.playSE(2);
|
||||
panel.ui.currentDialogue = "You drank the holy water.\nYour life has been recovered!";
|
||||
panel.player.life = panel.player.maxLife;
|
||||
canTouchEvent = false;
|
||||
|
||||
@@ -14,22 +14,20 @@ public class Sound {
|
||||
|
||||
Clip clip;
|
||||
URL[] soundURL = new URL[30];
|
||||
public Clip[] clips = new Clip[30];
|
||||
|
||||
public Sound() {
|
||||
try {
|
||||
soundURL[0] = new File("assets/sounds/BlueBoyAdventure.wav").toURI().toURL();
|
||||
soundURL[1] = new File("assets/sounds/coin.wav").toURI().toURL();
|
||||
soundURL[2] = new File("assets/sounds/powerup.wav").toURI().toURL();
|
||||
soundURL[3] = new File("assets/sounds/unlock.wav").toURI().toURL();
|
||||
soundURL[4] = new File("assets/sounds/fanfare.wav").toURI().toURL();
|
||||
soundURL[5] = new File("assets/sounds/hitmonster.wav").toURI().toURL();
|
||||
soundURL[6] = new File("assets/sounds/receivedamage.wav").toURI().toURL();
|
||||
soundURL[7] = new File("assets/sounds/blocked.wav").toURI().toURL();
|
||||
} catch(MalformedURLException e) {
|
||||
Boot.logger.log(Level.SEVERE, e.getMessage());
|
||||
}
|
||||
load(0, "assets/sounds/BlueBoyAdventure.wav");
|
||||
load(1, "assets/sounds/coin.wav");
|
||||
load(2, "assets/sounds/powerup.wav");
|
||||
load(3, "assets/sounds/unlock.wav");
|
||||
load(4, "assets/sounds/fanfare.wav");
|
||||
load(5, "assets/sounds/hitmonster.wav");
|
||||
load(6, "assets/sounds/receivedamage.wav");
|
||||
load(7, "assets/sounds/blocked.wav");
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setFile(int i) {
|
||||
try {
|
||||
AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL[i]);
|
||||
@@ -40,6 +38,16 @@ public class Sound {
|
||||
}
|
||||
}
|
||||
|
||||
private void load(int index, String path) {
|
||||
try {
|
||||
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(path));
|
||||
clips[index] = AudioSystem.getClip();
|
||||
clips[index].open(ais); // Wird nur einmal gemacht!
|
||||
} catch (Exception e) {
|
||||
Boot.logger.log(Level.SEVERE, "Could not load Sound File: " + soundURL[index], e);
|
||||
}
|
||||
}
|
||||
|
||||
public void play() {
|
||||
clip.start();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user