// Copyright 2001-2003, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/
/* Asteroids! */
package com.danger.rocks;

import danger.app.*;
import danger.audio.*;
import danger.system.*;
import danger.ui.*;
import danger.util.*;


/*
Track layout:

TRACK        CHANNEL        NAME                MAX VOLUME
tr1                ch1                bass        127
tr2                ch2                sweep       127
tr3                ch3                gtr                        100
tr4                ch4                ostinato1   100
tr5                ch5                ostinato2   127
tr6                ch6                ostinato3   64
tr7                ch7                ding        127
tr8                ch8                hihat       127
tr9                ch10        kick&snare        100
tr10        ch11        antagonist        127

loopstart at bar 1, loopend at bar 21 (first and last bars)
*/

// private class to do audio
class RockAudio implements Resources
{
	//~ Static fields/initializers ---------------------------------------------

	static final int LOOP_TEMPO  = 95;
	static final int LOOP_VOLUME = 45;
	static final int LOOP_FILTER = ToneFilter.NONE;

	// channels
	static final byte CHANNEL_BASS  = 0;
	static final byte CHANNEL_SWEEP = 1;
	static final byte CHANNEL_GTR   = 2;
	static final byte CHANNEL_OST1  = 3;
	static final byte CHANNEL_OST2  = 4;
	static final byte CHANNEL_OST3  = 5;
	static final byte CHANNEL_DING  = 6;
	static final byte CHANNEL_HATS  = 7;
	static final byte CHANNEL_PERC  = 9;
	static final byte CHANNEL_ANTAG = 10;

	// controls
	static final byte LEVEL1  = 0;
	static final byte LEVEL2  = 1;
	static final byte LEVEL3  = 2;
	static final byte LEVEL4  = 3;
	static final byte LEVEL5  = 4;
	static final byte LEVEL6  = 5;
	static final byte LEVEL7  = 6;
	static final byte LEVEL8  = 7;
	static final byte LEVEL9  = 8;
	static final byte LEVEL10 = 9;
	static final byte LEVEL11 = 10;
	static final byte LEVEL12 = 11;
	static final byte LEVEL13 = 12;
	static final byte LEVEL14 = 13;
	static final byte LEVEL15 = 14;
	static final byte ALL_FADE_DOWN = 20;
	static final byte ALL_OFF	    = 21;
	static final byte ALL_ON	    = 22;
	static final byte ANTAG		    = 23;
	static final byte START		    = 24;
	static final byte FADE		    = 25;
	static final byte MAX_LEVELS    = LEVEL15 + 1;
	static final byte MAX_SUB_LEVEL = 3;

	// variables
	static Sound     sThrustSound    = null;
	static Sound     sFireSound		 = null;
	static Sound[]   sBlowSound		 = { null, null };
	static int		 sWhichBlowSound = 0;
	static Sound     sNewLife		 = null;
	static Sequence  sLoop;
	static boolean   sAudioReady  = false;
	static boolean   sLoopStarted = false;
	static byte		 sAudioLevel    = LEVEL1;
	static int		 sSubAudioLevel = 0;
	static boolean   sSoundTrackEnabled = true;
	static Queue     sControlQueue = new Queue();
	static boolean[] sChannelMutes = new boolean[Sequence.MAX_MIDI_CHANNELS];

