flash on 2012-5-16

by mutantleg
♥0 | Line 274 | Modified 2012-05-16 18:42: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/PtHc
 */

package {
    import flash.text.TextField;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public var mw:int = 32;
        public var mh:int = 32;
        
        public var canvas:BitmapData;
        
        public var vecAct:Vector.<aActor>;
        
        public var map:actMap;
        public var world:aWorld;
        
        public var debText:TextField;
        
        public function FlashTest() {
            // write as3 code here..
            
             debText = new TextField();  tDebug.td = debText;   
                 debText.text = "debug";
            debText.textColor = 0xFFffFFff;
            debText.scaleX = debText.scaleY = 2;
            
            var i:int;
            var num:int;
            var a:aActor;
            var b:aActor;
            num = mw * mh;
            vecAct = new Vector.<aActor>(num,true);
            
            a = new aActor();
            a.flag = 0;
            a.type = 0;
            a.color = Math.random() * 0xFFffFFff;
            
            b = new aActor();
            b.flag = 0;
            b.type = 0;
            b.color = Math.random() * 0xFFffFFff;
            
            for (i = 0; i < num; i++)
            {
                if (Math.random() > 0.5)  { vecAct[i] = a; }
                else if (Math.random() > 0.5) { vecAct[i] =b;}
                
            }//nexti
            
            canvas = new BitmapData(450,450,false);
            canvas.fillRect(canvas.rect, 0x0000ff);
            
            var bm:Bitmap;
            bm = new Bitmap(canvas);
            bm.bitmapData = canvas;
            addChild(bm);
            
            world = new aWorld();
            map = new actMap();
            map.mw = mw;
            map.mh = mh;
            map.vecAct = vecAct;
            
            playerBm = getPlayerBm();
            
            player = new aPlayer();
            player.cx = 4;
            player.cy = 4;
            actMap.gMap.remActor(4,4);
           
            
            for (i = 0; i < mw; i++)
                        actMap.gMap.remActor(i,5);
                        
                  player.moveActor(4,5);
                  
           //  vecAct[ 4 + (5*mh)] = player;     
             
       
            addChild(debText);
           
            
            addEventListener(Event.ENTER_FRAME, update);
        }//ctor
        
        public var playerBm:BitmapData;
        public var player:aPlayer;
        
        public function getPlayerBm():BitmapData
        {
            var bm:BitmapData;
            var tmpr:Rectangle;
            tmpr = new Rectangle();
            bm = new BitmapData(16,16,false,0);
            
            //body
            bm.fillRect(bm.rect, 0xFFFF00);
            
            //mouth
            tmpr.x = 2;
            tmpr.width = 16-4;
            tmpr.height = 2;
            tmpr.y = 12;
            bm.fillRect(tmpr, 0);
            
            //eyes
            tmpr.width = 2;
            tmpr.height = 6;
            tmpr.y = 4;
            tmpr.x = 2;
            bm.fillRect(tmpr, 0);
            
            tmpr.x = 2+10;
            bm.fillRect(tmpr,0);
            
            return bm;
        }//playerbm
        
        public function update(e:Event):void
        {
            canvas.fillRect(canvas.rect,0);
            drawGrid(canvas,-10+(stage.mouseX) /16,-10+(stage.mouseY)/16);
        }//update
        
        public static var drect:Rectangle = new Rectangle();
        public static var dpoint:Point = new Point();
        public function drawGrid(to:BitmapData, sx_:int, sy_:int):void
        {
            var i:int;
            var k:int;
            var yt:int;
            var sx:int, sy:int;
            var ex:int, ey:int;
            var a:aActor;
            var dx:int;
            var dy:int;
            var tx:int, ty:int;
            var ei:int, ek:int;
            
            drect.x = 0;
            drect.y = 0;
            drect.width = 16;
            drect.height = 16;
           
            dx = 0;
            dy = 0;
            sx = sx_; sy = sy_;
           
            
           ei = to.height/ 16;
           ek = to.width / 16;
            
             ex = sx + 15; // (to.width/16); //sx+15;
            ey = sy + 15; // (to.height/16); //sy+15;
 /*           
             if (sx > mw) { return; } if (sy > mh) { return; }
            if (sx < 0) {  sx = 0;} if (sy < 0) { sy = 0;} 
            if (ex < 0) { return; } if (ey < 0) { return; }
            if (ex > mw) { ex = mw;} if (ey > mh) { ey = mh;}
  */          
            tx = sx;
            ty = sy;
            for (i = 0; i < ei; i++,ty++)
            {
                if (ty < 0) { continue; }
                else if (ty >= mh) { return;}
                yt = ty * mw;
                tx = sx;
               // dx = 0;
                //drect.y = dy +64;
                drect.y = i * 16;
                dpoint.y = i * 16;
                
                for (k = 0; k < ek; k++, tx++)
                {
                    if (tx < 0) { continue; }
                    else if (tx >= mw) { break; }
                    
                   a = vecAct[yt+tx];
                   
                   if (a == null) {  continue; }
                   
                   drect.x = k * 16;
                   dpoint.x = k * 16;
                   
                   //drect.x = dx+64;
                   if (a.type == 0)
                   {
                    to.fillRect(drect, a.color);
                   }
                   else
                   {
                       tDebug.td.text=  "Type "+a.type;
                       to.fillRect(drect, a.color);
                       to.copyPixels(playerBm, playerBm.rect, dpoint);
                       /*
                       switch (a.type)
                       {
                           case 2:
                             to.copyPixels(playerBm, playerBm.rect, dpoint);
                             tDebug.td.text= "drawn";
                           break;
                           
                        }//swend
                       */
                   }//endif
                   
                  // dx += 16;
                }//nextk
              //  dy += 16;

            }//nexti
        }//drawgrid
        
    }//classend
}
import flash.text.TextField;

