flash on 2016-12-25

by mutantleg
♥0 | Line 152 | Modified 2016-12-25 10:34:20 | 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/GTWY
 */

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, 0x303030); } 
            
            var m:xAct
            vecAct = new Vector.<xAct>(0, false);
            for (i=0;i<3;i+=1)
            {
             m = new xAct();
              m.tx = Math.random()*mw;
              m.ty = Math.random()*mh;    
              m.col = Math.random()*0xFFffFFff;
             vecAct.push(m); 
            }//nexti
            
            
          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 isWall(ax:int, ay:int):Boolean
        { var a:xTile; a = getTile(ax,ay);if(a==null){return true;} return (a.col ==0x303030); } 

        
        public var mode:int = 0;


        public var vecAct:Vector.<xAct>;
      
        public function onEnter(e:Event):void
        {
           graphics.clear();
           graphics.lineStyle(1,0);
           
           graphics.beginFill(0x808080,1);           
           graphics.drawRect(0,0, mw*cw, mh*ch);
           graphics.endFill();
           
            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.col,1);
               graphics.drawRect(k*cw, i*ch, cw, ch);             
              graphics.endFill();    
             }//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 && mhold == 1)
          { 
           mhold = 3;
           if (a.col == 0)
           {
            mode =0x303030;
            a.col = mode ;
           }
           else
           {
             a.col=0; 
             mode =0;
           }//endif2
          }//endif
          
          
          var num:int;
          var m:xAct;
          num = vecAct.length;
          for(i=0;i<num;i+=1)
          {
            m = vecAct[i];
            graphics.beginFill(m.col, 1);
            graphics.drawCircle(m.tx*cw+cw*0.5, m.ty*ch+ch*0.5, 8);  
            graphics.endFill();
            
            
            if (m.wait>0){m.wait-=1; continue; }
            
            var tmp:int;
            if (isWall(m.tx+m.wx, m.ty+m.wy)) 
            { tmp = m.wx; m.wx = m.wy; m.wy = -tmp; continue; }
            
            a = getTile(m.tx, m.ty);
            if (a != null) {a.col = m.col; }

            m.tx += m.wx; m.ty+=m.wy;            
            m.wait=3;

            a = getTile(m.tx+-m.wy, m.ty+m.wx);
            if (a != null && a.col != 0x303030 && a.col != m.col)
            { tmp = m.wx; m.wx = -m.wy; m.wy = tmp; continue; }
            else
            {
             a = getTile(m.tx+m.wy, m.ty+-m.wx);
             if (a != null && a.col != 0x303030 && a.col != m.col)
             { tmp = m.wx; m.wx = m.wy; m.wy = -tmp; continue; }
             else 
             {
              if (Math.random()<0.2)
              { tmp = m.wx; m.wx = -m.wy; m.wy = tmp; continue; }              
             }//endif2
            }//endif
         
            
            /*
            a = getTile(m.tx, m.ty);
            if (a == null) { continue; }
            if (a.col != m.col) { m.wait = 6; a.col = m.col; continue; }
            
            var dx:int; var dy:int;
            var d:int;
            dx=0;dy=0;
            d = Math.random()*5;
            if (d==0){dx=1;}
            else if (d==1){dx=-1;}
            else if (d==2){dy=1;}
            else if (d==3){dy=-1;}
            else
            {
              a = getTile(m.tx-1,m.ty); if (a!=null && a.col !=m.col){ dx=-1; } 
              else
              {                
               a = getTile(m.tx+1,m.ty); if (a!=null && a.col !=m.col){ dx=1; }  
               else
               {
               a = getTile(m.tx,m.ty-1); if (a!=null && a.col !=m.col){ dy=-1; }                 
               else
               {  a = getTile(m.tx,m.ty+1); if (a!=null && a.col !=m.col){ dy=1; } }
               }  
              }
                            
            }//endif
            
            if (isWall(m.tx+dx,m.ty+dy)==false)
            { m.tx+=dx; m.ty+=dy; m.wait = 3; }
            */
            
          }//nexti
          
          
          
        }//onenter


        
        
    }//classend
}

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

internal class xAct
{
  public var tx:int = 0;
  public var ty:int = 0;
  public var col:int  = 0;  
  public var wait:int = 0;
  public var wx:int = 1;
  public var wy:int = 0;
}//xact