-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalien.js
181 lines (159 loc) · 5.22 KB
/
alien.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
;(function(exports) {
function Alien(game, size) {
this.game = game;
this.color = game.COLORS.YELLOW;
this.isEnemy = true;
this.size = size;
this.angle = game.validAngle();
this.speed = 3 - size;
// Set points and lines about alien center.
this.center = game.randomPoint();
this.resetPoints();
this.resetLineSegments();
// Record the last occurrence of shooting.
this.lastShot = 0;
}
Alien.prototype = {
/* Limit constants. */
FIRING_LIMIT: 3000,
FIRING_THRESHOLD: 0.3,
/**
* Updates the position of the alien.
*/
update: function() {
// Move center by speed and angle.
this.game.trig.translatePoint(this.center, this.speed, this.angle);
// Reset points and lines about the new center.
this.resetPoints();
this.resetLineSegments();
// Shoot periodically.
if (this.canShoot() && this.willShoot()) {
this.shoot();
}
},
/**
* Draws the alien 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();
screen.lineWidth = 1;
screen.beginPath();
screen.moveTo(this.points[0].x, this.points[0].y);
screen.lineTo(this.points[3].x, this.points[3].y);
screen.closePath();
screen.stroke();
screen.lineWidth = 1;
screen.beginPath();
screen.moveTo(this.points[4].x, this.points[4].y);
screen.lineTo(this.points[7].x, this.points[7].y);
screen.closePath();
screen.stroke();
},
/**
* Handles events occurring upon the death of the alien.
*/
die: function() {
// Add points to the game score.
switch(this.size) {
case 2:
this.game.addToScore(200);
break;
case 1:
this.game.addToScore(1000);
break;
}
// Remove the dead asteroid from the list of game bodies.
this.game.removeBody(this);
},
/**
* Resets the points of the alien relative to its center.
*/
resetPoints: function() {
switch(this.size) {
case 2:
this.points = [
{ x: this.center.x - 22.5, y: this.center.y },
{ x: this.center.x - 11.25, y: this.center.y + 7.5 },
{ x: this.center.x + 11.25, y: this.center.y + 7.5},
{ x: this.center.x + 22.5, y: this.center.y },
{ x: this.center.x + 11.25, y: this.center.y - 7.5 },
{ x: this.center.x + 7.5, y: this.center.y - 15 },
{ x: this.center.x - 7.5, y: this.center.y - 15 },
{ x: this.center.x - 11.25, y: this.center.y - 7.5 }
];
break;
case 1:
this.points = [
{ x: this.center.x - 18, y: this.center.y },
{ x: this.center.x - 9, y: this.center.y + 6 },
{ x: this.center.x + 9, y: this.center.y + 6 },
{ x: this.center.x + 18, y: this.center.y },
{ x: this.center.x + 9, y: this.center.y - 6 },
{ x: this.center.x + 6, y: this.center.y - 12 },
{ x: this.center.x - 6, y: this.center.y - 12 },
{ x: this.center.x - 9, y: this.center.y - 6 }
];
break;
}
},
/**
* Resets the line segments of the alien 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] });
}
});
},
/**
* Adds a bullet to the list of game bodies.
*/
shoot: function() {
// Default to a random angle.
var angle = this.game.randomAngle();
// Small aliens shoot toward the ship center.
if (this.size === 1) {
// Shooting error is proportional to the level of the game.
var epsilon = Math.PI / (this.game.level * 6);
epsilon = Math.random() >= 0.5 ? epsilon : -epsilon;
angle = this.game.trig.angleToPoint(this.center, this.game.ship.center)
+ epsilon;
}
// Create a bullet at the center of the alien with the specified angle.
var bullet = new Bullet({ x: this.center.x, y: this.center.y }, angle, this);
this.game.addBody(bullet);
// Update last occurrence of shooting.
this.lastShot = Date.now();
},
/**
* Returns true if at least FIRING_LIMIT milliseconds
* have passed since the last shot.
*/
canShoot: function() {
return Date.now() - this.lastShot >= this.FIRING_LIMIT;
},
/**
* Returns true with probability FIRING_THRESHOLD.
*/
willShoot: function() {
return Math.random() >= this.FIRING_THRESHOLD;
}
};
exports.Alien = Alien;
})(this);