	// 0 is no change
	// 1 is up
	// -1 is down
	static int[]	   sChannelControl     = new int[Sequence.MAX_MIDI_CHANNELS];
	static int[]	   sChannelsToFadeUp   = new int[Sequence.MAX_MIDI_CHANNELS];
	static int[]	   sChannelsToFadeDown = new int[Sequence.MAX_MIDI_CHANNELS];
	static int[]	   sChannelLevels	   = new int[Sequence.MAX_MIDI_CHANNELS];
	static final int   FADE_RATE		   = 0x00022310;
	static int[]	   sMixChannels = new int[Sequence.MAX_MIDI_CHANNELS];
	static final int[] sChannelMaxVolume = 
		{
			Fixed.fromInt(127), // 1
			Fixed.fromInt(127), // 2
			Fixed.fromInt(100), // 3
			Fixed.fromInt(100), // 4
			Fixed.fromInt(127), // 5
			Fixed.fromInt(64), // 6
			Fixed.fromInt(127), // 7
			Fixed.fromInt(127), // 8
			0, // 9
			Fixed.fromInt(100), // 10
			Fixed.fromInt(127), // 11
			0, // 12
			0, // 13
			0, // 14
			0, // 15
			0 // 16
		};
	static final int[]   sLevelMix1  = { CHANNEL_BASS, -1 };
	static final int[]   sLevelMix2  = 
		{
			CHANNEL_BASS, CHANNEL_PERC, CHANNEL_HATS, -1
		};
	static final int[]   sLevelMix3  = 
		{
			CHANNEL_BASS, CHANNEL_PERC, CHANNEL_HATS, CHANNEL_SWEEP, CHANNEL_GTR,
			-1
		};
	static final int[]   sLevelMix4  = 
		{
			CHANNEL_BASS, CHANNEL_PERC, CHANNEL_SWEEP, CHANNEL_OST2, -1
		};
	static final int[]   sLevelMix5  = 
		{
			CHANNEL_BASS, CHANNEL_DING, CHANNEL_SWEEP, CHANNEL_OST1,
			CHANNEL_OST2, -1
		};
	static final int[]   sLevelMix6  = 
		{
			CHANNEL_BASS, CHANNEL_DING, CHANNEL_SWEEP, CHANNEL_OST1,
			CHANNEL_OST2, CHANNEL_OST3, -1
		};
	static final int[]   sLevelMix7  = 
		{
			CHANNEL_PERC, CHANNEL_DING, CHANNEL_HATS, CHANNEL_OST1, CHANNEL_OST2,
			CHANNEL_OST3, -1
		};
	static final int[]   sLevelMix8  = 
		{
			CHANNEL_PERC, CHANNEL_OST1, CHANNEL_OST2, CHANNEL_OST3, -1
		};
	static final int[]   sLevelMix9  = 
		{
			CHANNEL_PERC, CHANNEL_SWEEP, CHANNEL_DING, CHANNEL_HATS,
			CHANNEL_OST3, -1
		};
	static final int[]   sLevelMix10 = 
		{
			CHANNEL_PERC, CHANNEL_BASS, CHANNEL_DING, CHANNEL_HATS, CHANNEL_OST3,
			-1
		};
	static final int[]   sLevelMix11 = 
		{
			CHANNEL_BASS, CHANNEL_PERC, CHANNEL_OST1, CHANNEL_OST3, -1
		};
	static final int[]   sLevelMix12 = 
		{
			CHANNEL_BASS, CHANNEL_PERC, CHANNEL_GTR, CHANNEL_DING, CHANNEL_HATS,
			-1
		};
	static final int[]   sLevelMix13 = 
		{
			CHANNEL_BASS, CHANNEL_PERC, CHANNEL_SWEEP, CHANNEL_GTR, CHANNEL_DING,
			CHANNEL_HATS, -1
		};
	static final int[]   sLevelMix14 = 
		{
			CHANNEL_BASS, CHANNEL_SWEEP, CHANNEL_GTR, CHANNEL_HATS, -1
		};
	static final int[]   sLevelMix15 = 
		{
			CHANNEL_BASS, CHANNEL_SWEEP, CHANNEL_GTR - 1
		};
	static final int[][] sLevels = 
		{
			sLevelMix1, sLevelMix2, sLevelMix3, sLevelMix4, sLevelMix5,
			sLevelMix6, sLevelMix7, sLevelMix8, sLevelMix9, sLevelMix10,
			sLevelMix11, sLevelMix12, sLevelMix13, sLevelMix14, sLevelMix15
		};
	static final int[] sAllChannels = 
		{
			CHANNEL_BASS, CHANNEL_SWEEP, CHANNEL_GTR, CHANNEL_OST1, CHANNEL_OST2,
			CHANNEL_OST3, CHANNEL_DING, CHANNEL_HATS, CHANNEL_PERC, -1
		};

