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