-
Notifications
You must be signed in to change notification settings - Fork 1
/
moving_object.js
51 lines (44 loc) · 1.28 KB
/
moving_object.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
(function (root) {
var Asteroids = root.Asteroids = (root.Asteroids || {});
var MovingObject = Asteroids.MovingObject = function (posX, posY, vel, radius, color) {
this.posX = posX;
this.posY = posY;
this.vel = vel;
this.radius = radius;
this.color = color;
};
MovingObject.prototype.move = function() {
this.posX = bound((this.posX + this.vel[0]), Asteroids.Game.DIM_X);
this.posY = bound((this.posY + this.vel[1]), Asteroids.Game.DIM_Y);
};
var bound = Asteroids.bound = function (number, max) {
if (number < 0) {
return (number + max);
} else if (number > max) {
return (number - max);
} else {
return number;
}
};
MovingObject.prototype.draw = function(ctx) {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(
this.posX,
this.posY,
this.radius,
0,
2 * Math.PI,
false
);
ctx.fill();
};
MovingObject.prototype.isCollidedWith = function (otherObject) {
return (this.distanceFrom(otherObject) < (this.radius + otherObject.radius));
};
MovingObject.prototype.distanceFrom = function (otherObject) {
var dX = otherObject.posX - this.posX;
var dY = otherObject.posY - this.posY;
return Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
};
})(this);