-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuck.js
42 lines (35 loc) · 907 Bytes
/
puck.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
function Puck(rink) {
this.x = rink.center.x;
this.y = rink.center.y;
this.velX = 0;
this.velY = 0;
this.color = "black";
this.size = rink.width/100;
}
Puck.prototype.draw = function(ctx, rink) {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(rink.left + this.x, rink.top + this.y, this.size, 0, 2 * Math.PI);
ctx.fill();
}
Puck.prototype.updatePosition = function(rink, pos){
//TODO check if out of rink
this.x = pos.x;
this.y = pos.y;
}
Puck.prototype.update = function() {
if ((this.x + this.size) >= width) {
this.velX = -(this.velX);
}
if ((this.x - this.size) <= 0) {
this.velX = -(this.velX);
}
if ((this.y + this.size) >= height) {
this.velY = -(this.velY);
}
if ((this.y - this.size) <= 0) {
this.velY = -(this.velY);
}
this.x += this.velX;
this.y += this.velY;
}