-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.js
159 lines (146 loc) · 5.38 KB
/
asteroid.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
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
;(function(exports) {
function Asteroid(game, center, size) {
this.game = game;
this.color = game.COLORS.TEAL;
this.isEnemy = true;
this.size = size;
this.angle = game.validAngle(); // 'Good' random angle.
this.speed = (4 - size) / 2; // Inversely proportional to size.
// Set points and line segments about asteroid center.
this.center = center;
this.resetPoints();
this.resetLineSegments();
}
Asteroid.prototype = {
/**
* Updates the position of the asteroid.
*/
update: function() {
// Move center by speed and angle.
this.game.trig.translatePoint(this.center, this.speed, this.angle);
// Reset points and lines about new center.
this.resetPoints();
this.resetLineSegments();
},
/**
* Draws the asteroid on the screen.
*/
draw: function(screen) {
screen.lineWidth = 2;
screen.strokeStyle = this.color;
screen.fillStyle = this.game.color;
screen.beginPath();
screen.moveTo(this.points[0].x, this.points[0].y);
for (var i = 1; i < this.points.length; i++) {
screen.lineTo(this.points[i].x, this.points[i].y);
}
screen.closePath();
screen.stroke();
screen.fill();
},
/**
* Creates three asteroids of size n-1 where n is the size of the asteroid
* being destroyed, and adds them to the list of game bodies.
*/
spawn: function() {
for (var i = 0; i < 3; i++) {
this.game.addBody(new Asteroid(this.game, { x: this.center.x, y: this.center.y },
this.size - 1));
}
},
/**
* Handles events occurring upon the destruction of the asteroid.
*/
die: function() {
// Spawn smaller asteroids if size 2 or 3
if (this.size > 1) {
this.spawn();
}
// Add points to the game score.
switch(this.size) {
case 3:
this.game.addToScore(20);
break;
case 2:
this.game.addToScore(50);
break;
case 1:
this.game.addToScore(100);
break;
}
// Remove the dead asteroid from the list of game bodies.
this.game.removeBody(this);
},
/**
* Resets the points of the asteroid relative to its center.
*/
resetPoints: function() {
switch(this.size) {
case 3:
this.points = [
{ x: this.center.x - 7.5, y: this.center.y - 22.5 },
{ x: this.center.x - 22.5, y: this.center.y - 12 },
{ x: this.center.x - 18, y: this.center.y - 3 },
{ x: this.center.x - 25.5, y: this.center.y },
{ x: this.center.x - 24, y: this.center.y + 15 },
{ x: this.center.x - 7.5, y: this.center.y + 22.5 },
{ x: this.center.x , y: this.center.y + 15 },
{ x: this.center.x + 7.5, y: this.center.y + 22.5 },
{ x: this.center.x + 15, y: this.center.y + 12 },
{ x: this.center.x + 12, y: this.center.y + 4.5 },
{ x: this.center.x + 22.5, y: this.center.y },
{ x: this.center.x + 12, y: this.center.y - 21 }
];
break;
case 2:
this.points = [
{ x: this.center.x - 5, y: this.center.y - 15 },
{ x: this.center.x - 15, y: this.center.y - 8 },
{ x: this.center.x - 12, y: this.center.y - 2 },
{ x: this.center.x - 17, y: this.center.y },
{ x: this.center.x - 16, y: this.center.y + 10 },
{ x: this.center.x - 5, y: this.center.y + 15 },
{ x: this.center.x , y: this.center.y + 10 },
{ x: this.center.x + 5, y: this.center.y + 15 },
{ x: this.center.x + 10, y: this.center.y + 8 },
{ x: this.center.x + 8, y: this.center.y + 3 },
{ x: this.center.x + 15, y: this.center.y },
{ x: this.center.x + 8, y: this.center.y - 14 }
];
break;
case 1:
this.points = [
{ x: this.center.x - 3.75, y: this.center.y - 11.25 },
{ x: this.center.x - 11.25, y: this.center.y - 6.0 },
{ x: this.center.x - 9.0, y: this.center.y - 1.5 },
{ x: this.center.x - 12.75, y: this.center.y },
{ x: this.center.x - 12, y: this.center.y + 7.5 },
{ x: this.center.x - 3.75, y: this.center.y + 11.25 },
{ x: this.center.x , y: this.center.y + 7.5 },
{ x: this.center.x + 3.75, y: this.center.y + 11.25 },
{ x: this.center.x + 7.5, y: this.center.y + 6 },
{ x: this.center.x + 6, y: this.center.y + 2.25 },
{ x: this.center.x + 11.25, y: this.center.y },
{ x: this.center.x + 6, y: this.center.y - 10.5 }
];
break;
}
},
/**
* Resets the line segments of the asteroid, relative to its points.
* Used in collision detection.
*/
resetLineSegments: function() {
this.lineSegments = [];
var self = this;
this.points.forEach(function(point, i, points) {
if (i !== points.length - 1) {
self.lineSegments.push({ p1: points[i], p2: points[i + 1] });
} else {
self.lineSegments.push({ p1: points[i], p2: points[0] });
}
});
}
};
exports.Asteroid = Asteroid;
})(this);