package {
    import com.derschmale.fluids.ShallowWaterContainer;
    
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    /**
     * @author David Lenaerts (http://www.derschmale.com)
     */
    
    [SWF(width="512", height="512", backgroundColor="0x000000", frameRate="30")]
    public class RipplerV2 extends Sprite
    {
        private var _rippler : ShallowWaterContainer;
        
        private var _oldX : Number;
        private var _oldY : Number;
        
        [Embed(source="ocean.jpg")]
        private var Background : Class;
        
        public function RipplerV2()
        {
            // create the shallow water container and add it to the stage
            _rippler = new ShallowWaterContainer(512, 512, 200, 200);
            addChild(_rippler);
            
            // set the fluid simulation properties
            _rippler.timeStep = 1;
            _rippler.viscosity = 0.2;
            _rippler.drag = 0.01;
            _rippler.relaxation = 0.15;
            _rippler.relaxationSteps = 2;
            
            // add the background image to the rippler container
            _rippler.addChild(new Background());
            _rippler.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
        }
        
        public function handleMouseMove(event : MouseEvent) : void
        {
            // add velocity to the water in the direction of the mouse movement
            _rippler.addVelocity(    _rippler.mouseX, _rippler.mouseY,
                                    _rippler.mouseX-_oldX, _rippler.mouseY-_oldY,
                                    5, 1);
            _oldX = _rippler.mouseX;
            _oldY = _rippler.mouseY;
            
            // increase the water density at the mouse position, simulating a pressure acting on the surface
            _rippler.addPressure(_rippler.mouseX, _rippler.mouseY, 7, 1);
        }
    }
}