-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanets.js
100 lines (92 loc) · 3.42 KB
/
planets.js
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
const SMALL_PLANET_DATA = {
blocks: [
".", ".", ".", ".", "x", "x", ".", ".", ".", ".",
".", ".", "x", "x", "x", "x", "x", "x", ".", ".",
".", "x", "x", "x", "x", "x", "x", "x", "x", ".",
".", "x", "x", "x", "x", "x", "x", "x", "x", ".",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
".", "x", "x", "x", "x", "x", "x", "x", "x", ".",
".", "x", "x", "x", "x", "x", "x", "x", "x", ".",
".", ".", "x", "x", "x", "x", "x", "x", ".", ".",
".", ".", ".", ".", "x", "x", ".", ".", ".", ".",
],
width: 10,
height: 10,
health: 400,
malusForFriendlyFire: 15
};
const MEDIUM_PLANET_DATA = {
blocks: [
".", ".", ".", ".", ".", ".", "x", "x", "x", "x", ".", ".", ".", ".", ".", ".",
".", ".", ".", ".", "x", "x", "x", "x", "x", "x", "x", "x", ".", ".", ".", ".",
".", ".", ".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".", ".", ".",
".", ".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".", ".",
".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".",
".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".",
".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".",
".", ".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".", ".",
".", ".", ".", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", ".", ".", ".",
".", ".", ".", ".", "x", "x", "x", "x", "x", "x", "x", "x", ".", ".", ".", ".",
".", ".", ".", ".", ".", ".", "x", "x", "x", "x", ".", ".", ".", ".", ".", ".",
],
width: 16,
height: 16,
health: 800,
malusForFriendlyFire: 25
};
const MAX_PLANET_PIXEL_SIZE = 16*8;
const COLOURS = ["red", "sand", "blue", "ice"];
class Planets {
constructor() {
this.planets = [];
var genNum = Utils.randInt(10, 20);
for (var i=0; i<genNum; i++) {
var planetData = (Utils.randInt(0, 1) === 0) ? SMALL_PLANET_DATA : MEDIUM_PLANET_DATA;
var colour = COLOURS[Math.floor(Math.random()*COLOURS.length)];
this.planets.push(
new Planet("planet_"+i,
{
x: Utils.randInt(0, canvas.width-MAX_PLANET_PIXEL_SIZE),
y: Utils.randInt(0, canvas.height-MAX_PLANET_PIXEL_SIZE)
},
colour,
planetData));
this.planets[this.planets.length-1].init();
}
}
getRemaining() {
return this.planets.length;
}
getPlanet(index) {
return this.planets[index];
}
update(delta) {
var p = this.getRemaining();
if (p<=0) return; // TODO: Game over
while (p--) {
var planet = this.planets[p];
if (planet) {
planet.update(delta);
if (planet.destroyed) {
console.log("Destroyed planet ", planet);
this.planets.splice(p, 1);
SOUND_MANAGER.play("explosion");
}
}
}
}
render() {
for (var p = 0; p < this.planets.length; p++) {
var planet = this.planets[p];
if (planet && !planet.destroyed) {
planet.render();
}
}
}
}