-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWingman.as
100 lines (94 loc) · 2.36 KB
/
Wingman.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.globalization.NumberFormatter;
public class Wingman extends MovieClip {
static var list:Array = new Array();
static var maxWingmen:Number = 2;
static var maxWingmenLimit:Number = 6;
var shootTimer:Timer;
var health:Number;
var position:Number;
var yOffset:Number;
var nextY:Number;
var nextX:Number;
function Wingman() {
this.position = firstOpen();
yOffset = Math.floor((1+ position) / 2) * 15 + 7;
if (position % 2) { yOffset *= -1; }
list.push(this);
addEventListener("enterFrame", enterFrame);
shootTimer = new Timer(600);
shootTimer.addEventListener("timer", shoot);
shootTimer.start();
health = 1;
}
function firstOpen() { // finds an opening in wingman formation
var isTaken:Array = [];
var f:Number;
for (var i in list){
isTaken.push(list[i].position);
}
isTaken.sort();
f = 1;
for (var j in isTaken) {
if (f == isTaken[j]) {
f+=1;
}
}
return f;
}
function enterFrame(e:Event){
if (nextX) { this.x = nextX; } // make wingmen lag a bit
if (nextY) { this.y = nextY; }
nextX = Game.ship.x;
nextY = Game.ship.y + yOffset;
for (var i in EnemyShip.list){
if(this.hitTestObject(EnemyShip.list[i])){
takeDamage(3);
EnemyShip.list[i].takeDamage(3);
Game.updateScore(-25);
}
}
}
function takeDamage(d){
health -= d;
if(health <= 0) {
kill();
}
}
function kill(){
shootTimer.stop();
shootTimer.removeEventListener("timer",shoot);
var s = new ExplosionSound();
s.play();
var explosion = new Explosion();
stage.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
removeEventListener("enterFrame", enterFrame);
stage.removeChild(this);
for(var i in list){
if(list[i] == this){
var j = list.indexOf(list[i]);
delete list[i];
list.splice(j,1);
}
}
}
function shoot(e:Event){
var b = new Bullet();
stage.addChild(b);
b.x = this.x+50;
b.y = this.y+3;
}
function alert(floaty) {
var alert = new FloatyAlert();
alert.x = Game.ship.x;
alert.y = Game.ship.y - 25;
alert.floatyText.text = floaty;
stage.addChild(alert);
}
}
}