/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gSyg
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecAct = new Vector.<xAct>(0, false);
gridAct = new Vector.<xAct>(mw*mh, false);
vecTeam = new Vector.<uint>(0, false);
var i:int;
for(i=0;i<32;i+=1)
{ vecTeam.push( ((int(255*Math.random())&0xFF )<<16)|((int(255*Math.random())&0xFF )<<8)|((int(255*Math.random())&0xFF )) ); }
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var mw:int = 32;
public var mh:int = 32;
public var cw:Number = 32;
public var ch:Number = 32;
public var vecAct:Vector.<xAct>;
public var gridAct:Vector.<xAct>;
public var vecTeam:Vector.<uint>;
public function setAct(ax:Number,ay:Number, a:xAct):void
{var tx:int; var ty:int; tx=ax/cw; ty=ay/ch; if (tx<0||tx>=mw||ty<0||ty>=mh){return;} gridAct[tx+(ty*mw)]=a; }
public function getAct(ax:Number,ay:Number):xAct
{var tx:int; var ty:int; tx=ax/cw; ty=ay/ch; if (tx<0||tx>=mw||ty<0||ty>=mh){return null;} return gridAct[tx+(ty*mw)]; }
public function getAct2(tx:int,ty:int):xAct
{ if (tx<0||tx>=mw||ty<0||ty>=mh){return null;} return gridAct[tx+(ty*mw)]; }
public function getTarg(m:xAct, r:Number):xAct
{
var ax:int; var ay:int; var aw:int;
var d:Number; var dist:Number; var ret:xAct;
var i:int; var k:int; var a:xAct;
ax= m.cx/cw; ay = m.cy/ch; aw = r / cw;
dist = r*2; ret =null;
for (i=-aw;i<=aw;i+=1)
{
for (k=-aw;k<=aw;k+=1)
{
a =getAct2(k+ax,i+ay);
// graphics.drawRect((k+ax)*cw,(i+ay)*ch,cw,ch);
if (a==null){continue;}
if (a==m) {continue;}
if (a.team==m.team) {continue;}
d = getMag(m.cx-a.cx, m.cy-a.cy);
if (d>dist){ continue; }
dist = d; ret = a;
}//nextk
}//nexti
return ret;
}//gettarg
public function getMag(ax:Number,ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
public var gt:int = 0;
public var curId:int = 1;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
if (vecAct.length < 64)
{
a = new xAct();
a.cx = Math.random()*512;
a.cy = Math.random()*512;
a.team = Math.random()*4;
a.id = curId; curId+=1;
a.col =vecTeam[a.team];
vecAct.push(a);
}
var k:int;
for(i=0;i<mh;i+=1)
{ for(k=0;k<mw;k+=1)
{
if (getAct(k*cw+16,i*ch+16)!=null)
{ graphics.beginFill(0xff,0.5);
graphics.drawRect(k*cw,i*ch,cw,ch);
graphics.endFill();
}
}//nextk
}//nexti
var d:Number; var ta:Number;
var i:int; var num:int; var a:xAct;
num = vecAct.length;
for(i=0;i<num;i+=1)
{
a = vecAct[i];
if (a.hp<=0){continue; }
setAct(a.cx,a.cy, null);
graphics.beginFill(a.col,1);
graphics.drawCircle(a.cx,a.cy,8);
graphics.endFill();
if (a.targ!=null) { if (a.targ.hp<=0){a.targ=null;} }
if (a.targ !=null)
{ d = getMag(a.cx-a.targ.cx, a.cy-a.targ.cy);
d -= 8; d -= 8;
if (d > 8) {
ta = Math.atan2(a.targ.cy-a.cy,a.targ.cx-a.cx);
a.cx += Math.cos(ta)*3;
a.cy += Math.sin(ta)*3;
} else if (a.reload < gt)
{
a.reload = gt+16;
a.targ.hp-=3;
if (a.targ.targ == null){ a.targ.targ = a; }
}
}//endif
setAct(a.cx,a.cy, a);
if (a.targ!=null)
{ graphics.moveTo(a.cx,a.cy); graphics.lineTo(a.targ.cx,a.targ.cy); }
if ( (gt+a.id)%30 ==0){
// graphics.drawCircle(a.cx,a.cy,4);
a.targ = getTarg(a, 128);
}
}//nexti
num = vecAct.length;
for(i=0;i<num;i+=1)
{
a = vecAct[i];
if (a.hp>0){continue; }
setAct(a.cx,a.cy, null);
num-=1;
for (k=i;k<num;k+=1) { vecAct[k] = vecAct[k+1]; }
vecAct.pop(); i-=1;
}//nexti
gt +=1;
}//onenter
}//classend
}
internal class xAct
{
public var cx:Number = 0;
public var cy:Number = 0;
public var hp:Number = 32;
public var ang:Number = 0;
public var team:int = 0;
public var reload:int = 0;
public var id:int = 0;
public var col:uint =0;
public var targ:xAct = null;
};//xact