home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 3.2 KB | 151 lines |
- /*
- * @(#)ClientSounds.java
- */
-
- package games.Battle.client.ClientApplet;
-
- import java.applet.*;
- import java.awt.*;
- import java.awt.image.*;
- import java.net.*;
- import java.util.Random;
-
- import games.image.*;
- import games.Battle.shared.sys.*;
-
- /**
- * ClientSounds is responsible for loading and building all of the
- * client sounds.
- *
- * @author Alex Nicolaou
- * @author Jay Steele
- */
-
- public class ClientSounds {
-
- static Random rand = new Random();
-
- /**
- * The applet used to obtain the URL.
- */
- public static Applet applet = null;
-
- /**
- * The directory the sound files were located in.
- */
- private static String soundDir = "sounds";
-
- /**
- * The URL to load the sounds from.
- */
- private static URL docBase = null;
-
- /**
- * A flag to permit or deny sounds from playing.
- */
- private static boolean playOn = true;
-
- /**
- * The gun sound.
- */
- public static AudioClip gun = null;
-
- /**
- * The paratrooper sound.
- */
- public static AudioClip paratrooper = null;
-
- /**
- * The click sound.
- */
- public static AudioClip click = null;
-
- /**
- * The various fighting sounds.
- */
- public static AudioClip fight[] = new AudioClip[4];
-
- /**
- * Retrieves an audio clip. This method look after ensuring the
- * image is loaded from the correct URL, and the pathname
- * of the image.
- * @param name the filename of the sound to retrieve
- */
- private static AudioClip getAudioClip(String name) {
- AudioClip clip = applet.getAudioClip(docBase, soundDir+"/"+name);
- return clip;
- }
-
- /**
- * Get the images. The method blocks until all of the images
- * have been loaded.
- * @param a the applet for the URL
- */
- public static void loadAll(Applet a) {
- applet = a;
- try {
- docBase = a.getDocumentBase();
- if (docBase == null)
- docBase = new URL("file:/C:/projects/Java/Battle/client/battle.html");
- }
- catch (Exception e) {
- try {
- docBase = new URL("file:/C:/projects/Java/Battle/client/battle.html");
- }
- catch (Exception malformed) {
- System.out.println("malformed url");
- }
- }
-
- gun = getAudioClip("gun.au");
- paratrooper = getAudioClip("paratrooper.au");
- click = getAudioClip("click.au");
- for (int i=0; i<fight.length; i++) {
- fight[i] = getAudioClip("fight"+i+".au");
- }
- }
-
- /**
- * Play a click sound, if sound play is currently on.
- */
- public static synchronized void playClick() {
- if (playOn && click != null) click.play();
- }
-
- /**
- * Play a gun sound, if sound play is currently on.
- */
- public static synchronized void playGun() {
- if (playOn && gun != null) gun.play();
- }
-
- /**
- * Play a paratrooper sound, if sound play is currently on.
- */
- public static synchronized void playParatrooper() {
- if (playOn && paratrooper != null) paratrooper.play();
- }
-
- /*
- * Randomly play one of the fighting sounds, if sound play
- * is currently on.
- */
- public static synchronized void playRandomFight() {
- if (playOn) {
- int idx = Math.abs(rand.nextInt()) % fight.length;
- if (fight[idx] != null)
- fight[idx].play();
- }
- }
-
- /**
- * For those who don't like sounds, they can turn them off.
- */
- public static synchronized void disableSounds() { playOn = false; }
-
- /**
- * For those who like sounds, they can turn them on.
- */
- public static synchronized void enableSounds() { playOn = true; }
- }
-