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

flash on 2013-6-17

draw more rooms with the mouse
Get Adobe Flash player
by mutantleg 17 Jun 2013

    Talk

    Glidias at 26 Jun 2013 09:06
    This is useful for converting procedural generated tile-based rogue-likes into 3d stuff which uses polygons. The lines would be good for pathfinding as well.
    mutantleg at 26 Jun 2013 11:55
    Indeed, i made it for path finding originally: http://wonderfl.net/c/6H7p

    Tags

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

package {
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        
        //edit -- added fix for corners
        
        public function FlashTest() {
            
            mwidth = (465/8)- 1; // 64;
            mheight =(465/8)- 1; // 64;
            var num:int;
            num = mwidth * mheight;
            vecGrid = new Vector.<int>(num, false);
            
            var i:int;
            
            for (i = 0; i < 256; i++)
            {
               // setRect(vecGrid,mwidth, mheight,
               // Math.random()*mwidth, Math.random()*mheight, Math.random()*16, Math.random()*16,1);
                
                if (Math.random() < 0.5)
                {
                setRect(vecGrid,mwidth, mheight,
                Math.random()*mwidth, Math.random()*mheight, Math.random()*16, Math.random()*4,1);
                    
                }
                else
                {
                setRect(vecGrid,mwidth, mheight,
                Math.random()*mwidth, Math.random()*mheight, Math.random()*4, Math.random()*16,1);
                    
                }
                
            }//nexti
            
            
            //setRect(vecGrid, mwidth, mheight, 4,4, 16,16, 1);
            
            //well its zone generation thats important
            //and all this debug drawing slows things down
            stage.quality = "LOW";
            
            mapChange = 1;
            
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
            stage.addEventListener(MouseEvent.MOUSE_UP, mup);
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public var bMdown:Boolean = false;
        
        public function mdown(e:MouseEvent):void { bMdown = true; }
        public function mup(e:MouseEvent):void { bMdown = false; }
        
        public var cw:Number = 8;
        public var ch:Number = 8;
        public var mwidth:int = 0;
        public var mheight:int = 0;
        public var vecGrid:Vector.<int>;
        
        public var vecZone:Vector.<xZone> = new Vector.<xZone>;

        public var mapChange:int = 0;
        public var mwait:int = 0;
        
        public function buildZone():void
        {
            var a:xZone;
            var i:int;
            var k:int;
            var yt:int;
            var t:int;
            var vec:Vector.<int>;
            
            vec = copyVec(vecGrid);
            
            vecZone = new Vector.<xZone>(0, false);
            
            var n:int;
            var h:int;
            var m:int;
            
            for (i = 0; i < mheight; i++)
            {
                yt = i * mwidth;
                for (k = 0; k < mwidth; k++)
                {
                    t = vec[yt+k];
            
                    if (t <= 0) {  continue; }
                    
                    
                    n = getClosex(vec, k, i);
                    
                    for (h = i; h < mheight; h++)
                    {
                     m = getClosex(vec, k, h);
                     if (m < n) {  break; }
                    }//nexth
                    
                    a = new xZone();
                    a.cx = k;
                    a.cw = n - k;
                    a.cy = i;
                    a.ch = h - i;
                    a.group = 0; 
                    a.col = 0xff; // Math.random() * 0xFFffFF;
                    vecZone.push(a);
                    
                    setRect(vec, mwidth, mheight, a.cx,a.cy,a.cw,a.ch, 0);                    
                    vec[yt+k] = 1;
       
                    k = n;
                       
                    //debug
                    //if (vecZone.length > 50) { vecGrid = vec; return; }
                }//nextk
                
            }//nexti
            
            //debug
            //vecGrid = vec;
            
            
            //build connections
            
            var num:int;
            var b:xZone;
            
            
            num = vecZone.length;
            for (i = 0; i < num; i++)
            {
                a = vecZone[i];
                
                for (k = 0; k < num; k++)
                {
                    if (i == k) { continue; }
                    
                    b = vecZone[k];
                    
                    if (a.cx + a.cw < b.cx) { continue; } 
                    if (a.cy + a.ch < b.cy) { continue; }
                    if (b.cx + b.cw < a.cx) { continue; } 
                    if (b.cy + b.ch < a.cy) { continue; }
                    
                    //new -- corner fix
                     if ((a.cx + a.cw) == b.cx && (a.cy+a.ch) == b.cy) { continue; }
                     if ((b.cx + b.cw) == a.cx && (b.cy+b.ch) == a.cy) { continue; }
                     if ((a.cx + a.cw) == b.cx && (b.cy+b.ch) == a.cy) { continue; }
                     if ((b.cx + b.cw) == a.cx && (a.cy+a.ch) == b.cy) { continue; }
                     
              
                    a.vecNext.push(b);
   
                }//nextk                
            }//nexti

            
            genGroup();
            

        }//buildzone
        
        
        
         public function genGroup():void
        {
            var temp:Vector.<xZone>;
            var ti:int;
            var vec:Vector.<xZone>;
            var numk:int;
            var num:int;
            var i:int;
            var k:int;
            var a:xZone;
            var b:xZone;
            var group:int;
            var debcol:uint;
         
            
            group = 1; //dont start at 0 or its infinite loop
            
            num = vecZone.length;
            
            temp = new Vector.<xZone>(num, false);
            
            for (i = 0; i < num; i++)
            {
                a = vecZone[i];
            
                if (a.group != 0) { continue; }
                //debcol = Math.random() * 0xffFFff; 
                debcol = 0x888888 + (Math.random()*0x777777);
                group += 10;
                ti = 0;
                
                temp[0] = a;
                ti = 1;
                
                while (ti > 0)
                {
                    a = temp[ti - 1];
                    ti -= 1;
                    
                    if (a.group != 0) { continue;}
                    //if (a.group == group) { continue; }
                    a.group = group;
                    a.col = debcol; // group * 0xFF0a; //debcol; 
                                      
                    vec = a.vecNext;
                    numk = vec.length;
                    
                    for (k = 0; k < numk; k++)
                    {
                        b = vec[k];
                        temp[ti] = b;
                        ti += 1;                    
                    }//nextk
                
                }//wend
                
            
            }//nexti    
            
        }//gengroup

        
        
        
        
        
        
        
        
        public function getClosex(vec:Vector.<int>, ax:int, ay:int):int
        {
            var k:int;
            var yt:int;
            var t:int;
            
            yt = ay * mwidth;
            
            for (k = ax; k < mwidth; k++)
            {
                t = vec[yt + k];
                if (t <= 0) { return k;}
            }//nextk
            
            return mwidth;
        }//getclose
        
        
        
        public function copyVec(vec:Vector.<int>):Vector.<int>
        {
            var i:int;
            var num:int;
            var ret:Vector.<int>;
                      
            num = vec.length;
            ret = new Vector.<int>(num, false);
            for (i = 0; i < num; i++)
            { ret[i] = vec[i]; }
            
            return ret;
        }//copyvec
        
        public function setRect(vec:Vector.<int>,mw:int,mh:int,  ax:int, ay:int, aw:int, ah:int, c:int):void
        {
            var ex:int;
            var ey:int;
            var i:int;
            var k:int;
            var yt:int;
            
            ex = ax + aw;
            ey = ay + ah;
            
            if (ax < 0) { ax = 0;  }
            if (ay < 0) { ay = 0;  }
            if (ex > mw) { ex = mw;   }
            if (ey > mh) { ey = mh;    }
            if (ax >= mw) { return;  }
            if (ay >= mh){ return;  }
            if (ex < 0) { return;  }
            if (ey < 0) { return;  }
            
            for (i = ay; i < ey; i++)
            {
                yt = i * mw;
                for (k = ax; k < ex; k++)
                {
                    vec[yt+k] = c;
                }//nextk
 
            }//exti
            
        }//setrect


        public function setMap(ax:int, ay:int, c:int):void
        {
            if (ax < 0) { return; }
            if (ay < 0) { return;} 
            if (ax >= mwidth) { return;}
            if (ay >= mheight) { return; }
            
            vecGrid[ay*mwidth+ax] = c;
        }//setmap
        
        
        public function onEnter(e:Event):void
        {
            
            if (bMdown)
            {
               setMap(stage.mouseX/cw,stage.mouseY/ch, 1);
                 setMap(stage.mouseX/cw+1,stage.mouseY/ch, 1);
                   setMap(stage.mouseX/cw,stage.mouseY/ch+1, 1);
                     setMap(stage.mouseX/cw+1,stage.mouseY/ch+1, 1);
                mwait = 0;
                mapChange = 1;
            }//endif

                if (mapChange > 0)
                {
                  mwait += 1;
                  if (mwait >= 5) { mapChange = 0; buildZone();}   
                }

            
           // if (mapChange != 2) { return;}
           // mapChange = 0;
            
            graphics.clear();
            
            var i:int;
            var k:int;
            var yt:int;
            var t:int;
            
            for (i = 0; i < mheight; i++)
            {
                yt = i * mwidth;
                for (k = 0; k < mwidth; k++)
                {
                    t = vecGrid[yt+k];
                   
                   if (t <= 0) { continue; }                            
                   
                   graphics.beginFill(0, 1);
                     graphics.drawRect(k*cw,i*ch,cw,ch);
                   graphics.endFill();
                }//nextk
                
            }//nexti

            graphics.lineStyle(1, 0);
            graphics.drawRect(0,0,mwidth*cw,mheight*ch);


        
            var a:xZone;
            var num:int;
            
            var vz:Vector.<xZone>;
            var b:xZone;
            var numk:int;

            num = vecZone.length;

            graphics.lineStyle(1, 0, 1);
            
            //draw zones
            for (i = 0; i < num; i++)
            {
               a = vecZone[i];
               graphics.beginFill(a.col, 0.55);
               graphics.drawRect(a.cx*cw,a.cy*ch,a.cw*cw,a.ch*ch); 
               graphics.endFill();         
               graphics.drawCircle((a.cx+a.cw*0.5)*cw,(a.cy+a.ch*0.5)*ch,4); 
        
            }//nexti            
             
             //draw connections
            for (i = 0; i < num; i++)
            {
               a = vecZone[i];
               
                vz = a.vecNext;
               numk = vz.length;
               for (k = 0; k < numk; k++)
               {
                 b = vz[k];
                 graphics.moveTo((a.cx+a.cw*0.5)*cw,(a.cy+a.ch*0.5)*ch);
                 graphics.lineTo((b.cx+b.cw*0.5)*cw,(b.cy+b.ch*0.5)*ch);
                 
               }//nextk 
            }//nexti
             
        }//onenter
        
        
    }//classend
}

internal class xZone
{
    public var cx:Number = 0;
    public var cy:Number = 0;
    public var cw:Number = 0;
    public var ch:Number = 0;
    
    public var group:int = 0;
    public var col:uint = 0;

    public var vecNext:Vector.<xZone> = new Vector.<xZone>;
    
}//xzone