flash on 2013-6-27
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ltND
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
reset();
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecAct:Vector.<xActor>;
public var base1:xActor;
public var base2:xActor;
public var gameWait:int = 0;
public function reset():void
{
vecAct = new Vector.<xActor>(256, false);
var i:int;
var num:int;
num = vecAct.length;
for (i = 0; i < 256; i++)
{ vecAct[i] = new xActor(); }
var b:xActor;
b = vecAct[0]; // addActor(40, 200, 0);
b.cx= 40;
b.cy = 200;
b.team = 0;
b.mode = 99;
b.vx = 1;
b.col = 0xFFFFFF * Math.random();
b.hp = 100;
base1 = b;
b = vecAct[1]; //addActor(400, 200, 1);
b.cx = 400;
b.cy = 200;
b.team = 1;
b.mode = 99;
b.vx = -1;
b.col = 0xFFFFFF * Math.random();
b.hp = 100;
base2 = b;
gameWait= 0;
}//reset
public function onEnter(e:Event):void
{
var i:int;
var num:int;
var a:xActor;
var b:xActor;
graphics.clear();
graphics.lineStyle(2, 0);
num = vecAct.length;
for (i = 0; i < num; i++)
{
a = vecAct[i];
if (a.hp <= 0) { continue; }
if (a.mode == 99)
{
a.wait += 1;
if (a.wait >= 60)
{
a.wait = 0;
var c:int;
c = Math.random() * 9;
b = addActor(a.cx, a.cy, a.team);
if (b == null) { continue; }
b.vx = a.vx;
b.cy = a.cy + (Math.random()-Math.random())*64;
b.col = a.col;
b.wait = 0;
b.reload = 0;
switch (c)
{
//grunt
default:
b.hp = 100;
b.rad = 12;
b.range = 32;
b.damage = 10;
b.retime = 9;
break;
//heavy
case 1:
b.vx *= 0.5;
b.hp = 200;
b.rad = 16;
b.range = 32;
b.damage = 10;
b.retime = 12;
break;
//long range
case 2:
b.vx *= 0.3;
b.range = 100;
b.rad = 8;
b.hp = 50;
b.damage = 5;
b.retime = 8;
break;
//sniper
case 3:
b.vx *= 0.15;
b.range = 200;
b.rad = 10;
b.hp = 50;
b.damage = 40;
b.retime = 120;
break;
case 4:
b.vx *= 0.1;
b.range = 50;
b.rad = 25;
b.hp = 300;
b.damage = 30;
b.retime = 25;
break;
}//swend
}//endif2
graphics.drawRect(a.cx-12,a.cy-20, 24, 30);
continue;
}//endif
if (a.target != null)
{
if (a.reload > 0) { a.reload -= 1;}
if (a.reload <= 0)
{
a.reload = a.retime;
graphics.moveTo(a.cx, a.cy);
graphics.lineTo(a.target.cx+Math.random()*4, a.target.cy+Math.random()*4);
a.target.hp -= a.damage;
}
if (a.target.hp <= 0) { a.target = null; }
}
else
{
a.wait += 1;
if (a.wait >= 5)
{
a.target = findTarget(a, a.range);
a.wait = 0;
}
a.cx += a.vx;
}//endif
graphics.drawRect(a.cx-8, a.cy-a.rad-7, (a.hp/100)*(16) ,4);
graphics.beginFill(a.col, 0.2);
graphics.drawCircle(a.cx, a.cy, a.rad);
graphics.endFill();
if (a.cx <= -100 || a.cx >= 565) { a.hp = -1;}
}//nexti
//if (base1)
// if (base2)
if (base1.hp <= 0 || base2.hp <= 0)
{
gameWait += 1;
if (gameWait >= 90) { reset(); gameWait = 0;}
}
}//onenter
public function addActor(ax:Number, ay:Number, t:int):xActor
{
var i:int;
var num:int;
var a:xActor;
num = vecAct.length;
//first 2 is reserved for bases
for (i = 2; i < num; i++)
{
a = vecAct[i];
if (a.hp > 0) { continue; }
a.cx = ax;
a.cy = ay;
//a.vx = 0;
// a.vy = 0;
a.hp = 100;
a.team = t;
a.target = null;
//a.mode = 0;
return a;
}//nexti
return null;
}//addactor
public function findTarget(m:xActor, range:Number):xActor
{
var i:int;
var num:int;
var a:xActor;
var dist:Number;
var close:Number;
var ret:xActor;
ret = null;
close = 999999999999;
range *= range;
num = vecAct.length;
for (i = 0; i < num; i++)
{
a = vecAct[i];
if (a == m) { continue; } // self
if (a.hp <= 0) { continue; } //not exist
if (a.team == m.team) { continue; } //same team
dist = ((a.cx-m.cx)*(a.cx-m.cx))+((a.cy-m.cy)*(a.cy-m.cy));
if ( dist > range) { continue; }
if ( dist > close) { continue; }
close = dist;
ret = a;
// return a;
}//nexti
return ret;
}//findtarget
}//classend
}
internal class xActor
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var rad:Number = 12;
public var hp:Number = 0;
public var team:int = 0;
public var range:Number = 32;
public var retime:int = 9;
public var damage:Number = 10;
public var wp:int = 0;
public var wait:int = 0;
public var mode:int = 0;
public var reload:int = 0;
public var col:uint = 0;
public var target:xActor = null;
}//xactor