flash on 2017-2-25

by mutantleg
♥0 | Line 136 | Modified 2017-02-25 07:10:01 | 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/8d7G
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
          vecZone = new Vector.<xZone>(0,false);
          var v:xZone;
          var i:int; var k:int;
          for (i=0;i<4;i+=1)
          for (k=0;k<4;k+=1)
          {
            v = new xZone();
            v.cx = k*90;
            v.cy = i*90;
            v.cw=90;
            v.ch =90;
            vecZone.push(v);  
          }//nextki 
          
          vecAct = new Vector.<xAct>(0,false);
          
          /*
          var a:xAct;
          a = new xAct();
           a.cx = 230;
           a.cy = 230; 
           a.team = 0xFF0000;
          vecAct.push(a); 
          */
          
           for (i=0;i<32;i+=1)
           {
             a  = new xAct();
             a.cx = Math.random()*256+32;
             a.cy = Math.random()*256+32;
             a.gx =a.cx; a.gy=a.cy;
             a.team = Math.random()<0.5? 0xFF:0xFF0000;
             vecAct.push(a);  
               
           }//nexti
          
            
          stage.addEventListener(Event.ENTER_FRAME, onEnter);      
        }//ctor
        
        
        public function onEnter(e:Event):void
        {
          graphics.clear()
          graphics.lineStyle(2, 0);
          
          graphics.drawRect(0,0,465,465);
            
          var i:int; var num:int; var v:xZone;
          num =  vecZone.length;
          for (i=0;i<num;i+=1)
          { v = vecZone[i];
          
            graphics.beginFill(v.team, 0.2);
            graphics.drawRect(v.cx, v.cy,v.cw,v.ch);
            graphics.endFill();
          }//nexti  
          
          var a:xAct;
          num = vecAct.length;
          for(i=0;i<num;i+=1)
          {
           a = vecAct[i];
          // a.gx = stage.mouseX;
          // a.gy = stage.mouseY;
           a.update();
           graphics.beginFill(a.team, 1);
           graphics.drawCircle(a.cx,a.cy, 8);   
           graphics.endFill();
           
           /*
           if (a.zone != null)
           {
             graphics.moveTo(a.cx,a.cy);
             graphics.lineTo(a.zone.cx+a.zone.cw*0.5, a.zone.cy+a.zone.ch*0.5);   
           }
           */
          }//nexti  
            
        }//onenter
        
    }//classend
}
        
 var vecZone:Vector.<xZone>;
 var vecAct:Vector.<xAct>;

function getZone(ax:Number, ay:Number):xZone
{
  var i:int; var num:int; var v:xZone;
  num = vecZone.length;
  for (i=0;i<num;i+=1)
  {
   v = vecZone[i];
   if (ax < v.cx) { continue; }
   if (ax > v.cx+v.cw){ continue; }   
   if (ay < v.cy) { continue; }
   if (ay > v.cy+v.ch){ continue; }   
    return v;  
  }//nexti  
   return null; 
}//getzone

function getMag(ax:Number, ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }

internal class xAct
{
 public var cx:Number = 0;
 public var cy:Number = 0;
 public var gx:Number = 0;
 public var gy:Number = 0;
 public var team:int = 0;
 public var zone:xZone = null;  

 public function update():void
 {
     
  var vx:Number; var vy:Number;
  var ta:Number; var d:Number;
  var ms:Number;
  
  ta = Math.atan2(gy-cy, gx-cx);
  d = getMag(gx-cx, gy-cy);
  ms = 2; if (ms>d){ms=d;}
  vx = Math.cos(ta)*ms;
  vy = Math.sin(ta)*ms;
  
  cx+=vx;
  cy+=vy;
  
  if (zone != null)
  { if (cx<zone.cx||cx>=zone.cx+zone.cw||cy<zone.cy||cy>=zone.cy+zone.ch){zone=null;}  }
  if (zone == null) { zone = getZone(cx,cy);  }
  if (zone != null)
  {
      /*
    if (zone.team!=team) { zone.proc += 1; }
    if (zone.proc >= 10)
    {  zone.team = team;  zone.proc = 0;}  
       */
  }//endif
     
 }//update
 
}//xact

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 team:int = 0;
  public var proc:int = 0;
  
  public var vecAct:Vector.<xAct> = new Vector.<xAct>(0,false);
  public function addAct(m:xAct):void
  {
    if (m.zone == this){ return;}   
    if (m.zone != null) { m.zone.remAct(m); }
    vecAct.push(m);
    m.zone = this;
  }//addact
  public function remAct(m:xAct):void
  {
    if (m.zone != this){return;}
    var i:int; var num:int; var a:xAct;
    num = vecAct.length;
    for (i=0;i<num;i+=1)
    {
      a = vecAct[i];
      if (a != m) { continue; }
       vecAct[i] = vecAct[num-1]; 
       vecAct.pop();
       m.zone = null;
       return;        
    }//nexti       
  }//remact
}//xzone