/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4JAS
*/
package {
import flash.events.MouseEvent;
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()*100+32,100);
a.team = 1; a.hp=400; a.spec=1; a.rad = 24;
a.gx = a.cx +32; a.gy = a.cy+32;
// 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.gx = a.cx; a.gy = a.cy-44;
a = addUnit(Math.random()*400+32,400);
a.team = 4; a.hp=400; a.spec=1;
a.gx = a.cx; a.gy = a.cy-44;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecColor:Vector.<uint> = Vector.<uint>([0x808080, 0xFF0000,0xFF,0x808000,0x008000,0xF000F0]);
public var mdown:int = 0;
public function onMdown(e:MouseEvent):void { mdown = 1; }
public function onMup(e:MouseEvent):void { mdown = 0; }
public var vecUnit:Vector.<xUnit>;
public var mx:Number = 0;
public var my:Number = 0;
public var wmx:Number = 0;
public var wmy:Number = 0;
public var numAlive:int =0;
public var gt:int = 0;
public var curOver:xUnit = null;
public var curSel:xUnit = null;
public function onEnter(e:Event):void
{
mx = stage.mouseX; my = stage.mouseY;
wmx = mx; wmy = my;
graphics.clear();
graphics.beginFill(0x003020,0.4);
graphics.drawRect(0,0,465,465);
graphics.endFill();
graphics.lineStyle(2,0);
graphics.drawRect(8,8,465-16,465-16);
update();
curOver = getOver(mx, my, 8, 1);
handSel();
gt += 1;
}//onenter
public function handSel():void
{
var a:xUnit; a = curOver
if (a != null)
{
graphics.drawRect(a.cx-a.rad,a.cy-a.rad,a.rad*2,a.rad*2);
if (mdown > 0) { curSel = a; }
if (a.spec == 1)
{ graphics.drawCircle(a.gx, a.gy, 4); }
if (a.mode > 0)
{
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(a.gx,a.gy);
}
}//endif
a = curSel;
if (a == null) { return; }
if (a.hp <= 0) { curSel = null; return; }
graphics.drawRect(a.cx-a.rad,a.cy-a.rad,a.rad*2,a.rad*2);
if (mdown > 0 && curOver != a)
{
curSel = null;
a.gx = wmx; a.gy = wmy; a.mode = 1;
}
}//handsel
public function getOver(ax:Number, ay:Number, r:Number, ofteam:int):xUnit
{
var i:int; var num:int; var a:xUnit; var d:Number;
num = vecUnit.length;
for (i = 0; i < num; i++)
{
a = vecUnit[i]; if (a.hp <=0) {continue;}
if (a.team != ofteam) { continue;}
if (getMag(a.cx-ax,a.cy-ay) > (a.rad+r)) { continue; }
return a;
}//nexti
return null;
}//getover
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.rad = 12;
a.range = 32; a.targ = null;
//a.vx = Math.random()-0.5; a.vy = Math.random()-0.5;
a.vx = 1;
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 function update():void
{
var na:int; na = 0;
var i:int; var num:int; var a:xUnit;
var b:xUnit;
var d:Number;
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 + 120;
b = addUnit(a.cx+Math.random()*16-8,a.cy+Math.random()*16-8);
b.team = a.team;
b.gx = a.gx; b.gy = a.gy; b.mode = 1;
if (a.team > 1)
{
a.ang = Math.random()*6.28;
d = Math.random()*34 + a.rad;
a.gx = a.cx + Math.cos(a.ang) * d;
a.gy = a.cy+ Math.sin(a.ang) * d;
}//endif2
} //endif
continue;
}//endif
//regular unit
if (a.targ == null)
if ((gt % 16) == (i%16))
{
a.targ = getClose(a, a.sight);
//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)
{
if (a.mode == 1)
{
a.ang = Math.atan2(a.gy-a.cy,a.gx-a.cx);
a.vx = Math.cos(a.ang);
a.vy = Math.sin(a.ang);
if (getMag(a.gx-a.cx,a.gy-a.cy) < a.rad)
{ a.vx =0; a.vy =0; a.mode = 0; }
}
}//endif
if (a.targ != null)
{
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)
{
if (a.mode != 1) { 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 if (a.mode != 1)
{
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); }
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 gx:Number = 0;
public var gy:Number = 0;
public var mode:int = 0;
public var ang:Number =0;
public var hp:Number = 0;
public var team:int = 0;
public var targ:xUnit = null; //i shouldnt do this and use some id instead
public var reload:int = 0;
public var fire:int = 0;
public var sight:Number = 128;
public var range:Number = 64;
public var dmg:Number = 3;
public var dmgTime:int = 0;
public var rad:Number = 8;
public var spec:int = 0;
}//xunit