flash on 2015-1-26

by mutantleg
♥0 | Line 103 | Modified 2015-01-26 07:49:39 | 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/btC2
 */

package {
    import flash.text.TextField;
   import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.display.BitmapData;
    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;
            deb.textColor = 0xFFffFF;
            addChild(deb);
        
            map = new BitmapData(32,32, false, 0);
            mat = new Matrix(cw,0,0,cw);
        
            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 { mdown = 1; }
        public function onMup(e:MouseEvent):void { mdown = 0; }
        
        public var deb:TextField;
        
        public var mdown:int = 0;
        public var cw:Number = 32;
        public var map:BitmapData;
        public var mat:Matrix;
        
        public var mode:int = 0;
        public var wallCol:uint = 0x808080;
        
        public var gold:int = 30; //15;
        
        public function isWall(ax:Number, ay:Number):Boolean
        {
           var tx:int; var ty:int;
           tx = Math.floor(ax/cw);            ty = Math.floor(ay/cw);                      
           return map.getPixel(tx, ty) == 0x808080;
        }//iswall

        public function getWall(ax:Number, ay:Number):uint
        {
           var tx:int; var ty:int;
           tx = Math.floor(ax/cw);            ty = Math.floor(ay/cw);                      
           return map.getPixel(tx, ty);
        }//iswall
        

        public function setWall(ax:Number, ay:Number, w:uint):void
        {
           var tx:int; var ty:int;
           tx = Math.floor(ax/cw);            ty = Math.floor(ay/cw);                      
           map.setPixel(tx,ty, w);    
        }//setwall
        
        public var gt:int = 0;
        
        public function onEnter(e:Event):void
        {
            graphics.clear();
            graphics.lineStyle(2, 0);
            
            var tx:int; var ty:int;
            var ax:Number; var ay:Number;
            ax = stage.mouseX; ay = stage.mouseY;
            tx = Math.floor(ax/cw);            ty = Math.floor(ay/cw);            
            ax = tx*cw;            ay = ty*cw;
            
            if (mdown > 0 && mode == 0) { mode =1;  if (isWall(ax,ay)) { mode = 2;}   }
            else if (mdown <= 0) { mode = 0; }
            
            if (mode == 1) 
            if (gold > 0)
            if (isWall(ax,ay) == false)
            { setWall(ax,ay, wallCol); gold -= 1; }
            
            if (mode == 2) 
            if (isWall(ax,ay) == true)
            { setWall(ax,ay, 0); gold += 1; }
          
          
            if (mode != 2)
            if (gt % 5 == 0)
             {  updateMap(); }         
            
            gt += 1;
       


            graphics.beginBitmapFill(map, mat, false, false);
            graphics.drawRect(0,0,465,465);
            graphics.endFill(); 
            

            graphics.lineStyle(2, 0x00FF00);
            graphics.drawRect(ax,ay,cw,cw);
            
            deb.text = ""+gold;
            
        }//onenter
        
        public function updateMap():void
        {
            var wx:Number; var wy:Number;  var f:int;           
            var i:int; var k:int;                      
            for (i = 0; i < 14; i++)
            {
                wy = i * cw;
                for (k = 0; k < 14; k++)
                {
                    wx = k * cw;
                    if (isWall(wx,wy)) { continue; }
                    f = 0;
                    
                    if (isWall(wx+cw,wy)) { f|=1; }
                    if (isWall(wx-cw,wy)) { f|=2; }
                    if (isWall(wx,wy+cw)) { f|=4; }
                    if (isWall(wx,wy-cw)) { f|=8; }
                    
                    if (f == 15) { setWall(wx,wy,0xFF); continue; }
                    
                    if ((f & 3) == 0) { setWall(wx,wy,0); continue; }
                    if ((f & 12) == 0) { setWall(wx,wy,0);  continue; }
     
                    setWall(wx,wy,0xFF0000);               
                    
                }//nextk
            }//nexti
        }//updatemap
        
        
    }//classend
}