/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4too
*/
package {
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
deb = new TextField();
deb.width=320; deb.height=240;
deb.mouseEnabled=false;
addChild(deb);
vecAct = new Vector.<xAct>(0, false);
var i:int; var a:xAct;
for(i=0;i<19;i+=1)
{
a = new xAct();
a.team = 0xFF;
a.cx =Math.random()*400; a.cy = Math.random()*400;
// a.vz = 6; a.vx =Math.random()*2-1; a.vy = Math.random()*2-1;
vecAct.push(a);
}//nexti
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var deb:TextField;
public var vecAct:Vector.<xAct>;
public function onMdown(e:MouseEvent):void { mbutton =1;}
public function onMup(e:MouseEvent):void { mbutton = 0;}
public var curId:int = 1;
public var gt:int= 0;
public var mbutton:int = 0;
public function addActor(a:xAct):void { vecAct.push(a); a.id = curId; curId+=1; }
public function getMag(ax:Number, ay:Number):Number { return Math.sqrt(ax*ax+ay*ay); }
public function findTarg(m:xAct, dist:Number):xAct
{
var i:int; var num:int; var a:xAct;
var ret:xAct; var d:Number;
num = vecAct.length;
for (i=0;i<num;i+=1)
{
a = vecAct[i];
if (a==m) { continue; }
if (a.hp <=0) {continue;}
if (a.spec > 0) { continue; }
if (a.team==m.team) { continue; }
d = getMag(a.cx-m.cx, a.cy-m.cy);
if (d >dist) { continue; }
ret = a;
}//nexti
return ret;
}//findtarg
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var ta:Number; var ms: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; }
a.vx *=0.9; a.vy*=0.9;
a.vz -=0.2;
a.cx+=a.vx; a.cy+=a.vy; a.cz+=a.vz;
if(a.cz < 8) { a.cz = 8; }
if (a.targ != null && a.targ.hp<=0) { a.targ=null;}
if ((a.id + gt) %40 ==0)
{
if (a.targ != null ) { if (getMag(a.cx-a.targ.cx, a.cy-a.targ.cy) < 40) { a.targ.hp -= 8; a.targ.vz=1; } }
if (a.targ == null) { a.targ = findTarg(a, 128); }
}//endif
if (a.targ !=null)
{ ta = Math.atan2(a.targ.cy-a.cy,a.targ.cx-a.cx);
if (getMag(a.cx-a.targ.cx, a.cy-a.targ.cy) >32)
{ ms = 3; a.vx = Math.cos(ta)*ms; a.vy = Math.sin(ta)*ms; }
else { a.vx*=0.8; a.vy *=0.8; }
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(a.targ.cx, a.targ.cy);
}//endif
graphics.beginFill(a.team, 1);
graphics.drawCircle(a.cx, a.cy-a.cz, 8);
graphics.endFill();
graphics.moveTo(a.cx-8,a.cy);graphics.lineTo(a.cx+8,a.cy);
}//nexti
num = vecAct.length; var k:int;
for(i=0;i<num;i+=1)
{
a = vecAct[i];
if (a.hp > 0) { continue; }
for(k=i;k<num-1;k+=1) { vecAct[k] = vecAct[k+1];}
i-=1; vecAct.pop(); num-=1;
}//nexti
deb.text = ""+vecAct.length;
if (mbutton > 0)
{
a = new xAct();
a.team = 0xFF0000;
a.cx =stage.mouseX; a.cy = stage.mouseY;
a.cz = 8;
a.vz = 3; a.vx =Math.random()*2-1; a.vy = Math.random()*2-1;
vecAct.push(a);
}//endif
gt +=1;
}//onenter
}//classend
}
internal class xAct
{
public var cx:Number = 0;
public var cy:Number = 0;
public var cz:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var vz:Number = 0;
public var hp:Number = 30;
public var id:int = 0;
public var spec:int = 0;
public var team:int = 0;
public var think:int = 0;
public var targ:xAct = null;
}//xact