home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Aventura / Never_End.swf / scripts / game / SoundManager.as < prev    next >
Encoding:
Text File  |  2008-09-23  |  9.8 KB  |  334 lines

  1. package game
  2. {
  3.    import flash.events.Event;
  4.    import flash.media.Sound;
  5.    import flash.media.SoundChannel;
  6.    import flash.media.SoundTransform;
  7.    import flash.utils.clearTimeout;
  8.    import flash.utils.getTimer;
  9.    import flash.utils.setTimeout;
  10.    
  11.    public class SoundManager
  12.    {
  13.       
  14.       public static const TYPE_BG:String = "c";
  15.       
  16.       public static var _instance:SoundManager;
  17.       
  18.       public static const TYPE_MAN:String = "a";
  19.       
  20.       public static const TYPE_BTN:String = "b";
  21.       
  22.       public static const TYPE_ITEM:String = "d";
  23.        
  24.       
  25.       private var voiceTimer:int;
  26.       
  27.       private var bgStartTime:Number = -1;
  28.       
  29.       private var maxSound:Number = 0.75;
  30.       
  31.       private var nBgVolume:Number;
  32.       
  33.       public var soundOn:Boolean;
  34.       
  35.       private var nextBg:SoundChannel;
  36.       
  37.       private var currentSign:String;
  38.       
  39.       private var pBgVolume:Number;
  40.       
  41.       private var currentBgVolume:Number = -1;
  42.       
  43.       private var nextPlay:Boolean = false;
  44.       
  45.       private const MAX_VOLUME:Number = 0.75;
  46.       
  47.       public var playTable:Object;
  48.       
  49.       public var soundTable:Object;
  50.       
  51.       public function SoundManager()
  52.       {
  53.          maxSound = 0.75;
  54.          bgStartTime = -1;
  55.          currentBgVolume = -1;
  56.          nextPlay = false;
  57.          super();
  58.          soundTable = new Object();
  59.          playTable = new Object();
  60.          soundOn = Cookie.getInstance().getValue("soundOn") == null ? true : Cookie.getInstance().getValue("soundOn");
  61.       }
  62.       
  63.       public static function getInstance() : SoundManager
  64.       {
  65.          if(_instance == null)
  66.          {
  67.             _instance = new SoundManager();
  68.          }
  69.          return _instance;
  70.       }
  71.       
  72.       private function voiceDown() : void
  73.       {
  74.          pBgVolume -= 0.025;
  75.          nBgVolume += 0.025;
  76.          if(pBgVolume <= 0)
  77.          {
  78.             nextBg.soundTransform = new SoundTransform(maxSound);
  79.             playTable[TYPE_BG].soundTransform = new SoundTransform(0);
  80.             playTable[TYPE_BG].removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
  81.             playTable[TYPE_BG] = nextBg;
  82.             playTable[TYPE_BG].addEventListener(Event.SOUND_COMPLETE,onBgComplete,false,0,true);
  83.             nextBg = null;
  84.             if(currentSign == "geomBg")
  85.             {
  86.                voiceTimer = setTimeout(bgVoiceDown,70000,"mainBg");
  87.             }
  88.             return;
  89.          }
  90.          nextBg.soundTransform = new SoundTransform(nBgVolume);
  91.          playTable[TYPE_BG].soundTransform = new SoundTransform(pBgVolume);
  92.          voiceTimer = setTimeout(voiceDown,100);
  93.       }
  94.       
  95.       private function onManSdComplete(param1:Event) : void
  96.       {
  97.          playSound("manRun",TYPE_MAN,true);
  98.       }
  99.       
  100.       public function stopManSound() : void
  101.       {
  102.          if(playTable[TYPE_MAN])
  103.          {
  104.             playTable[TYPE_MAN].stop();
  105.             try
  106.             {
  107.                playTable[TYPE_MAN].removeEventListener(Event.SOUND_COMPLETE,onManSdComplete);
  108.             }
  109.             catch(e:Error)
  110.             {
  111.             }
  112.             playTable[TYPE_MAN] = null;
  113.          }
  114.       }
  115.       
  116.       private function bgVolumeDown() : void
  117.       {
  118.          try
  119.          {
  120.             pBgVolume -= 0.025;
  121.             if(pBgVolume <= 0)
  122.             {
  123.                clearAllSounds();
  124.                return;
  125.             }
  126.             currentBgVolume = pBgVolume;
  127.             playTable[TYPE_BG].soundTransform = new SoundTransform(pBgVolume);
  128.             voiceTimer = setTimeout(bgVolumeDown,100);
  129.          }
  130.          catch(e:Error)
  131.          {
  132.             clearAllSounds();
  133.          }
  134.       }
  135.       
  136.       public function loadSound(param1:String, param2:*) : void
  137.       {
  138.          var _loc3_:Sound = null;
  139.          _loc3_ = new param2();
  140.          soundTable[param1] = _loc3_;
  141.       }
  142.       
  143.       public function loadSounds() : void
  144.       {
  145.          loadSound("mainBg",MainBg);
  146.          loadSound("geomBg",GeometryBg);
  147.          loadSound("getItem",GetItem);
  148.          loadSound("pistonFall",PistonFall);
  149.          loadSound("doorOpen",DoorOpen);
  150.          loadSound("manClimb",ManClimb);
  151.          loadSound("manRun",ManRun);
  152.          loadSound("manFall",ManFall);
  153.          loadSound("manDie",ManDie);
  154.          loadSound("manJump",ManJump);
  155.          loadSound("mapRotate",MapRotate);
  156.          loadSound("splash",Splash);
  157.          loadSound("scaleMax",ScaleMax);
  158.          loadSound("scaleMin",ScaleMin);
  159.          loadSound("btnDown",MenuDown);
  160.       }
  161.       
  162.       public function turnSound(param1:Boolean) : void
  163.       {
  164.          soundOn = param1;
  165.          Cookie.getInstance().saveValue(param1,"soundOn");
  166.          if(param1)
  167.          {
  168.             maxSound = MAX_VOLUME;
  169.          }
  170.          else
  171.          {
  172.             maxSound = 0;
  173.          }
  174.          if(playTable[TYPE_MAN])
  175.          {
  176.             playTable[TYPE_MAN].soundTransform = new SoundTransform(maxSound);
  177.          }
  178.          if(playTable[TYPE_BTN])
  179.          {
  180.             playTable[TYPE_BTN].soundTransform = new SoundTransform(maxSound);
  181.          }
  182.          if(playTable[TYPE_BG])
  183.          {
  184.             playTable[TYPE_BG].soundTransform = new SoundTransform(maxSound);
  185.          }
  186.          if(playTable[TYPE_ITEM])
  187.          {
  188.             playTable[TYPE_ITEM].soundTransform = new SoundTransform(maxSound);
  189.          }
  190.       }
  191.       
  192.       public function turnOffBg() : void
  193.       {
  194.          try
  195.          {
  196.             clearTimeout(voiceTimer);
  197.          }
  198.          catch(e:Error)
  199.          {
  200.          }
  201.          pBgVolume = currentBgVolume < 0 ? maxSound : currentBgVolume;
  202.          voiceTimer = setTimeout(bgVolumeDown,100);
  203.       }
  204.       
  205.       private function onBgComplete(param1:Event) : void
  206.       {
  207.          if(bgStartTime < 0)
  208.          {
  209.             bgStartTime = getTimer();
  210.          }
  211.          param1.currentTarget.removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
  212.          playTable[TYPE_BG] = null;
  213.          if(Math.random() > 0.8 && getTimer() - bgStartTime > 80000)
  214.          {
  215.             nextPlay = true;
  216.             playSound("mainBg",TYPE_BG);
  217.             bgStartTime = getTimer() + 60000;
  218.          }
  219.          else
  220.          {
  221.             playSound("mainBg",TYPE_BG);
  222.          }
  223.       }
  224.       
  225.       private function bgVoiceDown(param1:String) : void
  226.       {
  227.          nextPlay = false;
  228.          currentSign = param1;
  229.          pBgVolume = maxSound;
  230.          nBgVolume = 0;
  231.          nextBg = soundTable[param1].play();
  232.          nextBg.soundTransform = new SoundTransform(maxSound);
  233.          voiceTimer = setTimeout(voiceDown,100);
  234.       }
  235.       
  236.       public function clearAllSounds() : void
  237.       {
  238.          bgStartTime = -1;
  239.          currentBgVolume = -1;
  240.          if(playTable[TYPE_MAN])
  241.          {
  242.             playTable[TYPE_MAN].stop();
  243.             try
  244.             {
  245.                playTable[TYPE_MAN].removeEventListener(Event.SOUND_COMPLETE,onManSdComplete);
  246.             }
  247.             catch(e:Error)
  248.             {
  249.             }
  250.          }
  251.          try
  252.          {
  253.             clearTimeout(voiceTimer);
  254.          }
  255.          catch(e:Error)
  256.          {
  257.          }
  258.          if(nextBg)
  259.          {
  260.             nextBg.stop();
  261.             nextBg = null;
  262.          }
  263.          if(playTable[TYPE_BTN])
  264.          {
  265.             playTable[TYPE_BTN].stop();
  266.          }
  267.          if(playTable[TYPE_BG])
  268.          {
  269.             playTable[TYPE_BG].stop();
  270.             playTable[TYPE_BG].removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
  271.          }
  272.          if(playTable[TYPE_ITEM])
  273.          {
  274.             playTable[TYPE_ITEM].stop();
  275.          }
  276.          playTable = new Object();
  277.       }
  278.       
  279.       public function playSound(param1:String, param2:String = "b", param3:Boolean = false) : void
  280.       {
  281.          var sign:String = param1;
  282.          var type:String = param2;
  283.          var _repeat:Boolean = param3;
  284.          if(soundTable[sign] == null)
  285.          {
  286.             return;
  287.          }
  288.          switch(type)
  289.          {
  290.             case TYPE_MAN:
  291.                if(playTable[TYPE_MAN])
  292.                {
  293.                   playTable[TYPE_MAN].stop();
  294.                   try
  295.                   {
  296.                      playTable[TYPE_MAN].removeEventListener(Event.SOUND_COMPLETE,onManSdComplete);
  297.                   }
  298.                   catch(e:Error)
  299.                   {
  300.                   }
  301.                }
  302.                playTable[TYPE_MAN] = soundTable[sign].play();
  303.                playTable[TYPE_MAN].soundTransform = new SoundTransform(maxSound);
  304.                if(_repeat)
  305.                {
  306.                   playTable[TYPE_MAN].addEventListener(Event.SOUND_COMPLETE,onManSdComplete,false,0,true);
  307.                }
  308.                break;
  309.             case TYPE_BTN:
  310.                playTable[TYPE_BTN] = soundTable[sign].play();
  311.                playTable[TYPE_BTN].soundTransform = new SoundTransform(maxSound);
  312.                break;
  313.             case TYPE_BG:
  314.                if(playTable[TYPE_BG])
  315.                {
  316.                   playTable[TYPE_BG].stop();
  317.                   playTable[TYPE_BG].removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
  318.                }
  319.                playTable[TYPE_BG] = soundTable[sign].play();
  320.                playTable[TYPE_BG].soundTransform = new SoundTransform(maxSound);
  321.                if(nextPlay)
  322.                {
  323.                   bgVoiceDown("geomBg");
  324.                }
  325.                playTable[TYPE_BG].addEventListener(Event.SOUND_COMPLETE,onBgComplete,false,0,true);
  326.                break;
  327.             case TYPE_ITEM:
  328.                playTable[TYPE_ITEM] = soundTable[sign].play();
  329.                playTable[TYPE_ITEM].soundTransform = new SoundTransform(maxSound);
  330.          }
  331.       }
  332.    }
  333. }
  334.