	static final int[] sMetaIDs =
		{
			Meta.SCROLL_WHEEL_UP, Meta.SCROLL_WHEEL_DOWN, Meta.FEEDBACK_WHEEL_BUTTON, 
			Meta.FEEDBACK_KEY_ONCE, Meta.FEEDBACK_KEY_REPEAT, Meta.FEEDBACK_SEND_BUTTON, 
			Meta.FEEDBACK_END_BUTTON, Meta.FEEDBACK_CANCEL_BUTTON, Meta.FEEDBACK_PAGE_UP,
			Meta.FEEDBACK_PAGE_DOWN, Meta.SCROLL_LIMIT, Meta.FEEDBACK_JUMP_BUTTON,
			Meta.FEEDBACK_DONE_BUTTON, Meta.FEEDBACK_CANCEL_BUTTON, Meta.FEEDBACK_MENU_BUTTON,
			Meta.FEEDBACK_LEFT_SHOULDER_BUTTON, Meta.FEEDBACK_RIGHT_SHOULDER_BUTTON
		};
	//~ Methods ----------------------------------------------------------------

	/**
	 * DOCUMENT ME!
	 */
	public static void setup()
	{
		if (sAudioReady == false)
		{
			/* turn off some automatic key sounds
			*/
			for (int count = 0; count < sMetaIDs.length; count++)
			{
				Meta.muteID(sMetaIDs[count]);
			}

			try
			{
				sLoop = AudioManager.createSequence(false);
			}
			catch (AudioException e)
			{
				System.out.println("Failed to load loop");

				if (sLoop != null)
				{
					sLoop.destroy();
					sLoop = null;
				}
			}

			try
			{
				sThrustSound = AudioManager.createSound(false);
			}
			catch (AudioException e)
			{
				sThrustSound = null;
			}

			try
			{
				sFireSound = AudioManager.createSound(false);
			}
			catch (AudioException e)
			{
				sFireSound = null;
			}

			sWhichBlowSound = 0;

			for (int count = 0; count < 2; count++)
			{
				try
				{
					sBlowSound[count] = AudioManager.createSound(false);
				}
				catch (AudioException e)
				{
					sBlowSound[count] = null;
				}
			}

			try
			{
				sNewLife = AudioManager.createSound(false);
			}
			catch (AudioException e)
			{
				sNewLife = null;
			}
		}
	}

