In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: forked from: forked from: 砂ゲー

水追加
Get Adobe Flash player
by ElectricEel 09 Aug 2011
/**
 * Copyright ElectricEel ( http://wonderfl.net/user/ElectricEel )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/gCKl
 */

// forked from ElectricEel's forked from: forked from: forked from: 砂ゲー
// forked from ElectricEel's forked from: forked from: 砂ゲー
// forked from ElectricEel's forked from: 砂ゲー
// forked from kuma360's 砂ゲー
//水追加
package 
{
    import com.bit101.components.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    
    [SWF ( width = 265, height = 265, frameRate = 1 )];
    public class Main extends Sprite 
    {
        private var q:uint;
        
        // these describe the color of everything
        private var empty:int = 0x000000;  // note: empty is not a keyword in this language
        private var plant:int = 0x006000;
        private var stone:int = 0x403080;
        private var smoke:int = 0x404040;
        private var water:int = 0x80A2F0;
        private var fiber:int  = 0xc3d034;
        private var dirt:int = 0x604020;
        private var steam:int = 0xc0c0c0;
        private var electron:int = 0xF7E779;
        private var fire:int = 0xff0000;

        // this is the top, left of the area you want to render.
        // typical values might be 100,30 for the small window while editing on wonderfl,
        private var topY:int=100;
        private var topX:int=30;

        // in herited, i have no idea what this does
        // makes a new bitmap?  is this like a drawing surface
        private var _CANVAS:BitmapData = new BitmapData ( stage.stageWidth , stage.stageHeight , false , 0 ) ;
        public var grid:Array = new Array(stage.stageWidth,stage.stageHeight);        
        
        
        
        // is there a way to combine these into something shorter?  
        // or at least place them somewhere else in the code?
        private var _B0:PushButton ; 
        private var _B1:PushButton ; 
        private var _B2:PushButton ; 
        private var _B3:PushButton ; 
        private var _B4:PushButton ; 
        private var _B5:PushButton ; 
        private var _B6:PushButton ; 
        private var _B7:PushButton ;

        private var _CLICK:Boolean = false ;
        private var _COLOR:uint = water;   // this is where you select the default material they paint with!
        private var _BX:int = 0 ;
        private var _BY:int = 0 ;
        private var temp:int;
        
        
        public function Main():void {       
            
            addChild ( new Bitmap ( _CANVAS ) ) ;
            
            // add new materials here
            _B0 = new PushButton ( this,  10 , 0, "erase" ) ;
            _B1 = new PushButton ( this, 120 , 0, "Fiber" ) ;      // wood/fiber are the same
            _B2 = new PushButton ( this, 230 , 0, "e-" ) ;
            _B3 = new PushButton ( this, 340 , 0, "Water" ) ;
            _B4 = new PushButton ( this,  10 , 30, "Stone" ) ;
            _B5 = new PushButton ( this, 120 , 30, "Plant" ) ;
            _B6 = new PushButton ( this, 230 , 30, "Fire" )  ;  
            _B7 = new PushButton ( this, 340 , 30, "Dirt" )  ;
            
            _B3.enabled = false ;
                        
             // It would be to combine these two together (this and the previous tw oparagraphs)
            _B0.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = empty ; _B0.enabled = false ; _B1.enabled = true  ; _B2.enabled = true  ; _B3.enabled = true  ; } ) ;
            _B1.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = fiber ; _B0.enabled = true  ; _B1.enabled = false ; _B2.enabled = true  ; _B3.enabled = true  ; } ) ;
            _B2.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = electron ; _B0.enabled = true  ; _B1.enabled = true  ; _B2.enabled = false ; _B3.enabled = true  ; } ) ;
            _B3.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = water ; _B0.enabled = true  ; _B1.enabled = true  ; _B2.enabled = true  ; _B3.enabled = false ; } ) ;
            _B4.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = stone ; _B0.enabled = true  ; _B1.enabled = true  ; _B2.enabled = true  ; _B3.enabled = false ; } ) ;
            _B5.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = plant ; _B0.enabled = true  ; _B1.enabled = true  ; _B2.enabled = true  ; _B3.enabled = false ; } ) ;
            _B6.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = fire ; _B0.enabled = true  ; _B1.enabled = true  ; _B2.enabled = true  ; _B3.enabled = false ; } ) ;
            _B7.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = dirt ; _B0.enabled = true  ; _B1.enabled = true  ; _B2.enabled = true  ; _B3.enabled = false ; } ) ;
            

            // makes it so that when you click down or up, it turns spray on or off                       
            stage.addEventListener ( MouseEvent.MOUSE_DOWN , function ():void { _CLICK = true ; } ) ;
            stage.addEventListener ( MouseEvent.MOUSE_UP   , function ():void { _CLICK = false; } ) ;
            addEventListener ( Event.ENTER_FRAME , RUN ) ;
        }
      
        public function RUN ( e:Event ):void {
             
            _CANVAS.lock() ;

            // unlock if you want some random pixels sprayed each frame
            // are there #ifdef's in whatever language this is?
            /* 
            for ( var I:int = 200 ; I < 250 ; ++ I ) {
                if ( Math.random() < .0001 ) {
                    _CANVAS.setPixel ( I , 260 +100*Math.random(), 0xF7E779 ) ;
                }       
            }
            */
            
            // If you click within the drawing rectangle
            // then spray some material
            if ( _CLICK && (60 < mouseY) && (60 < _BY) ) {
                
                for ( var J:int = 0 ; J < 20 ; ++ J ) {
                    var R:Number = J / 20 ;
                    _CANVAS.fillRect ( new Rectangle ( _BX * R + mouseX * ( 1 - R ) , _BY * R + mouseY * ( 1 - R ) , 5 , 5 ) , _COLOR ) ;
                }
                
            }
            _BX = mouseX ;
            _BY = mouseY ;           
            
            var T:uint ;
            var TX:int ;
            var x2:int;
            var y2:int;
            var X:int;
            var Y:int;
            
            // each time the frame is redrawn, it can simluate interactions
            // in a certain direction.            
            // direction control: left right up down
            q=q+1; if (q>3){q=0;}
            //q= q+Math.floor(2*Math.random());   // unlock for random direction

            // this most important loop that has to execute as fast as hell, and I mean HELL.
            // think about the system you're running on and everything.
            // this loop draws a single frame.  it redraws about 15% of the pixels on the screen.
            // you can adjust this ratio.  too many pixels per frame, and the updates won't look
            // smooth or belivable.  too few pixels for frame, and it's just too damn slow, because
            // the things that happen outside of this loop, whatever they are, seem to be significantly
            // long that they compete with the image manipulation code.                        
            for (var Count:int = 0; Count <= _CANVAS.width * _CANVAS.height*.15; Count ++)
            {
                // we use random pixels, but this routine seems to suck.
                // the Math.random() functions seems to be rather slow!
                // and floating point math, who knows what happens in the compilation heh
                // so it would be nice to replace this with a pseudo-random generator.
                // especially one gauarnteed to reach all values equally.
                // if this were an fpga, it would def be done with variable-length LFSR/seeds
                X= topX+Math.floor((_CANVAS.width/2*Math.random()));
                Y= topY+Math.floor((_CANVAS.height/2*Math.random()));       
                {    
                    //  get source particle from looking at a particular pixel
                    var C:uint = _CANVAS.getPixel ( X , Y ) ;
                 
                    // look from the source out into a particular direction
                    // directions  0-3  mean left, right, up, down
                    if(q==0)     {x2 = X-1;  y2 = Y;}
                    else if(q==1){x2 = X+1;  y2 = Y;}
                    else if(q==2){x2 = X;    y2 = Y-1;}
                    else if(q==3){x2 = X;    y2 = Y+1;}
                    
                    // at a particular target particle 
                    T = _CANVAS.getPixel ( x2 ,y2) ;

                    // an optimization that also improves consistency
                    // make Target the smaller number
                    // make sourCe the larger number
                    // e.g. interaction between fiber and water is found under fiber, because fiber is larger in RGB.
                    
                    if(C<T){  
                        temp=T;T=C;C=temp; temp=x2;x2=X;X=temp; temp=y2;y2=Y;Y=temp;
                        if(q<2){q=1-q;} else { q = ( 1-(q-2))+2; } 
                    }     
      
              
                    
                    // if source is fire....
                    if(C==fire)
                    {
                       
                       //  fire goes up
                       if(q==2)
                       {
                            if(T==0x000000)
                            {
                               _CANVAS.setPixel(x2,y2,C);
                               _CANVAS.setPixel(X,Y,0);
                               continue;                       
                            }
                       }
                       
                       // less often fire goes down
                       if(q==3){
                           if(T==0x000000){
                              if(Math.random()<.3){
                              _CANVAS.setPixel(x2,y2,C);
                               _CANVAS.setPixel(X,Y,0);  
                              }      
                           }
                       }
                       
                       // fire sometimes just disappears
                       if(Math.random()<.03){
                           _CANVAS.setPixel(X,Y,0);
                           
                            // make smoke
                             if(Math.random()<.90){
                               _CANVAS.setPixel(X,Y,smoke);
                             }  
                             
                           continue;
                       }
                       
                       // if fire sees kudzu,  turn it to wood
                        if(T==plant)
                        {                
                            _CANVAS.setPixel(x2,y2,fiber);   
                            
                            // make smoke
                             if(Math.random()<.90){
                               _CANVAS.setPixel(X,Y,smoke);
                             }  
                            continue;       
                        }
                        
                       // fire vs. wood, turn it to fire
                       if(T==fiber)
                       {
                           // burn wood
                           _CANVAS.setPixel(x2,y2,C);
                               if(Math.random()<.25){
                                   _CANVAS.setPixel(x2,y2,smoke);    
                               }
                            continue;
                       }
                       
                        
                        // fire vs. smoke.. swap places
                        if(T==smoke)
                        {
                            _CANVAS.setPixel(x2,y2,fire);    
                            _CANVAS.setPixel(X,Y,smoke);
                            continue;
                        }


                       // fire sees water, makes a steam and a smoke
                       if(T==water)
                       {
                           _CANVAS.setPixel(X,Y,smoke);
                           _CANVAS.setPixel(x2,y2,steam);
                           continue;
                       }
                             
                        // nothing else happens this round
                        continue; 
                         
                    }

                    
                    
                    

                    // source particle: Fiber
                    if( (C==fiber))
                    {
                       // if fiber sees space, go down
                       if( (T==0x000000) )
                       {
                           if((q==3))
                           {
                               _CANVAS.setPixel(X,Y,0x000000);  // erase old fiber
                               _CANVAS.setPixel(x2,y2,fiber);               
                               continue;
                           }
                       }
                       
                        // fiber attack vegetation - turn veggie to fiber
                        if(T==0x006000)
                        {
                            if(Math.random()<.0125){_CANVAS.setPixel(x2,y2,fiber);}
                            continue;
                        }

                        // if he sees fire, burn up!
                        if(T==fire)
                        {    
                            _CANVAS.setPixel(X,Y,fire);
                            if(Math.random()<.3){
                                _CANVAS.setPixel(x2,y2,smoke);
                            }
                            continue;
                        }

                        // fiber decays over time
                        if(T==fiber)
                        {
                           if(Math.random()<.0002){
                                _CANVAS.setPixel(X,Y,0);
                            }  
                        }


                        // fiber should float on water
                        if(T==water)
                        {
                            if(q==2)
                            {
                                // swap places
                                //_CANVAS.setPixel(X,Y,fiber);   
                                //_CANVAS.setPixel(x2,y2,water);  
                                
                                _CANVAS.setPixel(X,Y,water);   // turn orig into fiber
                               _CANVAS.setPixel(x2,y2,fiber);  // turn target into fier
                            }

                            // sometimes water turns fiber to vegetation
                            if(Math.random()<.01){
                                _CANVAS.setPixel(X,Y,plant)             ;
                            } 
                            
                            continue;
                        }
                        
                        continue;
                    }

                    // particle: egetation
                    if( C== plant)
                    {
                       // too much vegetation?
                       // one turns into fiber
                        if(T==plant)
                        {
                            if(Math.random()<.00001){
                            //_CANVAS.setPixel(X,Y,0x836034);
                            _CANVAS.setPixel(x2,y2,fiber);
                            }
                            continue;
                        }
                                                
                        if(T==fiber)
                        {
                            if(Math.random()<.0001)
                            {
                                if(Math.random()<.4){
                                   _CANVAS.setPixel(x2,y2,plant);
                                } else{
                                  _CANVAS.setPixel(x2,y2,fiber);
                                }
                                if(Math.random()<.5){
                                   _CANVAS.setPixel(X,Y,plant);
                                } else{
                                  _CANVAS.setPixel(X,Y,fiber);
                                }
                            }
                        }

                        // turns water to veg
                        if(T==water) 
                        {
                              _CANVAS.setPixel(x2,y2,plant);
                              _CANVAS.setPixel(X,Y,T);
                              continue;
                        }
                      
                        if(T==fire)  // veg attack fire, turns to wood
                        {
                                _CANVAS.setPixel(X,Y,fiber);
                                _CANVAS.setPixel(x2,y2,0);
                                continue;
                        } 


                        // sometimes it can grow left or right
                        if(q<2)
                        {
                             if(T==0)
                             {
                                    if(Math.random()<.013){
                                        _CANVAS.setPixel(x2,y2,C);
                                    continue;
                                    }
                              }
                        } 

                        // can grow upward
                        if(q==2)
                        {
                                if(T==water)
                                {
                                   if(Math.random()<.5){ _CANVAS.setPixel(x2,y2,C);}
                                    continue;
                                }
                        }

                        // can creep downward
                       if(q==3)
                       {
                          if(T==0x000000)
                            {  
                                if(Math.random()<.5){
                                        _CANVAS.setPixel( x2,y2,C);
                                        _CANVAS.setPixel(X,Y,0);
                                        continue;
                                }
                            }
                        }
                        
                        continue;

                    }


                    // electrons try to move in a random direction
                    //落下    
                    if ( C == 0xF7E779 ) 
                    {
      
                         // into air
                         if ( T == 0 ) 
                         {
                                 _CANVAS.setPixel ( X , Y  , 0 ) ;
                                 _CANVAS.setPixel ( x2, y2 , C ) ;
                                 continue ;
                         }
                         // into wire/weird    
                         if ( T == stone) 
                         {
                                 _CANVAS.setPixel ( X , Y  , 0xffffff ) ;
                                 _CANVAS.setPixel ( x2, y2 , C ) ;
                                 continue ;
                          }
                          // into green stuff    
                          if ( T ==plant)
                          {
                                _CANVAS.setPixel ( X , Y  , T ) ;
                                continue;
                           }
                           continue;
                    }



                    //水より砂の方が重い,適当な確率で場所の置換を許す。
                    // water
                    if ( C == water)
                    {
                        // water can turn fiber into plants
                        if(T==fiber)
                        {
                            _CANVAS.setPixel(x2,y2,plant);
                            continue;
                        }

                        // fall down
                        if(q==3){
                            //T = _CANVAS.getPixel ( X , Y + 1 ) ;
                            if ( T == 0) {
                                    _CANVAS.setPixel ( X, Y ,0 );
                                    _CANVAS.setPixel ( X, Y+1, C );
                                    continue ;
                            }
                            if ( T == smoke) {
                                    _CANVAS.setPixel ( X, Y     ,0 );
                                    _CANVAS.setPixel ( X, Y+1,C );
                                    continue ;
                            }                            
                            
                        }
                        
                        // move left and right
                        if(q<2)
                        {
                            //左右移動
                            //TX =  X + Math.floor ( Math.random() * 7 ) - 3 ;
                            //T = _CANVAS.getPixel ( TX , Y ) ;
                            if ( T == 0 ) {
                                _CANVAS.setPixel (  X , Y , 0 ) ;
                                _CANVAS.setPixel ( x2 , y2, water ) ;
                                continue ;
                            }
                        }
                        
                        continue;  // nothing else this round
                    }
            

                   // smoke
                    if( (C==smoke && ( (q==2) || (q==1)) ) )
                    {
                        if(T==0x000000)
                        {
                            _CANVAS.setPixel(x2,y2,smoke);
                            _CANVAS.setPixel(X,Y,0);
                            continue;
                        }
                        if(T==stone)
                        {
                              _CANVAS.setPixel(X,Y,water);
                              continue;
                        }

                        if(T==smoke)
                        {
                             _CANVAS.setPixel(X,Y,0);
                             continue;
                        }

                    }
                    
                    
                    
                    // particle: steam,  moves up and to the left
                    if( (C==steam) && ( (q==2) || (q==0) ) )
                    {
                        // turns back into water at top of screen
                        if( (Y==topY) || (X==topX) )
                        {
                         _CANVAS.setPixel(X,Y,water);
                         continue;
                        }

                        // rises into empty spaces
                        if(T==0x000000)
                        {
                            _CANVAS.setPixel(x2,y2,steam);
                            _CANVAS.setPixel(X,Y,0);
                            continue;
                        }
                        
                        // changes places with water
                        if(T==water)
                        {
                            _CANVAS.setPixel(x2,y2,C);
                            _CANVAS.setPixel(X,Y,T);
                            continue;
                        }
                        
                        // rots vegetation
                        if(T==plant)
                        {
                            _CANVAS.setPixel(x2,y2,0x000000);
                            _CANVAS.setPixel(X,Y,0);
                            continue;
                        }
                        
                        // turns back into water if it hits stone
                        if(T==stone)
                        {
                            _CANVAS.setPixel(X,Y,water);
                            continue;
                        }


                        continue; // nothing else this round
                    }
                }
            }
                        
            _CANVAS.unlock() ;
            
        }
        
    }
    
}