flash on 2009-11-15
package{
import flash.display.*;
import flash.events.*;
public class Flashtest extends MovieClip{
public var Keys:Array;
public var mouse:Boolean;
public var Turret:MovieClip;
public var Bullets:Array;
public var BMC:MovieClip;
public var Enemies:Array;
public var EMC:MovieClip;
public var Lines:Array;
public var LMC:MovieClip;
public function Flashtest():void{
Keys = new Array(255);
Turret = drawTurret(stage.stageWidth/2);
Bullets = new Array();
BMC = new MovieClip();
Enemies = new Array();
EMC = new MovieClip();
Lines = new Array();
LMC = new MovieClip();
addChildAt(EMC,0);
addChildAt(LMC,1);
addChildAt(BMC,2);
addChildAt(Turret,3);
mouse = false;
drawStage();
stage.addEventListener(Event.ENTER_FRAME,main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseup);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyup);
}
public function main(e:Event):void{
updateTurret();
updateBullets();
}
public function mousedown(e:MouseEvent):void{
mouse = true;
turretFire();
}
public function mouseup(e:MouseEvent):void{
mouse = false;
}
public function mousemove(e:MouseEvent):void{
}
public function keydown(e:KeyboardEvent):void{
Keys[e.keyCode] = true;
}
public function keyup(e:KeyboardEvent):void{
Keys[e.keyCode] = false;
}
public function isKeyDown(n:int):Boolean{
return Keys[n];
}
public function drawStage():void{
var h:Number = 40;
graphics.beginFill(0x999999);
graphics.drawRect(0,stage.stageHeight-h,stage.stageWidth,h);
graphics.endFill();
}
public function addBullet(X:Number,Y:Number,V:Number,A:Number,R:Number):void{
var b:MovieClip = new MovieClip();
BMC.addChild(b);
Bullets.push(b);
b.x = X;
b.y = Y;
b.radius = R;
b.angle = A;
b.rotation = A;
var VX:Number = Math.cos(A*Math.PI/180)*V;
var VY:Number = Math.sin(A*Math.PI/180)*V;
b.vx = VX;
b.vy = VY;
var B:MovieClip = new MovieClip();
B.graphics.beginFill(0x555555);
B.graphics.moveTo(5,5);
B.graphics.lineTo(20,5);
B.graphics.lineTo(25,0);
B.graphics.lineTo(20,-5);
B.graphics.lineTo(5,-5);
B.graphics.endFill();
b.Particles = new Array();
b.color = Math.random()*0xffffff;
var L:Array = new Array();
for(var i:int=0;i<5;i++){
var p:MovieClip = new MovieClip();
b.Particles.push(p);
b.addChild(p);
p.graphics.beginFill(b.color);
p.graphics.drawCircle(0,0,2);
p.graphics.endFill();
p.x = Math.random()*10-5;
p.y = Math.random()*10-5;
p.vx = 0;
p.vy = 0;
var l:MovieClip = new MovieClip();
L.push(l);
LMC.addChild(l);
l.graphics.lineStyle(1,b.color,1);
l.graphics.moveTo(b.x+p.x,b.y+p.y);
}
Lines.push(L);
b.addChild(B);
}
public function updateBullets():void{
for(var i:int=0;i<Bullets.length;i++){
var b:MovieClip = Bullets[i];
b.x += b.vx;
b.y += b.vy;
for(var j:int=0;j<b.Particles.length;j++){
var p:MovieClip = b.Particles[j];
if(p.x > 0){
p.vx -= 1;
}else if(p.x < 0){
p.vx += 1;
}
if(p.y > 0){
p.vy -= 1;
}else if(p.y < 0){
p.vy += 1;
}
p.x += p.vx;
p.y += p.vy;
Lines[i][j].graphics.lineTo(b.x+p.x,b.y+p.y);
}
if(b.x < -5 || b.x > stage.stageWidth+5 || b.y < -5 || b.y > stage.stageHeight+5){
Bullets.splice(i,1);
Lines.splice(i,1);
BMC.removeChild(b);
}
}
}
public function drawTurret(X:Number):MovieClip{
var t:MovieClip = new MovieClip();
t.x = X;
t.y = stage.stageHeight-75;
t.timer = 0;
t.health = 1;
t.angle = 0;
t.barrel = 40;
t.wide = 20;
t.flare = false;
t.divide = false;
t.weaponlag = 10;
t.bulletspeed = 5;
t.bulletsize = 10;
return t;
}
public function updateTurret():void{
Turret.graphics.clear();
if(Turret.timer > 0){
Turret.timer -= 1;
}
var X:Number = mouseX-Turret.x;
var Y:Number = mouseY-Turret.y;
var a:Number = Math.atan(Y/X)*180/Math.PI;
if(X < 0){
a += 180;
}
Turret.angle = a;
if(Turret.autofire && mouse){
turretFire();
}
Turret.graphics.lineStyle(1,0x000000,1);
Turret.graphics.beginFill(0x666666);
Turret.graphics.drawRect(-20,15,Turret.barrel,20);
Turret.graphics.endFill();
Turret.graphics.beginFill(0x666666);
Turret.graphics.drawCircle(0,0,20);
Turret.graphics.endFill();
Turret.graphics.beginFill(0xffffff);
Turret.graphics.moveTo(0,0);
var R:Array = spinRect(Turret.wide/2,Turret.barrel,a,Turret.divide);
for(var j:int=0;j<R.length;j++){
Turret.graphics.lineTo(R[j][0],R[j][1]);
}
}
public function turretFire():void{
if(Turret.timer == 0){
Turret.timer = Turret.weaponlag;
addBullet(Turret.x+Math.cos(Turret.angle*Math.PI/180)*10,Turret.y+Math.sin(Turret.angle*Math.PI/180)*10,Turret.bulletspeed,Turret.angle,Turret.bulletsize);
}
}
public function spinRect(W:Number,H:Number,A:Number,S:Boolean=false):Array{
var a:Array = new Array();
var r:Number = Math.atan(H/W)*180/Math.PI-90;
var d:Number = Math.sqrt(Math.pow(H,2)+Math.pow(W,2));
a.push([Math.cos((A+90)*Math.PI/180)*W,Math.sin((A+90)*Math.PI/180)*W]);
a.push([Math.cos((A-r)*Math.PI/180)*d,Math.sin((A-r)*Math.PI/180)*d]);
a.push([Math.cos((A+r)*Math.PI/180)*d,Math.sin((A+r)*Math.PI/180)*d]);
a.push([Math.cos((A-90)*Math.PI/180)*W,Math.sin((A-90)*Math.PI/180)*W]);
a.push([0,0]);
if(S){
a.push([Math.cos(A*Math.PI/180)*H,Math.sin(A*Math.PI/180)*H]);
}
return a;
}
}
}