forked from: forked from: forked from: [QuickBox2D] Doll in space_2

by figma000 forked from ball in maze (diff: 152)
♥0 | Line 139 | Modified 2012-05-23 10:32:09 | MIT License
play

ActionScript3 source code

/**
 * Copyright figma000 ( http://wonderfl.net/user/figma000 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7zSe
 */

// forked from aduonety's forked from: forked from: [QuickBox2D] Doll in space_2
// forked from a24's forked from: [QuickBox2D] Doll in space
// forked from paq's [QuickBox2D] Doll in space

package 
{
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import com.actionsnippet.qbox.objects.CircleObject;
    import com.actionsnippet.qbox.QuickBox2D;
    import com.actionsnippet.qbox.QuickObject;
    import flash.display.MovieClip;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.events.MouseEvent;

   [SWF(width = 465, height = 465, backgroundColor = 0x333333, frameRate = 60)]
    
    public class Sample extends MovieClip
    {
        private var sim:QuickBox2D;
        private var fixedPoint:b2Vec2 = new b2Vec2(7.25, 7.25);
        private var scale:Number = 10;
        private var balls:Array = new Array();
        
        public function Sample():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            sim = new QuickBox2D(this);
            sim.gravity = new b2Vec2(0, 30);
            sim.iterations = 10;
            
            sim.setDefault( { fillColor:0xFFFFFF, lineColor:0x333333 } );
            
            for (var i:int = 0; i < 1; i++)  //玉の生成
            {
                balls.push(sim.addCircle( { x:Math.random() * 15.5, y:Math.random() * 7, radius:0.2 } ));
            }

              {
////////////////////////////////////////////////////////////////////////////////////////////////////////                  
              //for文でボックス生成
              //for(var ix:int = 0;ix < 16; ix++){
                   //for(var j:int = 0;j < 16;j++){
                  //sim.addBox( { x:ix+0.5, y:j+0.7, width:0.5, height:0.5,angle:45,density:0 } );
                  
                  //}
                  //}
              }
////////////////////////////////////////////////////////////////////////////////////////////////////////              
            createWall();
            sim.start();
            sim.mouseDrag();
        }

        
        private function loop(e:Event):void 
        {
            for ( var i:int = 0; i < balls.length; i ++ )
            {
               var ball:QuickObject = balls[i];
                var fp:b2Vec2 = fixedPoint.Copy();
               fp.Subtract(ball.body.GetPosition());
                fp.Normalize();
               fp.Multiply(50);
                balls.push(sim.addCircle( { x:Math.random() * 15.5, y:Math.random() * 7, radius:0.6 } ));
               ball.body.ApplyForce(fp, ball.body.GetWorldCenter());
                ball.body.ApplyForce(new b2Vec2(0, -30), ball.body.GetWorldCenter());
            }
       }
       
       
       private function createWall(event:Event = null):void
       {
        private var panel:Panel
        private var bd:BitmapData;
        private var destbd:BitmapData;
        private var bitmap:Bitmap;
        private var list:SerialList = new SerialList();
        private var wait:Number = 10 / 1000.0;
        
        Wonderfl.capture_delay(10);
            
            panel = new Panel(this);
            panel.width = panel.height = 465;
            
            stage.frameRate = 120;
            bd = new BitmapData(100, 100, false, Status.FIELD);
            
            destbd = bd.clone();
            
            bitmap = new Bitmap(destbd);
            bitmap.scaleX = bitmap.scaleY = 6;
            bitmap.x = (465 - bitmap.width) / 2;
            panel.content.addChild(bitmap);
            dig2()
       }
       
       
       
       
       
       
               private function dig2(event:Event = null):void
        {
            list.clearCommand(true);
            list = new SerialList();
            bd = new BitmapData(100, 100, false, Status.FIELD);
            
            destbd = bd.clone();
            bitmap.bitmapData = destbd;
       
            for (var y:int = 0; y < bd.height; y++)
            {
                for (var x:int = 0; x < bd.width; x++)
                {//外壁と柱づくり
                    if (y == 0 || x == 0 || y == bd.height - 1 || x == bd.width - 1 || y % 2 == 0 && x % 2 == 0)//外かべづくり
                    {
                        bd.setPixel(x, y, Status.WALL);
                        list.addCommand(new Func(function(x:int, y:int):void { destbd.setPixel(x, y, Status.WALL); }, [x, y]));
                        list.addCommand(new Wait(wait));
                    }
                }
            }
            for (y = 2; y < bd.height - 1; y += 2)
            {
                var dx:int = 2;
                var dy:int = y;
                
                switch (int(Math.random() * 4))
                {
                    case 0: dx++; break;
                    case 1: dx--; break;
                    case 2: dy++; break;
                    case 3: dy--; break;
                }
                if (bd.getPixel(dx, dy) != Status.WALL)
                {
                    bd.setPixel(dx, dy, Status.WALL);
                    list.addCommand(new Func(function(x:int, y:int):void { destbd.setPixel(x, y, Status.WALL); }, [dx, dy]));
                    list.addCommand(new Wait(wait));
                }
                else y -= 2;
            }
            for (x = 4; x < bd.width - 1; x += 2)
            {
                for (y = 2; y < bd.height - 1; y += 2)
                {
                    dx = x;
                    dy = y;    
                    switch (int(Math.random() * 3))
                    {
                        case 0: dy++; break;
                        case 1: dy--; break;
                        case 2: dx++; break;
                    }
                    if (bd.getPixel(dx, dy) != Status.WALL)
                    {
                        bd.setPixel(dx, dy, Status.WALL);
                        list.addCommand(new Func(function(x:int, y:int):void { destbd.setPixel(x, y, Status.WALL); }, [dx, dy]));
                        list.addCommand(new Wait(wait));
                    }
                    else y -= 2;
                }
            }
            list.execute();
        }













       
       
    }
}