-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGameWorld.js
75 lines (52 loc) · 1.58 KB
/
GameWorld.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
function GameWorld(){
this.balls = CONSTANTS.ballsParams.map(params => new Ball(...params));
this.whiteBall = this.balls.find(ball => ball.color === COLOR.WHITE);
this.stick = new Stick(
this.whiteBall.position.copy(),
this.whiteBall.shoot.bind(this.whiteBall)
);
this.table = {
TopY: 57,
RightX: 1443,
BottomY: 768,
LeftX: 57
}
}
GameWorld.prototype.handleCollisions = function(){
for(let i = 0 ; i < this.balls.length ; i++ ){
this.balls[i].handleBallInPocket();
this.balls[i].collideWithTable(this.table);
for(let j = i + 1 ; j < this.balls.length ; j++ ){
const firstBall = this.balls[i];
const secondBall = this.balls[j];
firstBall.collideWithBall(secondBall);
}
}
}
GameWorld.prototype.update = function(){
this.handleCollisions();
this.stick.update();
for(let i = 0 ; i < this.balls.length ; i++){
this.balls[i].update(CONSTANTS.delta);
}
if(!this.ballsMoving() && this.stick.shot){
this.stick.reposition(this.whiteBall.position);
}
}
GameWorld.prototype.draw = function(){
Canvas.drawImage(sprites.background, new Vector2());
for(let i = 0 ; i < this.balls.length ; i++){
this.balls[i].draw();
}
this.stick.draw();
}
GameWorld.prototype.ballsMoving = function(){
let ballsMoving = false;
for( let i = 0 ; i < this.balls.length ; i++ ){
if(this.balls[i].moving){
ballsMoving = true;
break;
}
}
return ballsMoving;
}