flash on 2016-6-2

by mutantleg
♥0 | Line 175 | Modified 2016-06-02 04:08:47 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.AVM1Movie;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
           stage.quality="LOW"; 
            
           initEmpty(30,30); 
           
           var i:int;
           for(i=0;i<8;i+=1)
           {
             setTile(4,4+i,1);     
             setTile(4,16+i,1);     
             setTile(4+i,4,1);     
             setTile(8+i,8,1);     
             setTile(8+i,8+16,1);     
             setTile(15+i,8+12,1);     
             setTile(19,8+i,1);     
               
           }//nexti
            
           for (i=0;i<160;i+=1)
           { setTile(Math.random()*mw,Math.random()*mh,1); } 
            
           var a:xOrb;
           for(i=0;i<256;i+=1)
           {
             var ax:Number; var ay:Number;
             ax = Math.random()*mw*cw;
             ay = Math.random()*mw*ch;
             if (getTile(ax/cw,ay/ch)>0){ continue;}  
               a = new xOrb();
               a.cx=ax;a.cy=ay;
               vecOrb.push(a);
               if (vecOrb.length > 12){break;}              
           }//nnexti            


            
           stage.addEventListener(Event.ENTER_FRAME, onEnter); 
        }//ctor
        
        public var curFill:int = 0;
        
        public var vecFill:Vector.<int>;
        public var vecCost:Vector.<int>;
        public var vecGrid:Vector.<int>;
        public var mw:int = 0;         public var mh:int = 0;
        public var cw:Number = 16;     public var ch:Number = 16;

        public function initEmpty(aw:int, ah:int):void
        { mw = aw; mh = ah; vecGrid = new Vector.<int>(aw * ah, false); 
          vecCost = new Vector.<int>(aw * ah, false);
          vecFill = new Vector.<int>(aw * ah, false);
        }//veccost

        public function setTile(ax:int, ay:int, t:int):void
        { if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecGrid[ay*mw+ax]=t; }

        public function getTile(ax:int, ay:int ):int
        { if (ax<0||ax>=mw||ay<0||ay>=mh) {return 1;}  return vecGrid[ay*mw+ax]; } 
  
        public function setFill(ax:int, ay:int, t:int):void
        { if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecFill[ay*mw+ax]=t; }

        public function getFill(ax:int, ay:int ):int
        { if (ax < 0 || ax >= mw || ay < 0 || ay >= mh) { return curFill; } 
          if (getTile(ax, ay) > 0) { return curFill; }
            return vecFill[(ay*mw)+ax]; } 
  
        public function setCost(ax:int, ay:int, t:int):void
        { if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecCost[ay*mw+ax]=t; }

        public function getCost(ax:int, ay:int ):int
        { if (ax<0||ax>=mw||ay<0||ay>=mh) {return -1;}  return vecCost[ay*mw+ax]; } 

        
         public var vecTile:Vector.<xTile>;

        public function fillTile(ax:int, ay:int):void
        {
          var it:int; var i:int;
          var a:xTile;  var b:xTile;
    
          
          if (vecTile == null) { vecTile = new Vector.<xTile>(1024, false);
            for (i = 0; i < 1024; i += 1) { vecTile[i] = new xTile(); }
          }//endif
 
          a = vecTile[0];
          a.cx = ax; a.cy = ay; a.dt = 0;
          setFill(a.cx, a.cy, curFill);
          
          it = 1;
          curFill += 1;
    
           for (i = 0; i < 1024; i+=1)
          {         
            if (i >= it) { break; }
            a = vecTile[i];
            setFill(a.cx, a.cy, curFill);
            if (getTile(a.cx, a.cy) > 0) { continue; } //hack to prevent error
          
            setCost(a.cx, a.cy, a.dt);
           
            if (getFill(a.cx - 1, a.cy) != curFill)
            {  b = vecTile[it]; it += 1; b.cx = a.cx - 1; b.cy = a.cy;   b.dt = a.dt + 1;
              setFill(a.cx - 1, a.cy, curFill); }
              
            if (getFill(a.cx + 1, a.cy) != curFill)
            {  b = vecTile[it]; it += 1; b.cx = a.cx + 1; b.cy = a.cy;   b.dt = a.dt + 1; 
            setFill(a.cx + 1, a.cy, curFill); }
            
            if (getFill(a.cx , a.cy-1) != curFill)
            {  b = vecTile[it]; it += 1; b.cx = a.cx ; b.cy = a.cy - 1;   b.dt = a.dt + 1;
            setFill(a.cx , a.cy-1, curFill); }
            
            if (getFill(a.cx , a.cy+1) != curFill)
            {  b = vecTile[it]; it += 1; b.cx = a.cx ; b.cy = a.cy + 1;   b.dt = a.dt + 1; 
            setFill(a.cx , a.cy+1, curFill);}
                    
          
          }//nexti           
        }//filltile
 

          public function findDir(ax:int, ay:int):Number
          {
            var t:int; var d:int; var ret:Number;
            t = getCost(ax, ay);
            ret = 999; 
            var i:int; var k:int;
            for (i = -1; i < 2; i += 1)
            { for (k = -1; k < 2; k += 1)
              {
                if (k == 0 && i == 0) { continue; }
                if (k !=0 && i !=0){continue;}
                if (getTile(ax + k, ay + i) > 0) { continue; }
                d = getCost(ax + k, ay + i);
                if (d >= t) { continue; }
                t = d;
                ret = Math.atan2((ay + i) - ay, (ax + k) - ax);
              } //nextk
            }//nexti
            
            return ret;
          }//finddir



       public var vecOrb:Vector.<xOrb> = new Vector.<xOrb>(0,false);
 
        public function onEnter(e:Event):void
        {
            graphics.clear();
            graphics.lineStyle(2, 0);
            
            fillTile(stage.mouseX/cw,stage.mouseY/ch);
          

           graphics.lineStyle();
          
            var ax:Number; var ay:Number;
            var i:int; var k:int; var yt:int; var t:int;
            for (i=0;i<mh;i+=1)
            {
             yt = i * mw;
             for (k=0;k<mw;k+=1)
             {   
               t = vecGrid[k + yt];  
               ax=k*cw;ay=i*ch; 
               if (t <= 0) 
               {     graphics.lineStyle();
                   t=255-(vecCost[k+yt]*8); if (t<0){t=0;}   
               graphics.beginFill(t, 1); }
               else 
              {  
                graphics.lineStyle(2, 0);
                graphics.beginFill(0xF01010, 1);  }
                
                      graphics.drawRect(ax,ay,cw,ch);           
                 graphics.endFill();  

              continue;
             }//nextk
            }//nexti  

                graphics.lineStyle(2, 0);
    
            var a:xOrb; var num:int;
            num = vecOrb.length;
            for(i=0;i<num;i+=1)
            {
                a = vecOrb[i];
                
                var ta:Number;
                ta = findDir(a.cx/cw,a.cy/ch);
                a.cx+=Math.cos(ta)*2;
                a.cy+=Math.sin(ta)*2;
                
                if (getMag(a.cx-stage.mouseX,a.cy-stage.mouseY)<32)
                { a.cx = Math.random()*mw*cw; a.cy = Math.random()*mw*ch; }
                
                graphics.beginFill(0xFfFfFf, 1);  
                  graphics.drawCircle(a.cx,a.cy, 8);
                graphics.endFill();  
            }//nexti
    

        }//onenter
        
        public function getMag(ax:Number,ay:Number):Number
        { return Math.sqrt(ax*ax+ay*ay); }
 
 
        
    }//classend
}

internal class xTile
{
  public var cx:int = 0;
  public var cy:int = 0;
  public var dt:int = 0;
  
};//xtile

internal class xOrb
{
  public var cx:Number = 0;
  public var cy:Number = 0;  
    
};//xorb