/*
* Cero Project - Copyright 2006 The Cero Developement Team
* (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
package cero.ui.sound;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import cero.UnsupportedPluginException;
import cero.games.ChoiceMaker;
import cero.games.GameEvent;
import cero.ui.InterfaceModule;
import cero.ui.UserInterface;
/**
* This class allows us to manage the sound part of the game. It manage how
* sounds are played, that's to say if they add to be crushed when we want to
* play new sounds.
*
* @author LAGUERRE Michael
*/
public class SoundManager implements UserInterface {
/** the SoundQueue */
private static SoundQueue soundQueue = new SoundQueue();
/** AudioClip used to play sounds that can be cut */
private static AudioClip audio;
/** Boolean that stores the activated stus of the interface */
private static boolean activated = true;
/**
* Method to play a sound. The manager adds it either into an AudioClip or
* into the SoundQueue, depending if the sound can be cut during play or
* not.
*
* @param sound
* the Sound to play
*/
public static void playSound(Sound sound) {
if (activated) {
if (sound.canBeCut())
playAudioClip(sound);
else
playQueue(sound);
}
}
/**
* Method to stop the play of a sound. It can only be used while playing a
* sound that can be cut.
*/
public static void stopSound() {
if (audio != null)
audio.stop();
if (!soundQueue.isEmpty())
soundQueue.stop();
}
/**
* Method to add a sound that can't be cut into the SoundQueue.
*
* @param sound
* the Sound to play
*/
private static void playQueue(Sound sound) {
if (activated) {
if (audio != null)
audio.stop();
soundQueue.add(sound);
}
}
/**
* Method to play a sound that can be cut. It adds it into an AudioClip
*
* @param sound
* the Sound to play
*/
private static void playAudioClip(Sound sound) {
if (activated) {
while (!soundQueue.isEmpty()) {
// Do nothing, wait for the SoundQueue to stop playing
}
try {
if (audio != null) {
audio.stop();
}
if (sound instanceof WaveSound) {
audio = Applet.newAudioClip(new URL("file", "localhost",
sound.getFilename()));
audio.play();
} else if (sound instanceof TTSSound)
((TTSSound) sound).playUsingWav();
} catch (MalformedURLException e) {
System.out
.println("Mauvaise URL : Le fichier ne peut �tre lu");
}
}
}
/**
* Get the status of the interface, if it is activated or not.
*
* @return true if it has been activated, false otherwise.
*/
public boolean isActivated() {
return activated;
}
/**
* Activate or deactivate the interface.
*
* @param state
* a boolean showing the status of the interface
*/
public void setActivated(boolean state) {
activated = state;
}
/**
* Add a module to this interface.
*
* @param module
* the InterfaceModule to add.
* throws UnsupportedPluginException
*/
public void addInterfaceModule(InterfaceModule module)
throws UnsupportedPluginException {
throw new UnsupportedPluginException(
"Cannot add Module to the SoundManager");
}
/**
* Get this interface's modules.
*
* @return a Collection of InterfaceModule
*/
public Collection<InterfaceModule> getInterfaceModules() {
return null;
}
/**
* Get the ChoiceMake.
* throws UnsupportedOperationException
*
* @return a ChoiceMakerDialog
*/
public ChoiceMaker getChoiceMaker() throws UnsupportedOperationException {
return null;
}
/**
* Get the name of this User Interface.
*
* @return the name of this User Interface
*/
public String getUserInterfaceName() {
return "Cero Sound Interface";
}
/**
* Enable or disable the sound.
* It is enable by default.
*/
public static void switchSound(){
if(activated){
playSound(new WaveSound("wav/gui/deactivatedSound.wav",false));
activated=!activated;
}else{
activated=!activated;
playSound(new WaveSound("wav/gui/activatedSound.wav",false));
}
}
public void removeInterfaceModule(InterfaceModule module) {}
public void activateInterfaceModule(InterfaceModule module) {}
public void desactivateInterfaceModule(InterfaceModule module) {}
public void gameStart(GameEvent e) {}
public void gameEnd(GameEvent e) {}
}