-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollison.js
More file actions
46 lines (46 loc) · 1.5 KB
/
Copy pathCollison.js
File metadata and controls
46 lines (46 loc) · 1.5 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
export class Collision {
isCollidingBrick(ball, brick) {
if (ball.pos.x < brick.pos.x + brick.width &&
ball.pos.x + ball.width > brick.pos.x &&
ball.pos.y < brick.pos.y + brick.height &&
ball.pos.y + ball.height > brick.pos.y) {
return true;
}
return false;
}
// Check ball collision with bricks
isCollidingBricks(ball, bricks) {
let colliding = false;
bricks.forEach((brick, i) => {
if (this.isCollidingBrick(ball, brick)) {
ball.changeYDirection();
if (brick.energy === 1) {
bricks.splice(i, 1);
}
else {
brick.energy -= 1;
}
colliding = true;
}
});
return colliding;
}
checkBallCollision(ball, paddle, view) {
// 1. Check ball collision with paddle
if (ball.pos.x + ball.width > paddle.pos.x &&
ball.pos.x < paddle.pos.x + paddle.width &&
ball.pos.y + ball.height === paddle.pos.y) {
ball.changeYDirection();
}
// 2. Check ball collision with walls
// Ball movement X constraints
if (ball.pos.x > view.canvas.width - ball.width || ball.pos.x < 0) {
ball.changeXDirection();
}
// Ball movement Y constraints
if (ball.pos.y < 0) {
ball.changeYDirection();
}
}
}
//# sourceMappingURL=Collison.js.map