/
star.js
33 lines (26 loc) · 1017 Bytes
/
star.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
(function (root) {
var AST = root.Asteroids = (root.Asteroids || {});
var Star = AST.Star = function () {
var radius = (Math.random() * 2)
AST.MovingObject.call(this, radius, Star.COLOR);
};
Star.inherits(AST.MovingObject);
Star.COLOR = 'white';
Star.randomStar = function (dimX, dimY) {
var randomStar = new Star();
randomStar.pos = [Math.random() * dimX , Math.random() * dimY];
randomStar.vel = [0, 0];
randomStar.parallax = Math.random();
return randomStar;
}
Star.prototype.setSpeed = function (shipVel) {
this.vel = [-1 * shipVel[0] * this.parallax, -1 * shipVel[1] * this.parallax];
};
Star.prototype.outOfBounds = function (bounds) {
var pos = this.pos;
pos[0] > bounds[0] ? pos[0] -= bounds[0] : pos[0];
pos[0] < 0 ? pos[0] += bounds[0] : pos[0];
pos[1] > bounds[1] ? pos[1] -= bounds[1] : pos[1];
pos[1] < 0 ? pos[1] += bounds[1] : pos[1];
};
})(this);