-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.js
84 lines (67 loc) · 1.85 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
(function (root) {
var Asteroids = root.Asteroids = root.Asteroids || {};
Function.prototype.inherits = function(BaseClass) {
function Surrogate() {};
Surrogate.prototype = BaseClass.prototype;
this.prototype = new Surrogate();
}
var Asteroid = Asteroids.Asteroid = function() {
var args = Array.prototype.slice.call(arguments);
Asteroids.MovingObject.apply(this, args);
this.color = "brown";
this.radius = 10;
};
Asteroid.inherits(Asteroids.MovingObject);
Asteroid.prototype.randomAsteroid = function(dimX, dimY, offBoard) {
var startX, startY;
if (offBoard) {
var range = 0;
var startEnd = Math.random();
var whichCoordFixed = Math.random();
if (whichCoordFixed < 0.5) {
if (startEnd < 0.5) range = dimX - 1;
startX = range + Math.random();
startY = Math.random() * dimY;
} else {
if (startEnd < 0.5) range = dimY - 1;
startY = range + Math.random();
startX = Math.random() * dimX;
}
} else {
startX = Math.random() * dimX;
startY = Math.random() * dimY;
}
var startSpeed = randomSpeed();
var startDir = randomDir();
return new Asteroid([startX, startY], startSpeed, startDir);
};
var _randomDir = function() {
var xRand = Math.random();
var yRand = Math.random();
if (xRand < 0.33) {
var xDir = -1;
} else if (xRand < 0.67) {
var xDir = 0;
} else {
var xDir = 1;
}
if (yRand < 0.33) {
var yDir = -1;
} else if (yRand < 0.67) {
var yDir = 0;
} else {
var yDir = 1;
}
return [xDir, yDir];
};
var randomDir = function() {
var dir;
do {
dir = _randomDir();
} while (dir[0] === 0 && dir[1] === 0);
return dir;
};
var randomSpeed = function() {
return (Math.random() * 4 + 4);
}
})(this);