-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.js
107 lines (92 loc) · 3.02 KB
/
Ball.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const INITIAL_VELOCITY = 0.025;
const VELOCITY_INCREASE = .000001;
const hitWall = new Audio('hitWall.wav');
const hitPaddle = new Audio('hitPaddle.wav');
let collisionAllowed=true;
export default class Ball {
constructor(ballElem) {
this.ballElem = ballElem;
this.reset();
}
get x() {
return parseFloat(getComputedStyle(this.ballElem).getPropertyValue("--x"));
}
set x(value) {
this.ballElem.style.setProperty("--x", value);
}
get y() {
return parseFloat(getComputedStyle(this.ballElem).getPropertyValue("--y"));
}
set y(value) {
this.ballElem.style.setProperty("--y", value);
}
rect() {
return this.ballElem.getBoundingClientRect();
}
reset() {
this.x = 50;
this.y = 50;
this.direction = { x: 0, y: 0 };
this.velocity = INITIAL_VELOCITY;
while (
Math.abs(this.direction.x) <= .2 ||
Math.abs(this.direction.x) >= .9
) {
const heading = randomNumberBetween(0, 2 * Math.PI);
this.direction = { x: Math.cos(heading), y: Math.sin(heading) };
}
}
update(delta, paddleRects) {
this.x += this.direction.x * this.velocity * delta;
this.y += this.direction.y * this.velocity * delta;
this.velocity += VELOCITY_INCREASE * delta;
const rect = this.rect();
// Wall collisions
if (window.innerWidth <= 768) { // Mobile
if (rect.right >= window.innerWidth || rect.left <= 0) {
this.direction.x *= -1;
hitWall.currentTime = 0;
hitWall.play();
}
} else { // Desktop
if (rect.bottom >= window.innerHeight || rect.top <= 0) {
this.direction.y *= -1;
hitWall.currentTime = 0;
hitWall.play();
}
}
// Paddle collisions
if (paddleRects.some(r => isCollision(r, rect))) {
if (window.innerWidth <= 768) { // Mobile
this.direction.y *= -1;
// Adjust ball position slightly to avoid multiple collisions
if (rect.top < window.innerHeight / 2) {
this.y += 0.01;
} else {
this.y -= 0.01;
}
} else { // Desktop
this.direction.x *= -1;
// Adjust ball position slightly to avoid multiple collisions
if (rect.left < window.innerWidth / 2) {
this.x += 0.001;
} else {
this.x -= 0.001;
}
}
hitPaddle.currentTime = 0;
hitPaddle.play();
}
}
}
function randomNumberBetween(min, max) {
return Math.random() * (max - min) + min;
}
function isCollision(rect1, rect2) {
return (
rect1.left <= rect2.right &&
rect1.right >= rect2.left &&
rect1.top <= rect2.bottom &&
rect1.bottom >= rect2.top
);
}