package meyermike.minild9.scripts 
{
    import flash.events.KeyboardEvent;
    import meyermike.Layers;
    import meyermike.Main;
    import meyermike.minild9.Constants;
    import meyermike.minild9.Music;
    import meyermike.script.Script;
    import meyermike.script.Step;
    import meyermike.script.step.CreateGameObjectFromBitmap;
    import meyermike.script.step.Loop;
    import meyermike.script.step.Parallel;
    import meyermike.script.step.RunFunc;
    import meyermike.script.step.Wait;
    
    /**
     * ...
     * @author Mike Meyer
     */
    public class Intro extends Script
    {
        [Embed(source='../../../../assets/Intro1.png')]
        private static var Graphic1Class:Class;
        
        [Embed(source='../../../../assets/Intro2.png')]
        private static var Graphic2Class:Class;
        
        [Embed(source='../../../../assets/Intro3.png')]
        private static var Graphic3Class:Class;
        
        [Embed(source='../../../../assets/Intro4.png')]
        private static var Graphic4Class:Class;
        
        [Embed(source='../../../../assets/Intro5.png')]
        private static var Graphic5Class:Class;
        
        public function Intro()
        {
            this.mBeatsPerMinute = 30.0;
            super();
        }
        
        /**
         * steps to execute in this script
         */
        protected override function CreateScript():Step 
        {
            var steps:/*Step*/Array = [
                new CreateGameObjectFromBitmap(Layers.PlatformLayer, Graphic1Class, 0.5 * Constants.STAGE_WIDTH, 0.5 * Constants.STAGE_HEIGHT),
                new Parallel( [ 
                    new RunFunc(StartWatchingForKeyPress),
                    new CreateGameObjectFromBitmap(Layers.PlatformLayer, Graphic2Class, 0.5 * Constants.STAGE_WIDTH, 0.5 * Constants.STAGE_HEIGHT)
                ] ),
                new CreateGameObjectFromBitmap(Layers.PlatformLayer, Graphic3Class, 0.5 * Constants.STAGE_WIDTH, 0.5 * Constants.STAGE_HEIGHT),
                new CreateGameObjectFromBitmap(Layers.PlatformLayer, Graphic4Class, 0.5 * Constants.STAGE_WIDTH, 0.5 * Constants.STAGE_HEIGHT),
                new CreateGameObjectFromBitmap(Layers.PlatformLayer, Graphic5Class, 0.5 * Constants.STAGE_WIDTH, 0.5 * Constants.STAGE_HEIGHT)
                ];
            return new Loop(1, steps);
        }
        
        /**
         * watch for the keypress that skips the intro
         */
        private function OnKeyDown(e:KeyboardEvent):void
        {
            Main.sInstance.SwitchToScript(new Game());
        }
         
        /**
         * intitial set up
         */
        public override function Begin():void 
        {
            super.Begin();
            
            Music.StartNewMusic();
        }
        
        /**
         * 
         */
        public function StartWatchingForKeyPress():void
        {
            Main.sInstance.stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
        }
        
        /**
         * clean up
         */
        public override function End():void 
        {
            super.End();
            Main.sInstance.stage.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
            Layers.RemoveAll();
        }
    }
    
}