	private static void load()
	{
		if (sAudioReady == false)
		{
			if (sLoop != null)
			{
				if (sLoop.loadFromResource(ID_soundtrack) == false)
				{
					sLoop.destroy();
					sLoop = null;
					System.out.println("Failed to load loop");
				}
			}

			if (sThrustSound != null)
			{
				if (sThrustSound.loadFromResource(ID_Thrust) == false)
				{
					sThrustSound.destroy();
					sThrustSound = null;
				}
			}

			if (sFireSound != null)
			{
				if (sFireSound.loadFromResource(ID_Torp) == false)
				{
					sFireSound.destroy();
					sFireSound = null;
				}
			}

			if ((sBlowSound[0] != null) && (sBlowSound[1] != null))
			{
				sWhichBlowSound = 0;

				for (int count = 0; count < 2; count++)
				{
					if (sBlowSound[count].loadFromResource(ID_Blow) == false)
					{
						sBlowSound[count].destroy();
						sBlowSound[count] = null;
					}
				}
			}

			if (sNewLife != null)
			{
				if (sNewLife.loadFromResource(ID_newlife) == false)
				{
					sNewLife.destroy();
					sNewLife = null;
				}
				else
				{
					sNewLife.setVolume(200);
				}
			}

			setChannelControl(ALL_OFF);
			setChannelControl(ALL_FADE_DOWN);
			sAudioReady = true;
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param inKey DOCUMENT ME!
	 */
	public static void handleKeys(char inKey)
	{
		byte channel = -1;

		switch (inKey)
		{
			//	case 'n':
			//		GoNextLevel();
			//		break;
			case '1':
				channel = CHANNEL_BASS;

				break;

			case '2':
				channel = CHANNEL_SWEEP;

				break;

			case '3':
				channel = CHANNEL_GTR;

				break;

			case '4':
				channel = CHANNEL_OST1;

				break;

			case '5':
				channel = CHANNEL_OST2;

				break;

			case '6':
				channel = CHANNEL_OST3;

				break;

			case '7':
				channel = CHANNEL_DING;

				break;

			case '8':
				channel = CHANNEL_HATS;

				break;

			case '9':
				channel = CHANNEL_PERC;

				break;

			case '0':
				channel = CHANNEL_ANTAG;

				break;
		}

		if (channel != -1)
		{
			if (isChannelMuted(channel) == true)
			{
				setChannelVolume(channel, -1); // set to max volume
			}
			else
			{
				setChannelVolume(channel, 0); // off
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param on DOCUMENT ME!
	 */
	public static void Antagonist(boolean on)
	{
		if (sLoop != null)
		{
			if (on)
			{
				setChannelVolume(CHANNEL_ANTAG, -1); // set to max volume
			}
			else
			{
				setChannelVolume(CHANNEL_ANTAG, 0); // set to min volume
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void resetLevel()
	{
		sSubAudioLevel     = 0;
		sAudioLevel		   = LEVEL1;
		Antagonist(false);
		setChannelControl(ALL_FADE_DOWN);
		restartLevel();

		//	System.out.println("rocks::ResetLevel audio");
	}

	private static void restartLevel()
	{
		//	System.out.println("audio level " + sAudioLevel);
		if (sSoundTrackEnabled)
		{
			setChannelControl(sAudioLevel);
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param increaseAmount DOCUMENT ME!
	 */
	public static void goNextLevel(int increaseAmount)
	{
		sSubAudioLevel = sSubAudioLevel + increaseAmount;

		//	System.out.println("rocks::GoNextLevel() " + sSubAudioLevel);
		if (sSubAudioLevel > MAX_SUB_LEVEL)
		{
			sAudioLevel++;
			sSubAudioLevel = 0;

			if (sAudioLevel > MAX_LEVELS)
			{
				sAudioLevel = LEVEL1;
			}

			restartLevel();
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void goNextLevel()
	{
		goNextLevel(1);
	}

	private static void startChannelFadeUp(int[] channels)
	{
		int count = 0;

		while (channels[count] != -1)
		{
			startChannelFadeUp(channels[count]);
			count++;
		}
	}

	private static void startChannelFadeUp(int channel)
	{
		if (sSoundTrackEnabled == true)
		{
			//			sChannelsToFadeUp[channel] = 0;
			sChannelsToFadeUp[channel]     = Fixed.fromInt(sChannelLevels[channel]);
			sChannelControl[channel]	   = 1; // up

			//			SetChannelVolume(channel, Fixed.ToInt(sChannelsToFadeUp[channel]));
		}
	}

	private static void startChannelFadeDown(int[] channels)
	{
		int count = 0;

		while (channels[count] != -1)
		{
			startChannelFadeDown(channels[count]);
			count++;
		}
	}

	private static void startChannelFadeDown(int channel)
	{
		//		sChannelsToFadeDown[channel] = sChannelMaxVolume[channel];
		sChannelsToFadeDown[channel]     = Fixed.fromInt(sChannelLevels[channel]);
		sChannelControl[channel]		 = -1; // down

		//		SetChannelVolume(channel, Fixed.ToInt(sChannelsToFadeDown[channel]));
	}

	private static void stopChannelFades()
	{
		for (int count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
		{
			sChannelControl[count] = 0;
		}
	}

	private static boolean isChannelMuted(int channel)
	{
		return sChannelMutes[channel];
	}

	private static void resetChannelMutes()
	{
		if (sLoop != null)
		{
			for (int count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
			{
				if (sChannelMutes[count])
				{
					sLoop.muteChannel((byte) count);
				}
				else
				{
					sLoop.unmuteChannel((byte) count);
				}
			}
		}
	}

	private static void setChannelMute(int[] channels, boolean mute)
	{
		int count = 0;

		while (channels[count] != -1)
		{
			setChannelMute(channels[count], mute);
			count++;
		}
	}

	private static void setChannelMute(int channel, boolean mute)
	{
		if (sChannelMutes[channel] != mute)
		{
			if (sLoop != null)
			{
				if (mute)
				{
					sLoop.muteChannel((byte) channel);
				}
				else
				{
					sLoop.unmuteChannel((byte) channel);
				}
			}
		}
		sChannelMutes[channel] = mute;
	}

	private static void resetChannelVolumes()
	{
		if (sLoop != null)
		{
			for (int count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
			{
				sLoop.controlChange((byte) count, (byte) 7,
					(byte) sChannelLevels[count]);
			}
		}
	}

	private static void setChannelVolume(int channel, int volume)
	{
		if (volume == -1)
		{
			volume = Fixed.toInt(sChannelMaxVolume[channel]);
		}

		sChannelLevels[channel] = volume;

		if (sLoop != null)
		{
			sLoop.controlChange((byte) channel, (byte) 7, (byte) volume);

			setChannelMute(channel, (volume < 1));
		}
	}

	private static void setChannelsVolume(int volume)
	{
		if (sLoop != null)
		{
			for (int count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
			{
				setChannelVolume(count, volume);
			}
		}
	}

	// set mix level and fade all channels not in the mix down
	private static void setLevelMix(int[] channels, boolean now)
	{
		int count;

		for (count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
		{
			sMixChannels[count] = count;
		}

		count = 0;

		while (channels[count] != -1)
		{
			int chan = channels[count];
			sMixChannels[chan] = -1;

			if (now == false)
			{
				//		System.out.println("rocks::chan " + chan);
				startChannelFadeUp(chan);
			}
			else
			{
				setChannelVolume(chan, -1);
			}

			setChannelMute(chan, false);
			count++;
		}

		for (count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
		{
			if (sMixChannels[count] != -1)
			{
				if (now == false)
				{
					startChannelFadeDown(count);
				}
				else
				{
					setChannelVolume(count, 0);
				}
			}
		}
	}

	private static void setChannelControl(byte control)
	{
		setChannelControl(control, true);
	}

	private static void setChannelControl(byte control, boolean queue)
	{
		if (queue)
		{
			sControlQueue.append(new Byte(control));
		}
		else
		{
			//	System.out.println("rocks::SetChannelControl " + control);
			if (sLoop != null)
			{
				switch (control)
				{
					case ALL_FADE_DOWN:
						startChannelFadeDown(sAllChannels);

						break;

					case START:
						sLoop.start(LOOP_FILTER, LOOP_VOLUME);
						resetChannelVolumes();
						resetChannelMutes();
						setChannelControl(FADE);

						break;

					case FADE:
						setLevelMix(sLevels[sAudioLevel], false);

						break;

					case ALL_ON:
						setLevelMix(sLevels[sAudioLevel], true);

						break;

					case ALL_OFF:
						setChannelsVolume(0);
						stopChannelFades();

						break;

					case LEVEL1:
					case LEVEL2:
					case LEVEL3:
					case LEVEL4:
					case LEVEL5:
					case LEVEL6:
					case LEVEL7:
					case LEVEL8:
					case LEVEL9:
					case LEVEL10:
					case LEVEL11:
					case LEVEL12:
					case LEVEL13:
					case LEVEL14:
					case LEVEL15:
						setLevelMix(sLevels[control], false);

						break;
				}
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void toggleSoundtrack()
	{
		sSoundTrackEnabled = (sSoundTrackEnabled) ? false : true;

		if (sSoundTrackEnabled)
		{ // restart sound track
			setChannelControl(sAudioLevel);
		}
		else
		{
			sControlQueue     = null; // throw away any elements in the queue
			sControlQueue     = new Queue();
			setChannelControl(ALL_OFF);
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public static boolean isSoundtrackEnabled()
	{
		return sSoundTrackEnabled;
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void pause()
	{
		if (sLoop != null)
		{
			if (sLoop.isPaused() == false)
			{
				sLoop.pause();
			}
		}

		if (sThrustSound != null)
		{
			sThrustSound.stop();
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void resume()
	{
		if (sLoop != null)
		{
			if (sLoop.isPaused())
			{
				sLoop.resume();
			}

			if (sSoundTrackEnabled)
			{ // restart sound track
				setChannelControl(ALL_ON);
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void cleanup()
	{
		if (sNewLife != null)
		{
			sNewLife.destroy();
			sNewLife = null;
		}

		if (sFireSound != null)
		{
			sFireSound.destroy();
			sFireSound = null;
		}

		for (int count = 0; count < 2; count++)
		{
			if (sBlowSound[count] != null)
			{
				sBlowSound[count].destroy();
				sBlowSound[count] = null;
			}
		}

		if (sThrustSound != null)
		{
			sThrustSound.destroy();
			sThrustSound = null;
		}

		if (sLoop != null)
		{
			sLoop.getChannelMuteStatus(sChannelMutes);
			sLoop.destroy();
			sLoop = null;
		}

		/* Turn back on default sounds
		*/
		for (int count = 0; count < sMetaIDs.length; count++)
		{
			Meta.unmuteID(sMetaIDs[count]);
		}
		sAudioReady		 = false;
		sLoopStarted     = false;
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void idleAudio()
	{
		int count;

		if (sAudioReady == false)
		{
			load();
		}

		if (sAudioReady && (sLoop != null))
		{
			if (sLoopStarted == false)
			{
				sLoopStarted = true;
				setChannelControl(START);
			}

			// tweak tempo a bit based upon the number of rocks floating about
			// how many rocks are on the screen?
			count = rocks.mWindow.mRocksList.size();

			if (count < rocks.mWindow.INITIAL_ROCKS)
			{
				count = rocks.mWindow.INITIAL_ROCKS;
			}

			int tempo = LOOP_TEMPO + ((count * 5) / 2);
			sLoop.setMasterTempo(tempo);

			if (sControlQueue.isEmpty() == false)
			{
				Byte meSue   = (Byte) sControlQueue.peek();
				byte control = meSue.byteValue();
				sControlQueue.pop();
				setChannelControl(control, false);
			}

			// see if we need to fade any channnels
			for (count = 0; count < Sequence.MAX_MIDI_CHANNELS; count++)
			{
				if (sChannelControl[count] < 0)
				{
					// ...down?
					//	sChannelsToFadeDown[count] = Fixed.Subtract(sChannelsToFadeDown[count], FADE_RATE);
					sChannelsToFadeDown[count] -= FADE_RATE;

					if (sChannelsToFadeDown[count] < 0)
					{
						sChannelsToFadeDown[count]     = 0;
						sChannelControl[count]		   = 0;
					}

					setChannelVolume(count,
						Fixed.toInt(sChannelsToFadeDown[count]));

					//System.out.println("rocks:fade down chan " + count + " vol " + sChannelsToFadeDown[count]);
				}

				if (sChannelControl[count] > 0)
				{
					// ...up?
					//	sChannelsToFadeUp[count] = Fixed.Add(sChannelsToFadeUp[count], FADE_RATE);
					sChannelsToFadeUp[count] += FADE_RATE;

					if (sChannelsToFadeUp[count] > sChannelMaxVolume[count])
					{
						sChannelsToFadeUp[count]     = sChannelMaxVolume[count];
						sChannelControl[count]		 = 0;
					}

					setChannelVolume(count,
						Fixed.toInt(sChannelsToFadeUp[count]));

					//System.out.println("rocks:fade up chan " + count + " vol " + sChannelsToFadeUp[count]);
				}
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void startThrust()
	{
		if (sThrustSound != null)
		{
			if (sThrustSound.isDone() == true)
			{
				//		sThrustSound.setPriority(50);	// priority doesn't work yet
				sThrustSound.start();
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void stopThrust()
	{
		if (sThrustSound != null)
		{
			sThrustSound.stop();
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void fireMissle()
	{
		if (sFireSound != null)
		{
			//	sFireSound.setPriority(50);
			sFireSound.start();
		}
	}

	static void startBlow(int pri)
	{
		if (sBlowSound[sWhichBlowSound] != null)
		{
			//	sBlowSound[sWhichBlowSound].setPriority(pri);
			sBlowSound[sWhichBlowSound].start();
			sWhichBlowSound = (sWhichBlowSound == 0) ? 1 : 0;
		}
	}

	static void stopBlow()
	{
		for (int count = 0; count < 2; count++)
		{
			if (sBlowSound[count] != null)
			{
				sBlowSound[count].stop();
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void extraLife()
	{
		// stop extra sounds
		stopBlow();

		if (sFireSound != null)
		{
			sFireSound.stop();
		}

		if (sNewLife != null)
		{
			//	sNewLife.setPriority(100);
			sNewLife.start();
		}
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void hitHero()
	{
		startBlow(75);
		AudioManager.vibrate(90);
	}

	/**
	 * DOCUMENT ME!
	 */
	public static void hitRock()
	{
		startBlow(25);
		AudioManager.vibrate(70);
	}
}

// EOF
