Repository navigation
Expand file tree
/
Copy pathLevels.js
More file actions
594 lines (481 loc) · 17.5 KB
/
Copy pathLevels.js
File metadata and controls
594 lines (481 loc) · 17.5 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*********************************************************************
* Level - base class for a level
* Copyright (c) 2012 Steve Purkis
*
* Note: always use game.setTimeout to avoid random timeouts hanging
* around if the user switches levels.
*
* var l = new Level( game );
*/
require('asteroidUtils.js');
var gameLevels = [];
function Level(game) {
if (game) return this.initialize(game);
return this;
}
Level.images = []; // populate to auto-load images
Level.prototype.initialize = function(game) {
this.game = game;
var canvas = this.game.ctx.canvas;
this.maxX = canvas.width;
this.maxY = canvas.height;
this.canvas = canvas;
this.backgroundColor = "#fff";
this.images = {};
this.asteroids = [];
this.planets = [];
this.suns = [];
this.ships = [];
this.weapons = {};
this.respawn = {};
this.wrapX = false;
this.wrapY = false;
return this;
}
/******************************************************************************
* TrainingLevel: big planet out of field of view with falling asteroids.
*/
function TrainingLevel(game) {
if (game) return this.initialize(game);
return this;
}
TrainingLevel.inheritsFrom( Level );
TrainingLevel.description = "Training Level - learn how to fly!";
TrainingLevel.images = [ "planet.png", "planet-80px-green.png" ];
gameLevels.push(TrainingLevel);
TrainingLevel.prototype.initialize = function(game) {
TrainingLevel.prototype.parent.initialize.call(this, game);
this.wrapX = false;
this.wrapY = false;
var maxX = this.maxX;
var maxY = this.maxY;
var canvas = this.game.ctx.canvas;
this.planets.push(
{x: 1/2*maxX, y: 1/2*maxY, mass: 100, radius: 50, damage: 5, stationary: true, image_src: "planet.png" }
, {x: 40, y: 40, mass: 5, radius: 20, vX: 2, vY: 0, image_src:"planet-80px-green.png"}
, {x: maxX-40, y: maxY-40, mass: 5, radius: 20, vX: -2, vY: 0, image_src:"planet-80px-green.png"}
);
this.ships.push(
{x: 4/5*canvas.width, y: 1/3*canvas.height}
);
this.asteroids.push(
{x: 1/10*maxX, y: 6/10*maxY, mass: 0.5, radius: 14, vX: 0, vY: 0, spawn: 1, health: 1},
{x: 1/10*maxX, y: 2/10*maxY, mass: 1, radius: 5, vX: 0, vY: -0.1, spawn: 3 },
{x: 5/10*maxX, y: 1/10*maxY, mass: 2, radius: 6, vX: -0.2, vY: 0.25, spawn: 4 },
{x: 5/10*maxX, y: 2/10*maxY, mass: 3, radius: 8, vX: -0.22, vY: 0.2, spawn: 7 }
);
}
/******************************************************************************
* Level0: big planet out of field of view with falling asteroids.
*/
function Level0(game) {
if (game) return this.initialize(game);
return this;
}
Level0.inheritsFrom( Level );
Level0.description = "Level 0 - Five planets";
Level0.images = [ "planet.png", "planet-80px-green.png" ];
gameLevels.push(Level0);
Level0.prototype.initialize = function(game) {
Level0.prototype.parent.initialize.call(this, game);
this.maxX = 1000;
this.maxY = 700;
this.wrapX = false;
this.wrapY = false;
var maxX = this.maxX;
var maxY = this.maxY;
var canvas = this.game.ctx.canvas;
this.planets.push(
{x: 1/2*maxX, y: 1/2*maxY, mass: 500, radius: 100, damage: 5, stationary: true, image_src: "planet.png" }
, {x: 40, y: 40, mass: 5, radius: 20, stationary: true, image_src:"planet-80px-green.png"}
, {x: maxX-40, y: maxY-40, mass: 5, radius: 20, stationary: true, image_src:"planet-80px-green.png"}
, {x: maxX-40, y: 40, mass: 5, radius: 20, stationary: true, image_src:"planet-80px-green.png"}
, {x: 40, y: maxY-40, mass: 5, radius: 20, stationary: true, image_src:"planet-80px-green.png"}
);
this.ships.push(
{x: 4/5*canvas.width, y: 1/3*canvas.height}
, {x: 1/5*maxX, y: 1/3*maxY, color: {r: 0,g:100,b:100}, healthX: 10}
);
this.asteroids.push(
{x: 1/10*maxX, y: 6/10*maxY, mass: 0.5, radius: 14, vX: 0, vY: 0, spawn: 3, health: 1},
{x: 1/10*maxX, y: 2/10*maxY, mass: 1, radius: 5, vX: 0, vY: -0.1 },
{x: 5/10*maxX, y: 1/10*maxY, mass: 2, radius: 6, vX: -0.2, vY: 0.25 },
{x: 5/10*maxX, y: 2/10*maxY, mass: 3, radius: 8, vX: -0.22, vY: 0.2 }
);
}
/******************************************************************************
* Level1: big planet out of field of view with falling asteroids.
*/
function Level1(game) {
if (game) return this.initialize(game);
return this;
}
Level1.inheritsFrom( Level );
Level1.description = "Level 1 - Planetside, falling asteroids";
Level1.images = [ "planet-2000px.png", "planet.png", "planet-40px-brown.png" ];
gameLevels.push(Level1);
Level1.prototype.initialize = function(game) {
Level1.prototype.parent.initialize.call(this, game);
this.maxX = 1500;
this.maxY = 700;
this.wrapX = false;
this.wrapY = false;
// this.backgroundColor = "black";
var maxX = this.maxX;
var maxY = this.maxY;
var canvas = this.game.ctx.canvas;
this.planets.push(
{x: 1/2*maxX, y: canvas.height + 900, mass: 5000, radius: 1000, stationary: true, image_src: "planet-2000px.png"}
);
this.ships.push(
{x: 4/5*maxX, y: 2/3*canvas.height, color: {r:0,g:0,b:0}}
, {x: 1/5*maxX, y: 2/3*canvas.height, color: {r:0,g:100,b:100}, healthX: 10}
);
/**/
this.asteroids.push(
{x: 1/10*maxX, y: 6/10*maxY, mass: 0.5, radius: 14, vX: 0, vY: 0, spawn: 3, health: 1, image_src: "planet-40px-brown.png" },
{x: 1/10*maxX, y: 2/10*maxY, mass: 1, radius: 5, vX: 0, vY: -0.1, image_src: "planet-40px-brown.png" },
{x: 5/10*maxX, y: 1/10*maxY, mass: 2, radius: 6, vX: -0.2, vY: 0.25, image_src: "planet-40px-brown.png" },
{x: 5/10*maxX, y: 2/10*maxY, mass: 3, radius: 8, vX: -0.22, vY: 0.2, image_src: "planet-40px-brown.png" },
{x: 6/10*maxX, y: 8/10*maxY, mass: 2, radius: 6, vX: -0.4, vY: 0.1, image_src: "planet-40px-brown.png" },
{x: 6/10*maxX, y: 9/10*maxY, mass: 3, radius: 8, vX: 0.5, vY: -0.5, image_src: "planet-40px-brown.png" },
{x: 9/10*maxX, y: 8/10*maxY, mass: 2, radius: 6, vX: 0.6, vY: 0.4, image_src: "planet-40px-brown.png" },
{x: 9/10*maxX, y: 9/10*maxY, mass: 3, radius: 8, vX: 0.7, vY: 0.6, image_src: "planet-40px-brown.png" },
{x: 3/10*maxX, y: 1/10*maxY, mass: 2, radius: 6, vX: 0.8, vY: -0.2, image_src: "planet-40px-brown.png" },
{x: 3/10*maxX, y: 2/10*maxY, mass: 3, radius: 8, vX: 0.9, vY: -0.1, image_src: "planet-40px-brown.png" }
);
/**/
// spawn falling asteroid
var self = this;
var spawnAsteroid = function() {
var radius = getRandomInt(3,7);
var negative_vX = -1 * getRandomInt(0,1);
var asteroid = new Asteroid(self.game, {
radius: radius,
x: getRandomInt(0,maxX),
y: -radius,
vX: negative_vX * Math.random(),
vY: getRandomInt(0,10)/10,
health: 2,
spawn: 0,
image: self.game.images["planet-40px-brown.png"]
});
self.game.addObject(asteroid);
self.spawnTimeout = self.game.setTimeout(spawnAsteroid, 1000);
}
spawnAsteroid();
}
/******************************************************************************
* Level2: random asteroid field with 2 ships
*/
function Level2(game) {
if (game) return this.initialize(game);
return this;
}
Level2.inheritsFrom( Level );
Level2.description = "Level 2 - Random asteroid field, moving planets, wrapped map";
Level2.images = [ "planet.png", "planet-40px-brown.png", "planet-80px-green.png" ];
gameLevels.push(Level2);
Level2.prototype.initialize = function(game) {
Level2.prototype.parent.initialize.call(this, game);
//this.maxX = 500;
//this.maxy = 500;
this.wrapX = true;
this.wrapY = true;
var maxX = this.maxX;
var maxY = this.maxY;
this.planets.push(
{x: 3/4*maxX, y: 1/4*maxY, mass: 195, radius: 65, vX: -0.5, vY: 0, image_src: "planet-80px-green.png"}
, {x: 1/5*maxX, y: 2/5*maxY, mass: 15, radius: 15, vX: -0.5, vY: 0.5, image_src: "planet-40px-brown.png" }
, {x: 5/7*maxX, y: 4/5*maxY, mass: 30, radius: 40, image_src: "planet.png"}
, {x: 1/2*maxX-60, y: 1/2*maxY, mass: 15, radius: 15, vY: 0.5, image_src: "planet-40px-brown.png"}
);
this.ships.push(
{x: 4/5*maxX, y: 2/3*maxY}
, {x: 1/5*maxX, y: 2/3*maxY, color: {r: 0,g:100,b:100}, healthX: 10}
);
/**/
for (var i=50; i<this.maxX; i+= getRandomInt(80,120)) {
for (var j=50; j<this.maxY; j+= getRandomInt(80,120)) {
var asteroid = new Asteroid(this, {
x: i + getRandomInt(0, 80),
y: j + getRandomInt(0, 80),
mass: getRandomInt(1, 3),
radius: getRandomInt(3, 10),
vX: Math.random(),
vY: Math.random(),
});
// vary the velocities:
if (i%2) asteroid.vX = -asteroid.vX;
if (j%2) asteroid.vY = -asteroid.vY;
this.asteroids.push(asteroid);
}
}
/**/
}
/******************************************************************************
* Level3: basic asteroid field with 1 ship
*/
function Level3(game) {
if (game) return this.initialize(game);
return this;
}
Level3.inheritsFrom( Level );
Level3.description = "Level 3 - Basic asteroid field, wrapped map";
gameLevels.push(Level3);
Level3.prototype.initialize = function(game) {
Level3.prototype.parent.initialize.call(this, game);
//this.maxX = 500;
//this.maxy = 500;
this.wrapX = true;
this.wrapY = true;
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 4/5*maxX, y: 2/3*maxY}
);
/**/
this.asteroids.push(
{x: 1/10*maxX, y: 6/10*maxY, mass: 0.5, radius: 14, vX: 0, vY: 0, spawn: 3, health: 1 },
{x: 1/10*maxX, y: 2/10*maxY, mass: 1, radius: 5, vX: 0, vY: -0.1 },
{x: 5/10*maxX, y: 1/10*maxY, mass: 2, radius: 6, vX: -0.2, vY: 0.25 },
{x: 5/10*maxX, y: 2/10*maxY, mass: 3, radius: 8, vX: -0.22, vY: 0.2 },
{x: 6/10*maxX, y: 8/10*maxY, mass: 2, radius: 6, vX: -0.4, vY: 0.1 },
{x: 6/10*maxX, y: 9/10*maxY, mass: 3, radius: 8, vX: 0.5, vY: -0.5 },
{x: 9/10*maxX, y: 8/10*maxY, mass: 2, radius: 6, vX: 0.6, vY: 0.4 },
{x: 9/10*maxX, y: 9/10*maxY, mass: 3, radius: 8, vX: 0.7, vY: 0.6 },
{x: 3/10*maxX, y: 1/10*maxY, mass: 2, radius: 6, vX: 0.8, vY: -0.2 },
{x: 3/10*maxX, y: 2/10*maxY, mass: 3, radius: 8, vX: 0.9, vY: -0.1 }
);
/**/
}
/******************************************************************************
* Level4: horizontal scroller
*/
function Level4(game) {
if (game) return this.initialize(game);
return this;
}
Level4.inheritsFrom( Level );
Level4.description = "Level 4 - horizontal scroller, bad guy at end";
Level4.images = [ "planet.png", "planet-40px-brown.png", "planet-80px-green.png" ];
gameLevels.push(Level4);
Level4.prototype.initialize = function(game) {
Level4.prototype.parent.initialize.call(this, game);
this.maxX = 2000;
this.maxY = this.canvas.height;
this.wrapX = false;
this.wrapY = false;
this.backgroundColor = "#000";
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 1/10*maxX, y: 1/2*maxY, color: {r: 220, g:220, b:100}}
);
this.planets.push(
{x: 1/5*maxX, y: 2/3*maxY, mass: 100, radius: 60, damage: 5, stationary: true, image_src: "planet.png" }
, {x: 7/10*maxX, y: 1/2*maxY, mass: 400, radius: 100, stationary: true, image_src: "planet-80px-green.png"}
);
/**/
for (var i=50; i<this.maxX; i+= getRandomInt(120,220)) {
for (var j=50; j<this.maxY; j+= getRandomInt(80,120)) {
var asteroid = new Asteroid(this, {
x: i + getRandomInt(0, 80),
y: j + getRandomInt(0, 80),
mass: getRandomInt(1, 3),
radius: getRandomInt(3, 10),
vX: -Math.random(), // always come from the right.
vY: Math.random(),
image: this.game.images["planet-40px-brown.png"]
});
// vary the velocities:
if (j%2) asteroid.vY = -asteroid.vY;
this.asteroids.push(asteroid);
}
}
// spawn falling asteroid
var self = this;
var spawnAsteroid = function() {
var radius = getRandomInt(3,7);
var negative_vY = -1 * getRandomInt(0,1);
var offscreenX = self.game.viewOffset.x + self.canvas.width - radius;
var asteroid = new Asteroid(self.game, {
radius: radius,
x: getRandomInt(offscreenX, maxX),
y: getRandomInt(0, self.maxY),
vX: -1,
vY: negative_vY * Math.random(),
health: 2,
spawn: 0,
image: self.game.images["planet-40px-brown.png"]
});
self.game.addObject(asteroid);
self.spawnTimeout = self.game.setTimeout(spawnAsteroid, 1000);
}
spawnAsteroid();
var spawnBadGuy = function() {
var offset = self.game.viewOffset;
if (self.game.ship.x > 4/5*self.maxX) {
var badGuy = new ComputerShip(self.game, {
x: 9/10*maxX,
y: -14,
color: {r: 200,g:225,b:225},
healthX: 10
});
self.game.addObject(badGuy);
return; // only spawn once
}
self.badGuyTimeout = self.game.setTimeout(spawnBadGuy, 1500);
}
spawnBadGuy();
}
/******************************************************************************
* Level5: blank
*/
function Level5(game) {
if (game) return this.initialize(game);
return this;
}
Level5.inheritsFrom( Level );
Level5.description = "Level 5 - blank, unwrapped for demo";
Level5.images = [ "planet.png" ];
gameLevels.push(Level5);
Level5.prototype.initialize = function(game) {
Level5.prototype.parent.initialize.call(this, game);
this.maxX = this.canvas.width;
this.maxY = this.canvas.height;
this.wrapX = false;
this.wrapY = false;
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 1/10*maxX, y: 1/2*maxY}
);
}
/******************************************************************************
* Level6b: textures made easy
*/
function Level6b(game) {
if (game) return this.initialize(game);
return this;
}
Level6b.inheritsFrom( Level );
Level6b.description = "Level 6b - textures made easy";
Level6b.images = [ "chainsaw-guy.jpg" ];
gameLevels.push(Level6b);
Level6b.prototype.initialize = function(game) {
Level6b.prototype.parent.initialize.call(this, game);
this.maxX = this.canvas.width;
this.maxY = this.canvas.height;
this.wrapX = true;
this.wrapY = true;
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 1/10*maxX, y: 1/3*maxY}
);
this.planets.push(
{x: 5/7*maxX, y: 1/4*maxY, mass: 5, radius: 40, vX: -0.1, vY: 0, image_src: "chainsaw-guy.jpg"},
{x: 3/7*maxX, y: 3/4*maxY, mass: 5, radius: 45, vX: +0.1, vY: 0 }
);
}
/******************************************************************************
* Level6: planets, gravity & bounce
*/
function Level6(game) {
if (game) return this.initialize(game);
return this;
}
Level6.inheritsFrom( Level );
Level6.description = "Level 6 - planets, gravity & bounce";
Level6.images = [ "chainsaw-guy.jpg", "chainsaw-girl.jpg" ];
gameLevels.push(Level6);
Level6.prototype.initialize = function(game) {
Level6.prototype.parent.initialize.call(this, game);
this.maxX = this.canvas.width;
this.maxY = this.canvas.height;
this.wrapX = true;
this.wrapY = true;
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 1/10*maxX, y: 1/2*maxY}
);
this.planets.push(
//{x: 3/4*maxX, y: 3/4*maxY, mass: 195, radius: 65, vX: -0, vY: 0, image_src: "planet.png"},
{x: 5/7*maxX, y: 1/4*maxY, mass: 295, radius: 40, vX: -0, vY: 0, image_src: "chainsaw-guy.jpg"},
{x: 6/7*maxX, y: 3/4*maxY, mass: 100, radius: 85, vX: -0, vY: 0, image_src: "chainsaw-girl.jpg"}
);
}
/******************************************************************************
* Level7: hairballs & chainsaws?
*/
function Level7(game) {
if (game) return this.initialize(game);
return this;
}
Level7.inheritsFrom( Level );
Level7.description = "Level 7 - hairballs and chainsaws";
Level7.images = [ "hairball-145px.jpg", "planet.png", "chainsaw-guy.jpg", "chainsaw-girl.jpg" ];
gameLevels.push(Level7);
Level7.prototype.initialize = function(game) {
Level7.prototype.parent.initialize.call(this, game);
this.maxX = this.canvas.width + 200;
this.maxY = this.canvas.height;
this.wrapX = true;
this.wrapY = false;
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 1/10*maxX, y: 1/2*maxY}
);
this.planets.push(
//{x: 3/4*maxX, y: 3/4*maxY, mass: 195, radius: 65, vX: -0, vY: 0, image_src: "planet.png"},
{x: 1/7*maxX, y: 1/4*maxY, mass: 195, radius: 40, vX: -0, vY: 0, image_src: "chainsaw-guy.jpg"},
{x: 6/7*maxX, y: 3/4*maxY, mass: 295, radius: 85, vX: -0, vY: 0, image_src: "chainsaw-girl.jpg"}
);
// spawn falling asteroid
var self = this;
var spawnAsteroid = function() {
var radius = getRandomInt(15,40);
var negative_vX = -1 * getRandomInt(0,1);
var asteroid = new Asteroid(self.game, {
radius: radius,
x: getRandomInt(0,maxX),
y: -radius,
vX: negative_vX * Math.random(),
vY: getRandomInt(0,10)/10, // always falling down
health: getRandomInt(10, 30),
image: self.game.images["hairball-145px.jpg"],
});
if (self.game.objects.length < 50) {
self.game.addObject(asteroid);
}
self.spawnTimeout = self.game.setTimeout(spawnAsteroid, 1000);
}
spawnAsteroid();
}
/******************************************************************************
* Level8: Lone Enemy
*/
function Level8(game) {
if (game) return this.initialize(game);
return this;
}
Level8.inheritsFrom( Level );
Level8.description = "Level 8 - Lone enemy";
Level8.images = [ "planet.png" ];
gameLevels.push(Level8);
Level8.prototype.initialize = function(game) {
Level8.prototype.parent.initialize.call(this, game);
this.maxX = this.canvas.width;
this.maxY = this.canvas.height;
this.wrapX = true;
this.wrapY = true;
var maxX = this.maxX;
var maxY = this.maxY;
this.ships.push(
{x: 1/10*maxX, y: 1/2*maxY}
, {x: 1/5*maxX, y: 1/3*maxY, color: {r: 150,g:50,b:50}, healthX: 10}
);
}