GAM456-SpaceInvaders / SpaceInvaders / 6_Sound / SoundManager.cs
SoundManager.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class SoundManager
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private static SoundManager pInstance = null;

        //LTN: own by sound manager
        public IrrKlang.ISoundEngine pSoundEngine = new IrrKlang.ISoundEngine();

        private IrrKlang.ISoundSource invader1;
        private IrrKlang.ISoundSource invader2;
        private IrrKlang.ISoundSource invader3;
        private IrrKlang.ISoundSource invader4;
        private IrrKlang.ISoundSource invaderDeath;

        private IrrKlang.ISoundSource ufo_highpitch;
        private IrrKlang.ISoundSource ufo_lowpitch;

        private IrrKlang.ISoundSource explosion;
        private IrrKlang.ISoundSource shoot;


        //-------------------------------------------------------------------
        //  CLASS METHODS
        //-------------------------------------------------------------------

        private SoundManager()
        {
            this.pSoundEngine.SoundVolume = 0.1f;   //default

            //preload sound effects?
            invader1 = pSoundEngine.AddSoundSourceFromFile("fastinvader1.wav");
            invader2 = pSoundEngine.AddSoundSourceFromFile("fastinvader2.wav");
            invader3 = pSoundEngine.AddSoundSourceFromFile("fastinvader3.wav");
            invader4 = pSoundEngine.AddSoundSourceFromFile("fastinvader4.wav");
            invaderDeath = pSoundEngine.AddSoundSourceFromFile("invaderkilled.wav");

            ufo_highpitch = pSoundEngine.AddSoundSourceFromFile("ufo_highpitch.wav");
            ufo_lowpitch = pSoundEngine.AddSoundSourceFromFile("ufo_lowpitch.wav");

            explosion = pSoundEngine.AddSoundSourceFromFile("explosion.wav");
            shoot = pSoundEngine.AddSoundSourceFromFile("shoot.wav");
        }

        public static void Create()
        {
            //LTN: own by sound manager
            Debug.Assert(pInstance == null);
            pInstance = new SoundManager();
        }

        public static void Destroy()
        {
            SoundManager pManager = SoundManager.GetInstance();
            Debug.Assert(pManager != null);

            pManager.pSoundEngine = null;
            pManager = null;
        }


        public static void Play(String sound)
        {
            SoundManager pManager = SoundManager.GetInstance();
            Debug.Assert(pManager != null);

            pManager.pSoundEngine.Play2D(sound);
        }

        public static bool IsPlaying(String sound)
        {
            SoundManager pManager = SoundManager.GetInstance();
            Debug.Assert(pManager != null);

            return pManager.pSoundEngine.IsCurrentlyPlaying(sound);
        }

        private static SoundManager GetInstance()
        {
            Debug.Assert(pInstance != null);
            return pInstance;
        }

        public static void Update()
        {
            SoundManager pManager = SoundManager.GetInstance();
            Debug.Assert(pManager != null);
            
            pManager.pSoundEngine.Update();
        }
    }
}