-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.js
134 lines (102 loc) · 3.97 KB
/
paddle.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { ball, gameCompStyles} from "./game.js";
import{game_started,ballDiv, ballRadius} from "./ball.js"
// gameboard variables
const gameBoard = document.querySelector(".game-board");
const gameBoardRect = gameBoard.getBoundingClientRect();
// paddle variables
const paddle_width = 100;
const paddle_margin_bottom = 50;
const paddle_height = 20;
const pad = document.createElement("div");
const padCompStyles = window.getComputedStyle(pad);
//sound
const paddle_hit_sound = new Audio('./assets/paddlehit.mp3')
// start is used to track the translated values
let pad_start = {a:0}
let paddle = {
x: gameBoardRect.width / 2 - paddle_width / 2,
// y value is the top left corner value of the paddle
y: gameBoardRect.height - paddle_margin_bottom - paddle_height,
width: paddle_width,
height: paddle_height,
xMovement: 20,
right: false,
left: false,
};
// draw paddle
function drawPaddle() {
pad.classList.add("pad");
pad.style.left = paddle.x + "px";
pad.style.top = paddle.y + "px";
pad.style.width = paddle.width + "px";
pad.style.height = paddle.height + "px";
pad.style.backgroundColor = "white";
pad.style.position = "absolute";
gameBoard.append(pad);
}
//translates paddle on the X axis
function movePaddle() {
const padRect = pad.getBoundingClientRect();
if (paddle.right) {
if (padRect.right + paddle.xMovement <= gameBoardRect.right) {
pad_start.a += paddle.xMovement;
} else {
pad_start.a += gameBoardRect.right - padRect.right;
}
if (!game_started.a) {
ballDiv.style.transform = `translateX(${pad_start.a}px)`;
pad.style.transform = `translateX(${pad_start.a}px)`;
paddle.right = false;
}
pad.style.transform = `translate(${pad_start.a}px)`;
paddle.right = false;
}
if (paddle.left) {
if (padRect.left - paddle.xMovement >= gameBoardRect.left) {
pad_start.a -= paddle.xMovement;
} else {
pad_start.a -= padRect.left - gameBoardRect.left;
}
if (!game_started.a) {
ballDiv.style.transform = `translate(${pad_start.a}px)`;
pad.style.transform = `translate(${pad_start.a}px)`;
paddle.left = false;
}
pad.style.transform = `translate(${pad_start.a}px)`;
paddle.left = false;
}
}
function padCollision() {
//also tried making the below two variables global but causes errors
const ballRect = ballDiv.getBoundingClientRect();
const padRect = pad.getBoundingClientRect();
//added + ballRect.height
if (ballRect.x < padRect.x + padRect.width && ballRect.x + ballRadius * 2 > padRect.x && ballRect.bottom >= padRect.top && ball.deltaY > 0) {
// play sound
if (game_started.a) {
paddle_hit_sound.play()
}
// CHECK WHERE THE ballRect HIT THE PADDLE
let collidePoint = ballRect.x - (padRect.x + padRect.width / 2);
// NORMALIZE THE VALUES
collidePoint = collidePoint / (padRect.width / 2);
// CALCULATE THE ANGLE OF THE ballRect
let angle = (collidePoint * Math.PI) / 3;
ball.deltaX = ball.speed * Math.sin(angle);
ball.deltaY = -ball.speed * Math.cos(angle);
ball.speed *= 1.05
}
}
//toggles boolean used within movePaddle function
function movePaddleBool() {
document.addEventListener("keydown", function (event) {
const padRect = pad.getBoundingClientRect();
event.preventDefault();
if (event.key == "ArrowRight" && padRect.right + parseInt(gameCompStyles.border) < gameBoardRect.right) {
paddle.right = true;
} else if (event.key == "ArrowLeft" && padRect.left - parseInt(gameCompStyles.border) > gameBoardRect.left) {
paddle.left = true;
}
});
}
export{paddle, drawPaddle, movePaddle, padCollision, pad, movePaddleBool, paddle_width, paddle_height, pad_start }