/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qX6g
*/
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 )) ); }
var a:xAct;
for (i=0;i<4;i+=1)
{
a = new xAct(); a.cx= 64; a.cy=64; a.spec=1; a.rad=24; a.hp=1000; a.team=i; a.col = vecTeam[a.team]; vecAct.push(a);
if (i==1){ a.cx = 512; }
if (i==2){ a.cy=512;}
if (i==3){a.cx=512; a.cy=512; }
a.gx=a.cx;a.gy=a.cy;
}
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, gx:Number, gy:Number, 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= gx/cw; ay = gy/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);
graphics.drawRect(0,0,mw*cw,mh*ch);
/*
if (vecAct.length < 16)
{
a = new xAct();
a.cx = Math.random()*512;
a.cy = Math.random()*512;
a.rad = Math.random()*8+8;
a.gx=a.cx; a.gy=a.cy;
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.dmgTime>gt?0xFFffFF:a.col,1);
graphics.drawCircle(a.cx,a.cy,a.rad);
graphics.endFill();
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(a.cx+Math.cos(a.ang)*8, a.cy+Math.sin(a.ang)*a.rad);
if(a.spec == 1)
{
if (Math.random()<0.1)
{
a.gx = Math.random()*512;
a.gy = Math.random()*512;
}
if (a.reload < gt)
{
a.reload = gt+16;
var b:xAct;
b = new xAct();
b.team=a.team;
b.col = a.col;
b.cx = a.cx + Math.cos(a.ang)*32;
b.cy = a.cy + Math.sin(a.ang)*32;
// b.owner=a;
b.gx=a.gx; b.gy=a.gy;
if (Math.random()<0.5) { b.owner =a; }
vecAct.push(b);
}//endif2
}//endif
if (a.targ ==null ) // && a.spec==0)
{
d = getMag(a.cx-a.gx, a.cy-a.gy);
ta = Math.atan2(a.gy-a.cy, a.gx-a.cx);
a.ang = ta;
if (d > 32) {
a.cx += Math.cos(ta)*3;
a.cy += Math.sin(ta)*3;
}
}//endif
if(a.spec == 0 && a.owner!=null)
{ if (a.owner.hp<=0){ a.owner=null;} }
if(a.spec == 0 && a.owner!=null)
{ a.gx = a.owner.cx; a.gy=a.owner.cy;
if (a.owner.targ !=null) { a.targ = a.owner.targ; }
}//endif
if (a.targ!=null) { if (a.targ.hp<=0){a.targ=null;} }
if (a.targ !=null && a.spec==0)
{ d = getMag(a.cx-a.targ.cx, a.cy-a.targ.cy);
ta = Math.atan2(a.targ.cy-a.cy,a.targ.cx-a.cx);
a.ang = ta;
d -= a.rad; d -= a.targ.rad;
if (d > 8) {
a.cx += Math.cos(ta)*3;
a.cy += Math.sin(ta)*3;
} else if (a.reload < gt)
{
a.reload = gt+16;
a.targ.hp-=6;
a.targ.dmgTime = gt+4;
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 (a.targ==null && a.spec==0) { graphics.moveTo(a.cx,a.cy); graphics.lineTo(a.gx,a.gy); }
if ( (gt+a.id)%20 ==0){
// graphics.drawCircle(a.cx,a.cy,4);
if (a.spec==0) { a.targ = getTarg(a, a.gx,a.gy, 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 gx:Number =0;
public var gy:Number= 0;
public var hp:Number = 32;
public var spec:int = 0;
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 rad:Number = 8;
public var dmgTime:int = 0;
public var targ:xAct = null;
public var owner:xAct=null;
};//xact