/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/bdmn
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
var i:int;
vecUnit = new Vector.<xUnit>(0,false);
for(i=0;i<128;i++) {vecUnit.push(new xUnit()); }
for (i = 0; i < 16; i++) { vecColor.push(Math.random()*0xFFffFFff); }
for (i =0 ; i < 32; i++) { addUnit(Math.random()*400,Math.random()*400).team = Math.random()*4+1;}
var a:xUnit;
a = addUnit(Math.random()*400+32,100); a.team = 1; a.hp=400; a.spec=1;
a = addUnit(Math.random()*400+32,200); a.team = 2; a.hp=400; a.spec=1;
a = addUnit(Math.random()*400+32,300); a.team = 3; a.hp=400; a.spec=1;
a = addUnit(Math.random()*400+32,400); a.team = 4; a.hp=400; a.spec=1;
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecColor:Vector.<uint> = Vector.<uint>([0x808080, 0xFF0000,0xFF,0x808000,0x008000,0xF000F0]);
public var vecUnit:Vector.<xUnit>;
public function addUnit(ax:Number, ay:Number):xUnit
{
var a:xUnit;
a = getFree();
a.hp=30;
a.cx=ax; a.cy=ay;
a.vx=0; a.vy=0;
//a.range = 32+ Math.random()*64;
a.range = 32;
a.targ = null;
a.vx = Math.random()-0.5; a.vy = Math.random()-0.5;
return a;
}//addunit
public function getFree():xUnit
{
var i:int; var num:int; var a:xUnit;
num = vecUnit.length;
for (i = 0; i < num; i++)
{ a = vecUnit[i]; if (a.hp>0){continue;} return a; }
return a; //return last if reached limit
}//makeunit
public function getClose(m:xUnit, maxd:Number=800):xUnit
{
var i:int; var num:int; var a:xUnit;
var d:Number; var ret:xUnit;
ret = null;
maxd *= maxd;//squared
num = vecUnit.length;
for (i = 0; i < num; i++)
{
a = vecUnit[i]; if (a.hp <=0) {continue;}
if (a.team==m.team){continue;}
if (a == m) {continue;}
d = (a.cx-m.cx)*(a.cx-m.cx) + (a.cy-m.cy)*(a.cy-m.cy);
if (d > maxd) {continue;}
ret = a; maxd = d;
}//nexti
return ret;
}//getclose
public var numAlive:int =0;
public var gt:int = 0;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.beginFill(0x003000,0.4);
graphics.drawRect(0,0,465,465);
graphics.endFill();
graphics.lineStyle(2,0);
gt += 1;
var na:int; na = 0;
var i:int; var num:int; var a:xUnit;
num = vecUnit.length;
for (i = 0; i < num; i++)
{
a = vecUnit[i]; if (a.hp<=0){continue;}
na +=1;
//base
if (a.spec == 1)
{
var c:uint;
c = vecColor[a.team];
if (a.dmgTime > gt) { c=0xFFffFFff; }
graphics.beginFill(c,0.85);
graphics.drawCircle(a.cx, a.cy, 16);
graphics.endFill();
if (numAlive < 100)
if (a.reload < gt)
{
a.reload = gt + 60;
addUnit(a.cx+Math.random()*16-8,a.cy+Math.random()*16-8).team = a.team;
} //endif
continue;
}//endif
//regular unit
if (a.targ == null)
if ((gt % 16) == (i%16))
{
a.targ = getClose(a);
//a.fire = gt+5;
if (getMag(a.vx,a.vy)<0.5)
{a.vx =Math.random()*2-1; a.vy = Math.random()*2-1;}
}//endif
if (a.targ != null)
{
var d:Number;
d = getMag(a.cx-a.targ.cx, a.cy-a.targ.cy);
if (d > 300 || a.targ.hp<0 || a.targ.team == a.team) { a.targ = null;}
else
{
if (d < a.range)
{
a.vx=0;a.vy=0;
if (a.reload < gt)
{
a.reload = gt+20;
a.fire =gt+3;
a.targ.hp -= a.dmg;
a.dmgTime = gt+4;
if (a.targ.targ == null){a.targ.targ = a;}
if (a.targ.hp <0) {a.targ=null;}
}//endif5
}
else
{
a.ang = Math.atan2(a.targ.cy-a.cy,a.targ.cx-a.cx);
a.vx = Math.cos(a.ang);
a.vy = Math.sin(a.ang);
} //endif4
}//endif3
}//endif2
a.cx += a.vx;
a.cy += a.vy;
if (a.vx >0 && a.cx > 465) {a.vx*=-1;}
else if (a.vx<0 && a.cx < 0) {a.vx*=-1;}
if (a.vy >0 && a.cy > 465) {a.vy*=-1;}
else if (a.vy<0 && a.cy < 0) {a.vy*=-1;}
if (a.fire > gt)
if (a.targ != null)
if (a.targ.hp > 0)
{ graphics.moveTo(a.cx,a.cy); graphics.lineTo(a.targ.cx, a.targ.cy); }
//var c:uint;
c = vecColor[a.team];
if (a.dmgTime > gt) { c=0xFFffFFff; }
graphics.beginFill(c,0.85);
graphics.drawCircle(a.cx, a.cy, 8);
graphics.endFill();
}//nexti
numAlive = na;
}//onenter
public function getMag(ax:Number,ay:Number):Number
{return Math.sqrt(ax*ax+ay*ay);}
}//classend
}
internal class xUnit
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var ang:Number =0;
public var hp:Number = 0;
public var team:int = 0;
public var targ:xUnit = null;
public var reload:int = 0;
public var fire:int = 0;
public var range:Number = 64;
public var dmg:Number = 3;
public var dmgTime:int = 0;
public var spec:int = 0;
}//xunit