Lightsaber Fighter
// forked from Abarrow's Lightsaber Fighter
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.geom.Matrix;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.events.ProgressEvent;
import flash.display.DisplayObject;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.events.KeyboardEvent;
import flash.filters.GlowFilter;
public class LightSaberFighter extends Sprite {
public var light:Sprite;
public var dragging:Boolean;
public var saber:Sprite;
public var hearts:Vector.<Sprite>;
public var reload:Number;
public var life:Number;
public var handle:Sprite;
public var dead:Boolean;
public var output:TextField;
public var score:Number;
public var level:Number;
public var oldscore:Number;
public var pause:Boolean;
public var background:Loader;
public var ghost:Boolean;
public var ghosttime:Number;
public var forcerecharge:Number;
public var force:Boolean;
public var boss:MovieClip;
public var paused:Boolean;
public var setup:Boolean;
[SWF(backgroundColor="0x000000", frameRate=20, width=465, height=465)]
public function LightSaberFighter() {
stage.scaleMode=StageScaleMode.SHOW_ALL;
background=new Loader();
var url:String = "http://www.through-africa.com/mediac/450_0/media/Tunisia$20-$20Star$20Wars.jpg";
var urlReq:URLRequest = new URLRequest(url);
background.load(urlReq);
background.scaleX=background.scaleY=1.6;
addChild(background);
var pow:Loader=new Loader();
url = "http://www.nukeation.net/content/binary/tmp43459c695707c_vaderminipi.gif";
urlReq = new URLRequest(url);
pow.load(urlReq);
pow.scaleX=pow.scaleY=.4;
light=new Sprite();
pow.x=-100;
pow.y=-50;
light.addChild(pow);
saber=new Sprite();
lazer(saber,300,12,0x00FFFF);
light.addChild(saber);
handle=new Sprite();
lazer(handle,166,16,0xFF0000);
handle.visible=false;
handle.x=-96;
handle.y=-13;
light.addChild(handle);
addChild(light);
saber.y-=10;
saber.x+=70;
light.x=150;
light.y=150;
light.scaleX=light.scaleY=.70;
var powe:Loader=new Loader();
url = "http://www.nukeation.net/content/binary/tmp43459c695707c_vaderminipi.gif";
urlReq = new URLRequest(url);
powe.load(urlReq);
powe.scaleX=powe.scaleY=.4;
boss=new MovieClip();
powe.x=-100;
powe.y=-50;
boss.addChild(powe);
var sabre:Sprite=new Sprite();
lazer(sabre,300,12,0xFF0000);
boss.addChild(sabre);
var handl:Sprite=new Sprite();
lazer(handl,166,16,0xFF0000);
handl.visible=false;
handl.x=-96;
handl.y=-13;
boss.addChild(handl);
sabre.y-=10;
sabre.x+=70;
boss.x=200;
boss.y=200;
boss.scaleX=boss.scaleY=.70;
//addChild(boss);
dragging=false;
reload=0;
life=5;
dead=false;
score=0;
level=1;
oldscore=0;
pause=false;
ghost=false;
ghosttime=0;
forcerecharge=0;
paused=true;
setup=true;
hearts=new Vector.<Sprite>();
for(var n:Number=0;n<5;n++){
var heart:Sprite=new Sprite();
var loadheart:Loader=new Loader();
url = "http://www.pixabella.com/wp-content/uploads/2009/03/love-heart-clipart.png";
var heartreq:URLRequest = new URLRequest(url);
loadheart.load(heartreq);
heart.scaleX=heart.scaleY=.07;
heart.x=30*n;
heart.addChild(loadheart);
hearts.push(heart);
addChild(heart);
}
output=new TextField();
output.width=465;
output.height=465;
output.multiline=true;
output.wordWrap=true;
output.background=false;
output.selectable=false;
output.x=0;
output.y=0;
addChild(output);
var request:URLRequest = new URLRequest("http://www.paducahimperials.com/media/_Sigle_TV_Star_War_theme.mp3");
var soundFactory:Sound = new Sound();
soundFactory.load(request);
soundFactory.addEventListener(ProgressEvent.PROGRESS,loading);
soundFactory.addEventListener(Event.COMPLETE, doneloading);
soundFactory.play(0,int.MAX_VALUE);
addEventListener(Event.ENTER_FRAME,createlazer);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mover);
stage.addEventListener(MouseEvent.MOUSE_DOWN,click);
stage.addEventListener(MouseEvent.MOUSE_UP,unclick);
stage.addEventListener(KeyboardEvent.KEY_DOWN,continu);
}
private function createlazer(event:Event):void{
if(!pause){
if(!dead){
if(!paused){
forcerecharge--;
if(ghost){
ghosttime--;
light.alpha=.5;
if(ghosttime<0){
ghost=false;
}
}else{
light.alpha=1;
}
if(force){
ghosttime--;
if(ghosttime<0){
force=false;
}
}
//output.htmlText="<font size='20' face='Aston-F1'><p align='right'>"+(light.rotation+180)+"</p></font>"
output.htmlText="<font size='20' face='Aston-F1'><p align='right'>Level:"+level+" Score: "+score+"</p></font>"
if(oldscore+10*level<=score){
life=5;
oldscore=score;
level++;
pause=true;
if(level==2){
var url:String = "http://farm1.static.flickr.com/57/215672195_f3cda59e55.jpg";
var urlReq:URLRequest = new URLRequest(url);
background.load(urlReq);
}else if(level==3){
var urle:String = "http://images.wikia.com/starwars/images/c/c1/Lava_Rivers.jpg";
var urlReqe:URLRequest = new URLRequest(urle);
background.load(urlReqe);
}else if(level==4){
var urlee:String = "http://www.obh.snafu.de/~madley/starwars/Hoth/battle_hoth.jpg";
var urlReqee:URLRequest = new URLRequest(urlee);
background.load(urlReqee);
}else if(level==5){
var urleee:String = "http://www.blogcdn.com/www.scifisquad.com/media/2009/11/luke-yoda.jpg";
var urlReqeee:URLRequest = new URLRequest(urleee);
background.load(urlReqeee);
}else if(level==6){
var urleeee:String = "http://img.engadget.com/common/images/1758767121143966.JPG?0.06620922838268961";
var urlReqeeee:URLRequest = new URLRequest(urleeee);
background.load(urlReqeeee);
addChild(boss);
boss.rot=0;
boss.rota=0;
boss.health=150;
reload=0;
}else{
}
}
for(var n:Number=0;n<5;n++){
if(life>n){
hearts[n].visible=true;
}else{
hearts[n].visible=false;
}
}
if(life<=0){
dead=true;
}
reload--;
if(reload<0){
if(level==6){
if(!force){
if(Math.abs(light.x-boss.x)<75){
boss.rot+=3;
}else if(light.x>boss.x){
boss.rot++;
boss.x+=3;
boss.rota=0;
}else if(light.x<boss.x){
boss.rot++;
boss.x-=3;
boss.rota=180;
}
}
if(Math.abs(light.y-boss.y)<75){
}else if(light.y>boss.y){
boss.y+=3;
}else if(light.y<boss.y){
boss.y-=3;
}
var hit:Boolean=false
boss.rotation=boss.rota+boss.rot;
for(var spot:Number=0;spot<300;spot+=10){
if(boss.getChildAt(2).hitTestPoint(light.x+Math.cos(light.rotation/180*Math.PI)*spot,light.y+Math.sin(light.rotation/180*Math.PI)*spot,true)){
hit=true;
}
}
boss.alpha=1;
if(hit){
boss.health--;
boss.alpha=.5;
}
if(boss.health==0){
score+=60;
}
hit=false;
for(spot=0;spot<300;spot+=10){
if(handle.hitTestPoint(boss.x+Math.cos(boss.rotation/180*Math.PI)*spot,boss.y+Math.sin(boss.rotation/180*Math.PI)*spot,true)){
hit=true;
}
}
if(!ghost){
if(hit){
life-=1;
}
}
}else{
if(!force){
if(level>2){
reload=75;
}else{
reload=100;
}
lazerblast().rotation=Math.random()*90;
if(level>=2){
var sho:Sprite=lazerblast();
sho.rotation=-90+Math.random()*-90;
sho.x=465;
sho.y=465;
}
if(level>=4){
var shoe:Sprite=lazerblast();
shoe.rotation=90+Math.random()*90;
shoe.x=465;
shoe.y=0;
}
if(level>=5){
var shoee:Sprite=lazerblast();
shoee.rotation=Math.random()*-90;
shoee.x=0;
shoee.y=465;
}
}
}
}
}else{
var liste:String="Click and drag to move your lightsaber<br>Move your mouse to aim<br>Press D to make your lightsaber blue";
if(level>1){
liste+="<br>Press H to make your lightsaber green"
}
if(level>2){
liste+="<br>Press J to make your lightsaber purple"
}
if(level>3){
liste+="<br>Press S to make your lightsaber red"
}
if(level>4){
liste+="<br>Press F to stall your enemies using The Force"
}
if(level>5){
liste+="<br>Press G to turn invisible as a Force Ghost"
}
if(setup){
output.htmlText="<br><p align='center'><font size='30' face='Aston-F1'>Lightsaber Fighter</font><br><font size='20'> Press Space To Play<br>"+liste+"</font></p>"
}else{
output.htmlText="<br><p align='center'><font size='30' face='Aston-F1'>Paused</font><br><font size='20'> Press Space To Continue<br>"+liste+"</font></p>"
}
}
}else{
output.htmlText="<p align='center'><font size='30' face='Aston-F1'>Game Over</font><br><font size='20'> Press Any Key To Try Again</font></p>";
}
}else{
if(level>6){
output.htmlText="<p align='center'><font size='30' face='Aston-F1'><br>You have won scoring "+score+" points!</font></p>"
}else{
var list:String="Press D to make your lightsaber blue";
if(level>1){
list+="<br>Press H to make your lightsaber green"
}
if(level>2){
list+="<br>Press J to make your lightsaber purple"
}
if(level>3){
list+="<br>Press S to make your lightsaber red"
}
if(level>4){
list+="<br>Press F to stall your enemies using The Force"
}
if(level>5){
list+="<br>Press G to turn invisible as a Force Ghost"
}
output.htmlText="<br><p align='center'><font size='30' face='Aston-F1'>Level Complete</font><br><font size='20'> Press Any Key To Continue<br>"+list+"</font></p>"
}
}
}
private function lazerblast():Sprite{
var laz:MovieClip=new MovieClip();
var lazc:Sprite=new Sprite();
lazer(lazc,80,5,0xFF0000);
lazc.x=-40;
lazc.y=-5;
laz.addChild(lazc);
laz.speed=4;
laz.lev=level;
laz.addEventListener(Event.ENTER_FRAME,lazerfly);
addChild(laz);
return laz;
}
private function lazerfly(event:Event):void{
if(!dead&&!pause&&!paused){
var rotatio:Number=event.target.rotation/180*Math.PI;
if(!force){
if(level>3){
event.target.x+=1.2*event.target.speed*Math.cos(rotatio);
event.target.y+=1.2*event.target.speed*Math.sin(rotatio);
}else{
event.target.x+=event.target.speed*Math.cos(rotatio);
event.target.y+=event.target.speed*Math.sin(rotatio);
}
}else{
event.target.x-=2.5*event.target.speed*Math.cos(rotatio);
event.target.y-=2.5*event.target.speed*Math.sin(rotatio);
}
if(event.target.speed==4){
var hit:Boolean=false;
var front:Boolean=false;
for(var dis:Number=-40;dis<=40;dis+=10){
if(saber.hitTestPoint(event.target.x+Math.cos(rotatio)*dis,event.target.y+Math.sin(rotatio)*dis,true)){
if(!hit){
if(dis>0){
front=true;
}
score++;
hit=true;
event.target.speed+=2;
}
}
if(!hit){
if(handle.hitTestPoint(event.target.x+Math.cos(rotatio)*dis,event.target.y+Math.sin(rotatio)*dis,true)){
if(!ghost){
hit=true;
event.target.removeEventListener(Event.ENTER_FRAME,lazerfly);
life--;
event.target.parent.removeChild(event.target as DisplayObject);
}
}
}
}
if(hit){
/*var oldrot:Number=event.target.rotation/180*Math.PI;
event.target.speed+=2;
event.target.rotation=180-event.target.rotation+90;
event.target.x+=(Math.cos(oldrot))*20;
event.target.y+=(Math.sin(oldrot))*20;*/
var oldrot:Number=event.target.rotation/180*Math.PI;
event.target.speed+=2;
event.target.x+=(Math.cos(oldrot))*20;
event.target.y+=(Math.sin(oldrot))*20;
//event.target.rotation=(light.rotation+event.target.rotation)+90;
var a:Number=light.rotation+180;
event.target.rotation=(light.rotation+180+event.target.rotation)-90;
var dx:Number=event.target.x-stage.stageWidth/2;
var dy:Number=event.target.y-stage.stageHeight/2;
dis=Math.sqrt(dx*dx+dy*dy);
var rote:Number=event.target.rotation/180*Math.PI;
var dxe:Number=event.target.x+Math.cos(rote)*10-stage.stageWidth/2;
var dye:Number=event.target.y+Math.sin(rote)*10-stage.stageHeight/2;
var dise:Number=Math.sqrt(dx*dx+dy*dy);
if(dis<dise){
event.target.rotation=(light.rotation+180+event.target.rotation)+90;
}
if(a<180){
event.target.rotation+=180;
}
if(!front){
event.target.rotation+=180;
}
}
}
if(event.target.x>465||event.target.y>465||event.target.x<0||event.target.y<0){
event.target.removeEventListener(Event.ENTER_FRAME,lazerfly);
event.target.parent.removeChild(event.target as DisplayObject);
}
if(event.target.lev!=level){
event.target.removeEventListener(Event.ENTER_FRAME,lazerfly);
event.target.parent.removeChild(event.target as DisplayObject);
}
}
}
private function continu(event:KeyboardEvent):void{
if(dead){
life=5;
dead=false;
score=0;
dragging=false;
oldscore=0;
boss.rot=0;
boss.rota=0;
boss.health=150;
reload=0;
boss.x=Math.random()*465;
boss.y=465*Math.random();
}else if(pause){
if(level<7){
pause=false;
}
}
if(event.keyCode==68){
saber.graphics.clear();
saber.filters=new Array();
lazer(saber,300,12,0x00FFFF);
}
if(level>1){
if(event.keyCode==72){
saber.graphics.clear();
saber.filters=new Array();
lazer(saber,300,12,0x00FF00);
}
}
if(level>2){
if(event.keyCode==74){
saber.graphics.clear();
saber.filters=new Array();
lazer(saber,300,12,0x990099);
}
}
if(level>3){
if(event.keyCode==83){
saber.graphics.clear();
saber.filters=new Array();
lazer(saber,300,12,0xFF0000);
}
}
if(level>4){
if(forcerecharge<0){
if(event.keyCode==70){
forcerecharge=240;
force=true;
ghosttime=120;
}
}
}
if(level>5){
if(forcerecharge<0){
if(event.keyCode==71){
forcerecharge=240;
ghost=true;
ghosttime=120;
}
}
}
if(event.keyCode==32){
paused=!paused;
setup=false;
}
}
private function unclick(event:MouseEvent):void{
if(!dead&&!pause&&!paused){
if(light.hitTestPoint(event.stageX,event.stageY,true)){
dragging=false;
}
}
}
private function click(event:MouseEvent):void{
if(!dead&&!pause&&!paused){
if(light.hitTestPoint(event.stageX,event.stageY,true)){
dragging=true;
}
}
}
private function mover(event:MouseEvent):void{
if(!dead&&!pause&&!paused){
if(!dead){
if(dragging){
light.x=event.stageX;
light.y=event.stageY;
}else{
var dx:Number=light.x-event.stageX;
var dy:Number=light.y-event.stageY;
var angle:Number=Math.atan2(dy,dx)/Math.PI*180;
light.rotation=angle+180;
}
}
}
}
private function loading(event:ProgressEvent):void{
graphics.clear();
lazer(this,465*event.bytesLoaded/event.bytesTotal,10,0xFF00FF);
}
private function doneloading(event:Event):void{
graphics.clear();
this.filters=new Array();
}
public function lazer(graphs:Sprite,xsize:Number,ysize:Number,color:Number):void{
var fillType:String = GradientType.LINEAR;
var colors:Array = [color, 0xFFFFFF];
var alphas:Array = [5, 1];
var ratios:Array = [0x00, 0xFF];
var spreadMethod:String = SpreadMethod.REFLECT;
var matr:Matrix = new Matrix();
matr.createGradientBox(xsize, ysize, 90/180*Math.PI, 0, 0);
graphs.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
graphs.graphics.drawRect(0,0,xsize,2*ysize);
graphs.filters=new Array(new GlowFilter(color,1,ysize,ysize));
}
}
}