package {
    import com.derschmale.bumpmaps.BumpMap;
    import com.derschmale.bumpmaps.ParallaxMap;
    import com.derschmale.bumpmaps.lightmaps.LightMap;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    [SWF(width="512", height="512", backgroundColor="#000000")]
    public class Parallax extends Sprite
    {
        // the normal texture for the wall
        [Embed(source="../assets/images/lion.jpg")]
        private var _wallSource : Class;
        
        // the bump map
        [Embed(source="../assets/images/lion-bump.jpg")]
        private var _wallBump : Class;
        
        private var _bitmap : Bitmap;
        private var _bumpMap : BumpMap;
        private var _parallaxMap : ParallaxMap;
    
        public function Parallax()
        {
            var heightmap : BitmapData = new _wallBump().bitmapData;
            var lightmap : LightMap = new LightMap(700, 2, 20, 0xffffcc, 0x000010);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            // create target for bump map
            _bitmap = new Bitmap(new _wallSource().bitmapData);
        
            // create bump map
            _bumpMap = new BumpMap(_bitmap, heightmap, lightmap, 200);
            
            // create parallax using bump mapped image and add to stage
            _parallaxMap = new ParallaxMap(_bumpMap, heightmap, 25, 3);
            addChild(_parallaxMap);
            
            stage.focus = this;
            stage.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        }
        
        private function handleEnterFrame(event : Event) : void
        {
            // eye position and light position follow mouse position
            _parallaxMap.eyeX = mouseX;
            _parallaxMap.eyeY = mouseY;
            _parallaxMap.render();
            _bumpMap.lightX = mouseX;
            _bumpMap.lightY = mouseY;
            _bumpMap.render();
        }
    }
}