internal class tDebug
{
    public static var td:TextField;
}//debug

internal class actMap
{
    public static var gMap:actMap;     
    
     public var mw:int = 32;
     public var mh:int = 32;
    
     public var vecAct:Vector.<aActor>;
    
    public function actMap()
    {
        gMap = this;
    }//ctor
    
    public function remActor(ax:int, ay:int):void
    {
       if (ax < 0) { return ; }    if (ax >= mw) { return ; }
       if (ay < 0) { return ; }    if (ay >= mh) { return ; }
       
       vecAct[ax + (ay*mw)] = null;
    }//remactor
    
    public function setActor(ax:int, ay:int, a:aActor):void
    {
       if (ax < 0) { return ; }    if (ax >= mw) { return ; }
       if (ay < 0) { return ; }    if (ay >= mh) { return ; }
       //tDebug.td.text = "set";
        vecAct[ax +(ay*mw)] = a;
    }//setact
    
    public function getActor(ax:int, ay:int):aActor
    {
       if (ax < 0) { return null; }    if (ax >= mw) { return null; }
       if (ay < 0) { return null; }    if (ay >= mh) { return null; }
       
       //return null;
       return vecAct[ ax + (ay * mw) ];  
    }//getactor
        
}//map

internal class aWorld
{
    public var vecAct:Vector.<aActor>;
    
    public function aWorld()
    {
        vecAct = new Vector.<aActor>;
    }//aworld
    
    public function addActor(a:aActor):aActor
    {
        if (a == null) { return null; }
        vecAct.push(a);
        return a;
    }//adda
    
    public function update():void
    {
        var bHasDead:Boolean = false;
        var i:int;
        var num:int;
        var a:aActor;
        
        num = vecAct.length;
        
        for (i = 0; i < num; i++)
        {
            a = vecAct[i];
            if (a.bDead) { bHasDead = true; continue;}
            
            a.update();
            
        }//nexti
        
        if (bHasDead) { remDead(); }
    }//update
    
    public function remDead():void
    {
        var i:int;
        var num:int;
        var a:aActor;
        
        num = vecAct.length;
        
        for (i = 0; i < num; i++)
        {
            a = vecAct[i];
            if ( !(a.bDead) ) { continue;}
            
            a.remove(); //dtor thing
            vecAct[i] = vecAct[num - 1]; //copy last to this one
            num = vecAct.length; //vector is now shorter
            vecAct.pop(); //remove last
            i -= 1; //go back one iteration
            
        }//nexti
    }//remdead
    
}//world

internal class aActor
{
    public var color:int = 0;
    public var flag:int = 0;
    public var team:int = 0;
    public var spec:int = 0;
    public var type:int = 0;
    public var bDead:Boolean = false;
  
    public function aActor()
    {   }//ctor

    public function update():void
    {} //update
    
    public function remove():void //called before removing from world 
    {} //rem
}//actor

internal class aAdvActor extends aActor
{
    public var cx:int = 0;
    public var cy:int = 0; 
    public var hp:int = 0;
       
    public function aAdvActor()
    {
    }//ctor
    
    override public function remove():void
    {
        actMap.gMap.remActor(cx,cy);
    }//remove
    
    public function moveActor(gx:int, gy:int):void
    {
        var a:aActor;
        a = actMap.gMap.getActor(gx, gy);
        if (a != null) { return; }
        actMap.gMap.remActor(cx,cy);
        actMap.gMap.setActor(gx,gy,this);
        cx = gx; cy = gy;
    }//moveactor
    
    
}//advactor

internal class aPlayer extends aAdvActor
{
    public function aPlayer()
    {
        color = 0xFFFFFF;
        type = 2;
        tDebug.td.text=  "playertype "+type;
    }//ctor
    
}//aplayer