-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.js
45 lines (36 loc) · 1.15 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
(function(root) {
var Asteroids = root.Asteroids = (root.Asteroids || {});
var Asteroid = Asteroids.Asteroid = function(pos, vel, direction){
Asteroids.MovingObject.call(this, pos, vel, direction, randomRadius(),
Asteroid.COLOR);
};
Asteroid.COLOR = 'grey';
Asteroid.RADIUS = 50;
Asteroid.inherits(Asteroids.MovingObject);
Asteroid.randomAsteroid = function(dimX, dimY, ship) {
var asteroid = new Asteroid (
[dimX * Math.random(), dimY * Math.random()],
randomInitialVec(), randomInitialDirection());
while (asteroid.isCollidedWith(ship)) {
var asteroid = new Asteroid (
[dimX * Math.random(), dimY * Math.random()],
randomInitialVec(), randomInitialDirection());
}
return asteroid;
};
var randomInitialVec = function() {
return Math.ceil(Math.random() * 1);
};
var randomInitialDirection = function() {
var dx = (Math.random() * 2) - 1;
var dy = (Math.random() * 2) - 1;
return [dx, dy];
};
var randomRadius = function(){
var radius = Math.random()*Asteroid.RADIUS;
if (radius < 10){
radius = 10;
}
return radius;
};
})(this);