forked from: 玉転がし forked from: 迷路(skatou)

by figma000 forked from 玉転がしin迷路 (diff: 139)
♥0 | Line 157 | Modified 2012-05-24 17:26:43 | 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/xgep
 */

// forked from aduonety's 玉転がし forked from: 迷路
// forked from figma000's 迷路(くっつける練習)
// 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 frocessing.math.*;
    import flash.geom.Rectangle;
    import flash.events.MouseEvent;
    import jp.progression.commands.lists.*;
    import flash.display.Sprite;
    import flash.display.*;
    import flash.events.*;
    import flash.text.TextField;
    import flash.utils.*;
    
   [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();
        private var list:SerialList = new SerialList();
         
        private var boxA:QuickObject;
        private var boxB:QuickObject;
        private var parts:Array = [];
        public var text_field:TextField  = new TextField();
        
        private const wallWidth:Number = 0.25;//それぞれに使う定数
        private const wallHeight:Number = 0.25;
        private const ballRadius:Number = 0.1;
        private const wallQuantity:Number = 29;
        private const wallSpace:Number = 4;//壁と壁との距離を割る数 つまり多いほど距離は詰まる
        private const array:Number = 29;//wallQuantity+1でいいんじゃないかな
        public var data:Array = new Array(array);      
        //フラグ用の配列の生成
        public function Makearray():void
       {
           {
            for (var i_m:int = 0; i_m < array; i_m++)
                {
                data[i_m] = new Array(array);
                }         
                for(var i:int = 0; i<array; i++){
                   for(var j:int = 0; j<array; j++)
                   {
                    data[i][j]= 0;    //初期値0
                   }
                }
           }
       }
         // 配列の値を返す  
        public function CheckExistBox(xe:int, ye:int):int
        {              
         var value:int = data[xe][ye];         
            return value;
        }           
        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, 40);
            sim.iterations = 10;
           
            sim.setDefault( { fillColor:0xFFFFFF, lineColor:0x000000 } );
            Makearray();
            
            //玉の生成
            for (var i:int = 0; i < 1; i++)  
            {
                //balls.push(sim.addCircle( { x:Math.random() * 14, y:Math.random() * 1, radius:ballRadius ,fillColor:0xdc143c} ));
                balls.push(sim.addCircle( { x:8.6,y:2.22, radius:ballRadius ,fillColor:0xdc143c} ));
            }
            {//
             //枠生成             
              for(var ix:int = 0; ix < wallQuantity; ix++){
                    for(var iy:int = 0; iy < wallQuantity; iy++){
                           if(ix == 0 || ix == (wallQuantity-1) || iy == 0 || iy == (wallQuantity-1)){
                               parts.push(sim.addBox( { x:(ix/wallSpace), y:(iy/wallSpace), width:wallWidth, height:wallHeight} ));
                           }                           
                    }
              }                      
              //枠の中身
             {
             for(var ix_d:int = 1; ix_d < (wallQuantity-1); ix_d++){
                 for(var iy_d:int = 1; iy_d < (wallQuantity-1); iy_d++){
                           if(ix_d % 2 == 0 && iy_d % 2 == 0){
                               parts.push(sim.addBox( { x:(ix_d/wallSpace), y:(iy_d/wallSpace), width:wallWidth, height:wallHeight} ));
                                                          
                         switch (int(Math.random() * 4)) // 0~3のランダム値を生成
                                {
                                    // 右に倒す
                                    case 0: ix_d++;
                                        parts.push(sim.addBox( { x:(ix_d/wallSpace), y:(iy_d/wallSpace), width:wallWidth, height:wallHeight } ));
                                        data[ix_d][iy_d] = 1;
                                        ix_d--;
                                        break;
                     
                                    // 左                
                                    case 1: ix_d--; 
                                    if(CheckExistBox(ix_d,iy_d) == 1){
                                        ix_d++;
                                        iy_d--;
                                        break;
                                    }
                                       else {
                                        parts.push(sim.addBox( { x:(ix_d/wallSpace), y:(iy_d/wallSpace), width:wallWidth, height:wallHeight} ));
                                        data[ix_d][iy_d] = 1;
                                        ix_d++;                                       
                                         break;
                                    }                               
                                    // 下
                                    case 2: iy_d++; 
                                        parts.push(sim.addBox( { x:(ix_d/wallSpace), y:(iy_d/wallSpace), width:wallWidth, height:wallHeight} ));
                                        data[ix_d][iy_d] = 1;
                                        iy_d--;                                         
                                         break;             
                                   // 上
                                    case 3: iy_d--; 
                                    if(CheckExistBox(ix_d,iy_d) == 1){
                                        break;
                                    }
                                    else{
                                    parts.push(sim.addBox( { x:(ix_d/wallSpace), y:(iy_d/wallSpace), width:wallWidth, height:wallHeight} ));
                                    data[ix_d][iy_d] = 1;
                                        iy_d++;                                       
                                         break;
                                     }
                                }   
                           } 
                    }
              }
              }
                  }
            //sim.createStageWalls();
                  
            var timer:Timer = new Timer(100, 0);
            timer.addEventListener(TimerEvent.TIMER, onTick);
            timer.start();
            
            boxA = sim.addGroup({objects:parts, x:1.8, y:2, angle:0});
            boxB = sim.addJoint({type:"revolute", a:boxA.body, b:sim.w.GetGroundBody()});
            sim.start();
            sim.mouseDrag();
        }
   
        private function onTick(evt:TimerEvent):void {
            stage.addChild(text_field);
              text_field.text = "Time   "+int(getTimer()/1000)+" second";
        }
        
        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());
            }
       }
    }
}