-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.as
293 lines (251 loc) · 8.36 KB
/
Game.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.TextFormat;
import mochi.as3.*
import flash.net.*
public dynamic class Game extends MovieClip{
static var kongregate:*
static var ship:MovieClip;
static var enemyShipTimer:Timer;
static var weaponTimer:Timer;
static var scoreText:TextField;
static var score:Number = 0;
static var healthMeter:HealthMeter;
static var enemyHealthMeter:EnemyHealthMeter;
static var gameOverMenu:GameOverMenu;
static var creditsMenu:CreditsMenu;
static var startMenu:StartMenu;
static var powerUpTimer:Timer;
static var moreWingmenTimer:Timer;
static var miniBossTimer:Timer;
//static var towerBossTimer:Timer;
static var towerBossCountdown:Number;
static var bossCountdown:Number;
static var music = new BackgroundMusic();
static var musicChannel;
static var shieldSound2 = new ShieldSound2;
static var shieldChannel;
static var kills = 0;
static var misses = 0;
static var gameComplete = 0;
static var gameInitialized:Boolean = true;
public function loadAPI(){
var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;
var api_url:String = paramObj.api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf";
var request:URLRequest = new URLRequest ( api_url );
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, apiLoadComplete );
loader.load ( request );
this.addChild ( loader );
}
public function apiLoadComplete( event:Event ):void {
kongregate = event.target.content;
kongregate.services.connect();
kongregate.stats.submit("score", score);
kongregate.stats.submit("kills", kills);
kongregate.stats.submit("misses", misses);
kongregate.stats.submit("gameComplete", gameComplete);
}
public function onConnectError(status:String):void {
// handle error here...
if(!gameInitialized) {initializeGame();}
}
function Game() {
//var _mochiads_game_id:String = "dcbe9b807ff7322e";
//mochi.as3.MochiServices.connect("dcbe9b807ff7322e", root, onConnectError); // use mochi.as2.MochiServices.connect for AS2 API
//MochiAd.showPreGameAd({clip:root, id:"dcbe9b807ff7322e", res:"600x300", ad_finished: initializeGame});
//loadAPI(); // for Kongregate
//uncomment updatescore and above lines and remove the one below to turn on kong and mochi
initializeGame();
}
function initializeGame(){
Key.initialize(stage);
ship = new Ship();
ship.x = 300;
ship.y = 150;
ship.visible = false;
ship.shield.visible = false;
addChild(ship);
enemyShipTimer = new Timer(1500);
enemyShipTimer.addEventListener("timer", sendEnemy);
//enemyShipTimer.start();
miniBossTimer = new Timer(10000);
miniBossTimer.addEventListener("timer", sendMiniBoss);
//towerBossCountdown = 3;
//bossCountdown = 8;
powerUpTimer = new Timer(6000);
powerUpTimer.addEventListener("timer", sendPowerUp);
//powerUpTimer.start();
weaponTimer = new Timer(9000);
weaponTimer.addEventListener("timer", sendWeapon);
moreWingmenTimer = new Timer(25000);
moreWingmenTimer.addEventListener("timer", sendMoreWingmen);
scoreText = new TextField();
scoreText.x = 290;
scoreText.text = String(0);
scoreText.visible = false;
addChild(scoreText);
var scoreFormat = new TextFormat("Comic Sans MS", 20, 0x000000);
scoreText.defaultTextFormat = scoreFormat;
healthMeter = new HealthMeter();
healthMeter.x = 10;
healthMeter.y = 10;
addChild(healthMeter);
healthMeter.visible = false;
enemyHealthMeter = new EnemyHealthMeter();
enemyHealthMeter.x = 530;
enemyHealthMeter.y = 10;
addChild(enemyHealthMeter);
enemyHealthMeter.visible = false;
startMenu = new StartMenu();
startMenu.x = 52;
startMenu.y = 45;
addChild(startMenu);
startMenu.visible = true;
creditsMenu = new CreditsMenu();
creditsMenu.x = 52;
creditsMenu.y = 45;
addChild(creditsMenu);
creditsMenu.visible = false;
creditsMenu.closeCreditsButton.addEventListener("mouseDown", hideCredits);
startMenu.playButton.addEventListener("mouseDown", newGame);
startMenu.creditsButton.addEventListener("mouseDown", showCredits);
gameOverMenu = new GameOverMenu();
gameOverMenu.x = 80;
gameOverMenu.y = 35;
addChild(gameOverMenu);
gameOverMenu.visible = false;
gameOverMenu.playAgainButton.addEventListener("mouseDown", newGame);
resetScore();
gameInitialized = true;
}
static function updateScore(points){
score += points;
scoreText.text = String(score);
//kongregate.stats.submit("score", score);
//kongregate.stats.submit("kills", kills);
//kongregate.stats.submit("misses", misses);
//kongregate.stats.submit("gameComplete", gameComplete);
}
static function resetScore(){
score = 0;
kills = 0;
misses = 0;
gameComplete = 0;
scoreText.text = String(score);
scoreText.visible = true;
}
static function newGame(e:Event){
mochi.as3.MochiEvents.startPlay();
resetScore();
gameOverMenu.visible = false;
startMenu.visible = false;
healthMeter.visible = true;
ship.health = ship.maxHealth;
ship.shaking = false;
ship.rotation = 0;
healthMeter.bar.scaleX = 1;
ship.visible = true;
ship.x = 300;
ship.y = 150;
ship.addEventListener("enterFrame", ship.move);
enemyShipTimer.start();
miniBossTimer.start();
//towerBossTimer.start();
towerBossCountdown = 3;
bossCountdown = 8;
powerUpTimer.start();
weaponTimer.start();
moreWingmenTimer.start();
Wingman.maxWingmen = 2;
musicChannel = music.play(0,1000);
}
static function gameOver(){
var efficiency;
var effBonus;
Game.ship.visible = false;
Game.enemyHealthMeter.visible = false;
gameOverMenu.visible = true;
gameOverMenu.baseScore.text = score.toString();
gameOverMenu.kills.text = kills.toString();
gameOverMenu.killBonus.text = (kills*20).toString();
efficiency = Math.floor(100*kills/(misses + kills));
effBonus = Math.round(10000 / (100-efficiency)) / 100;
if (effBonus == Infinity) { effBonus = 10; }
gameOverMenu.efficiency.text = efficiency.toString();
gameOverMenu.multiplier.text = effBonus.toString();
score += (kills * 50);
score = Math.floor(score*effBonus);
gameOverMenu.finalScore.text = score.toString();
for(var i in EnemyShip.list){
EnemyShip.list[i].destroy();
}
while (Wingman.list.length > 0) {
for(var j in Wingman.list){
Wingman.list[j].kill();
//Wingman.list = [];
}
}
enemyShipTimer.stop();
miniBossTimer.stop();
//towerBossTimer.stop();
powerUpTimer.stop();
weaponTimer.stop();
musicChannel.stop();
updateScore(0); // reports to Kong
}
function showCredits(e:Event) {
creditsMenu.visible = true;
}
function hideCredits(e:Event) {
creditsMenu.visible = false;
}
function sendEnemy(e:Event){
var enemy = new EnemyShip();
stage.addChild(enemy);
}
function sendMiniBoss(e:Event){ //Also handles other bosses
miniBossTimer.stop(); //restart it when it's dead
if(bossCountdown == 0) {
var boss = new Boss();
stage.addChild(boss);
bossCountdown = 8;
}else{
bossCountdown -= 1;
if(towerBossCountdown == 0){
sendTowerBoss();
if(bossCountdown <= 3) {
var miniBossToo = new MiniBoss(); // Yes, both at once!
stage.addChild(miniBossToo);
}
towerBossCountdown = 3;
} else{
towerBossCountdown -= 1;
var miniBoss = new MiniBoss();
stage.addChild(miniBoss);
}
}
}
function sendTowerBoss(){
var towerBoss = new TowerBoss;
stage.addChild(towerBoss);
}
function sendPowerUp(e:Event){
var powerUp = new PowerUp();
stage.addChild(powerUp);
}
function sendWeapon(e:Event){
var weapon = new Weapon();
stage.addChild(weapon);
}
function sendMoreWingmen(e:Event){
var moreWingmen = new MoreWingmen();
stage.addChild(moreWingmen);
}
}
}