home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / client / clientapplet / clientsounds.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.2 KB  |  151 lines

  1. /*
  2.  * @(#)ClientSounds.java
  3.  */
  4.  
  5. package games.Battle.client.ClientApplet;
  6.  
  7. import java.applet.*;
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import java.net.*;
  11. import java.util.Random;
  12.  
  13. import games.image.*;
  14. import games.Battle.shared.sys.*;
  15.  
  16. /**
  17.  * ClientSounds is responsible for loading and building all of the
  18.  * client sounds.
  19.  *
  20.  * @author Alex Nicolaou
  21.  * @author Jay Steele
  22.  */
  23.  
  24. public class ClientSounds {
  25.  
  26.     static Random rand = new Random();
  27.  
  28.     /**
  29.      * The applet used to obtain the URL.
  30.      */
  31.     public static Applet applet = null;
  32.  
  33.     /**
  34.      * The directory the sound files were located in.
  35.      */
  36.     private static String soundDir = "sounds";
  37.  
  38.     /**
  39.      * The URL to load the sounds from.
  40.      */
  41.     private static URL docBase = null;
  42.  
  43.     /**
  44.      * A flag to permit or deny sounds from playing.
  45.      */
  46.     private static boolean playOn = true;
  47.  
  48.     /**
  49.      * The gun sound.
  50.      */
  51.     public static AudioClip gun = null;
  52.  
  53.     /**
  54.      * The paratrooper sound.
  55.      */
  56.     public static AudioClip paratrooper = null;
  57.  
  58.     /**
  59.      * The click sound.
  60.      */
  61.     public static AudioClip click = null;
  62.  
  63.     /**
  64.      * The various fighting sounds.
  65.      */
  66.     public static AudioClip fight[] = new AudioClip[4];
  67.  
  68.     /**
  69.      * Retrieves an audio clip. This method look after ensuring the 
  70.      * image is loaded from the correct URL, and the pathname
  71.      * of the image.
  72.      * @param name the filename of the sound to retrieve
  73.      */
  74.     private static AudioClip getAudioClip(String name) {
  75.         AudioClip clip = applet.getAudioClip(docBase, soundDir+"/"+name);
  76.         return clip;
  77.     }
  78.  
  79.     /**
  80.      * Get the images. The method blocks until all of the images
  81.      * have been loaded.
  82.      * @param a the applet for the URL 
  83.      */
  84.     public static void loadAll(Applet a) {
  85.         applet = a;
  86.         try { 
  87.             docBase = a.getDocumentBase();
  88.             if (docBase == null)
  89.                 docBase = new URL("file:/C:/projects/Java/Battle/client/battle.html");
  90.         }
  91.         catch (Exception e) {
  92.             try {
  93.                 docBase = new URL("file:/C:/projects/Java/Battle/client/battle.html");
  94.             }
  95.             catch (Exception malformed) {
  96.                 System.out.println("malformed url");
  97.             }
  98.         }
  99.  
  100.         gun         = getAudioClip("gun.au");
  101.         paratrooper = getAudioClip("paratrooper.au");
  102.         click         = getAudioClip("click.au");
  103.         for (int i=0; i<fight.length; i++) {
  104.             fight[i]    = getAudioClip("fight"+i+".au");
  105.         }
  106.     }
  107.  
  108.     /**
  109.      * Play a click sound, if sound play is currently on.
  110.      */
  111.     public static synchronized void playClick() {
  112.         if (playOn && click != null) click.play();
  113.     }
  114.  
  115.     /**
  116.      * Play a gun sound, if sound play is currently on.
  117.      */
  118.     public static synchronized void playGun() {
  119.         if (playOn && gun != null) gun.play();
  120.     }
  121.  
  122.     /**
  123.      * Play a paratrooper sound, if sound play is currently on.
  124.      */
  125.     public static synchronized void playParatrooper() {
  126.         if (playOn && paratrooper != null) paratrooper.play();
  127.     }
  128.  
  129.     /*
  130.      * Randomly play one of the fighting sounds, if sound play
  131.      * is currently on.
  132.      */
  133.     public static synchronized void playRandomFight() {
  134.         if (playOn) {
  135.             int idx = Math.abs(rand.nextInt()) % fight.length;
  136.             if (fight[idx] != null)
  137.                 fight[idx].play();
  138.         }
  139.     }
  140.  
  141.     /**
  142.      * For those who don't like sounds, they can turn them off.
  143.      */
  144.     public static synchronized void disableSounds() { playOn = false; }
  145.  
  146.     /**
  147.      * For those who like sounds, they can turn them on.
  148.      */
  149.     public static synchronized void enableSounds() { playOn = true; }
  150. }
  151.