flash on 2016-12-23

by mutantleg
♥0 | Line 130 | Modified 2016-12-23 05:27:24 | 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/ag6y
 */

package {
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
            deb = new TextField();
            deb.width=320;
            deb.height=240;
            deb.mouseEnabled=false;
            addChild(deb);

            var i:int; var k:int; var num:int;
            var a:xTile; var yt:int;
            mw=13;mh=13; 
            num=mw*mh;
            vecGrid = new Vector.<xTile>(num,false);
            for(i=0;i<num;i+=1){ vecGrid[i] = new xTile(); }

            for(i=0;i<32;i+=1)
            { setTile(Math.random()*mw, Math.random()*mh, 0x808080); } 
            
            
          stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
          stage.addEventListener(MouseEvent.MOUSE_UP, onMup);  
          stage.addEventListener(Event.ENTER_FRAME, onEnter);            
        }//ctor
        
        public function onMdown(e:MouseEvent):void { mbutton = 1; }
        public function onMup(e:MouseEvent):void { mbutton = 0; }
        
        public var deb:TextField;        
        
        public var mbutton:int = 0;
        public var mx:Number = 0;
        public var my:Number = 0;
        
        public var mhold:int = 0;
        
        public var vecGrid:Vector.<xTile>// = new Vector.<int>(16*16,false);
        public var mw:int = 16;
        public var mh:int = 16;
        public var cw:Number = 32;
        public var ch:Number = 32;
        
        public function setTile(ax:int, ay:int, t:int):void
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return;} vecGrid[ax+(ay*mw)].col = t;  }
        
        public function getTile(ax:int, ay:int):xTile
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return null;} return vecGrid[ax+(ay*mw)];  }

        public function isZone(ax:int, ay:int):Boolean
        { var a:xTile; a = getTile(ax,ay); if (a==null){return false;} if(a.col!=0){return true;} return false;  }
        
        
        public var cx:int = -32;
        public var cy:int = -32;
        public var wx:int = 1;        
        public var wy:int = 0;

        public var curTest:int = 0;        
        public var mode:int = 0;
        
        public function onEnter(e:Event):void
        {
           graphics.clear();
           graphics.lineStyle(1,0);
           
           graphics.drawRect(0,0, mw*cw, mh*ch);
           
            mx = stage.mouseX;
            my = stage.mouseY;
            
            var tx:int; var ty:int;
            tx = Math.floor(mx/cw);
            ty = Math.floor(my/ch);
            
            
          var a:xTile;
          var yt:int; var t:int;
          var i:int; var k:int;
          for (i=0;i<mh;i+=1)
          {
           yt = mw * i;
           for (k=0;k<mw;k+=1)
           {
             //graphics.drawRect(k*cw, i*ch, cw, ch);
             a = getTile(k,i);
             if (a.col>0)
             {
              graphics.lineStyle();
              graphics.beginFill(a.flag == 15 ? 0x909090 : a.col,1);
               graphics.drawRect(k*cw, i*ch, cw, ch);             
              graphics.endFill();    
             }//endif
 
             graphics.lineStyle(2, 0);
           
             if (a.col > 0)
             {
              if ((a.flag & 1)==0) { graphics.drawRect(k*cw,i*ch,1,ch); }            
              if ((a.flag & 2)==0) { graphics.drawRect(k*cw+cw,i*ch,1,ch); }            
              if ((a.flag & 4)==0) { graphics.drawRect(k*cw,i*ch,cw,1); }            
              if ((a.flag & 8)==0) { graphics.drawRect(k*cw,i*ch+ch,cw,1); }            
             }//endif 
             
           }//nextk  
          }//nexti    
          
          graphics.lineStyle(4, 0xFF);
          graphics.drawRect(tx*cw,ty*ch,cw,ch);

          if (mbutton > 0) { mhold += 1; }
          else { mhold = 0; }
          
          a = getTile(tx,ty);
          if (a!=null) {  deb.text = ""+ a.flag+" "+a.col; }
          if (a != null && mhold == 1)
          { 
           mhold = 3;
           if (a.col == 0)
           {
            mode =0x808080;
            a.col = 0x808080 ;
           // cx=tx; cy=ty;
           // curTest += 1;
           }
           else
           {
             a.col=0; 
             mode =0;
           }//endif2
          }//endif
          if (a!=null && mhold>3)
          { a.col=mode; }
                  
          graphics.drawCircle(cx*cw+cw*0.5,cy*ch+ch*0.5, 8);         
          //graphics.drawCircle((cx+wy)*cw+cw*0.5,(cy-wx)*ch+ch*0.5, 4);         

         var f:int;
          for (i=0;i<mh;i+=1)
          {
           for (k=0;k<mw;k+=1)
           {
             a = getTile(k,i);

             f = 0;
             if ( isZone(k-1, i) ) { f |= 1; }
             if ( isZone(k+1, i) ) { f |= 2; }
             if ( isZone(k, i-1) ) { f |= 4; }
             if ( isZone(k, i+1) ) { f |= 8; }
             a.flag = f;
             
           }//nextk
          }//nexti

/*
          var f:int;           
           for (i=-1;i<2;i+=1)
           {
             for (k=-1;k<2;k+=1)
             {
               if (i==0&&k==0){continue;}
               if (i!=0&&k!=0){continue;}
               a = getTile(k+cx, i+cy);
               
               f = 0;
               if ( isZone(k+cx-1, i+cy) ) { f |= 1; }
               if ( isZone(k+cx+1, i+cy) ) { f |= 2; }
               if ( isZone(k+cx, i+cy-1) ) { f |= 4; }
               if ( isZone(k+cx, i+cy+1) ) { f |= 8; }
               if (f == 15) { continue; }
               
               if (a.col != 0 && a.test != curTest)
               { a.test = curTest;  cx+=k; cy+=i;  i=3;k=3;  }    
             }   
           }
          */
          /*
          var temp:int;            
          a = getTile(cx+wx, cy+wy);
          if (a == null || a.col == 0)
          { temp = wx; wx = -wy; wy = temp;  }            
          else
          {
           a = getTile(cx+wy, cy-wx);
           if (a == null || a.col != 0)
           {temp = wx; wx = -wy; wy = temp; }          
           else             
           { cx += wx;  cy += wy; }              
          }//endif
           */
                      
        }//onenter


        
        
    }//classend
}

internal class xTile 
{
 public var flag:int = 0;
 public var col:int = 0; 
 public var test:int = 0;
}//xtile