Repository navigation
Expand file tree
/
Copy pathasteroid.js
More file actions
81 lines (73 loc) · 2.07 KB
/
Copy pathasteroid.js
File metadata and controls
81 lines (73 loc) · 2.07 KB
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
(function (root) {
var Asteroids = root.Asteroids = (root.Asteroids || {});
var Asteroid = Asteroids.Asteroid = function (pos, vel){
var radius = Math.floor(Math.random() * (Asteroid.RADIUS - 10)) + 10;
Asteroids.MovingObject.call(this, pos, vel, radius, Asteroid.COLOR)
};
Asteroid.RADIUS = 50;
Asteroid.COLOR = '#888888';
Asteroid.MAXVEL = 3;
Asteroid.inherits(Asteroids.MovingObject);
Asteroid.randomAsteroid = function(DIM_X, DIM_Y) {
var pos = [];
pos[0] = Math.floor(Math.random() * DIM_X);
pos[1] = Math.floor(Math.random() * DIM_Y);
var whichIndex = Math.floor(Math.random() * 2);
var whichVal = Math.floor(Math.random() * 2);
if (whichVal === 0) {
pos[whichIndex] = 0;
} else {
pos[whichIndex] = DIM_X;
}
pos = Asteroid.randomPos(DIM_X, DIM_Y);
var vel = ensureVelOffWall(pos, Asteroid.MAXVEL);
return (new Asteroid(pos, vel));
}
Asteroid.randomPos = function(DIM_X, DIM_Y) {
var pos = []
pos[0] = Math.floor(Math.random() * DIM_X);
pos[1] = Math.floor(Math.random() * DIM_Y);
randn = Math.floor(Math.random() * 4);
switch (randn) {
case 0:
pos[0] = 0;
break;
case 1:
pos[1] = 0;
break;
case 2:
pos[0] = DIM_X;
break;
case 3:
pos[1] = DIM_Y;
break;
}
return pos;
}
function randomVec(maxVel){
var vel = [0, 0];
while (vel[0] < 2 && vel[1] < 2) {
vel[0] = randomVelScalar(maxVel);
vel[1] = randomVelScalar(maxVel);
}
return vel;
}
function randomVelScalar(maxVel) {
return (Math.floor(Math.random() * (maxVel * 2)) - maxVel);
}
function ensureVelOffWall(pos, maxVel, DIM_X, DIM_Y) {
var velIntoWall = true;
var vel = [];
while (velIntoWall) {
velIntoWall = false;
vel = randomVec(maxVel);
if ((pos[0] === 0 && vel[0] < 0) ||
(pos[0] === DIM_X && vel[0] > 0) ||
(pos[1] === 0 && vel[1] < 0) ||
(pos[1] === DIM_X && vel[0] > 0)) {
velIntoWall = true;
}
}
return vel;
}
})(this);