-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
60 lines (49 loc) · 1.35 KB
/
helpers.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
import {
BOARD,
DROP_HEIGHT,
PADDLE_SIZE,
BRICKS_MARGIN,
BRICKS_PADDING,
BRICK_SIZE,
} from './constants';
export const ballStartPoint = () => {
const y = DROP_HEIGHT + Math.round(Math.random() * 40);
const x = Math.round(Math.random() * BOARD.width);
return { y, x: Math.max(x, 30) };
};
export const paddleStartPoint = () => {
return {
x: BOARD.width / 2 - PADDLE_SIZE.width / 2,
y: BOARD.height - PADDLE_SIZE.height,
};
};
export const generateBricks = () => {
const rows = 3;
const rowMaxLength = BOARD.width - BRICKS_MARGIN * 2;
return Array.from(Array(rows).keys())
.map((row) => {
const rowBricks = [];
let yStartPosition =
(row + 1) * BRICK_SIZE.height + BRICKS_MARGIN + BRICKS_PADDING * row;
let availableSpace = rowMaxLength;
let xStartingPoint = BRICKS_MARGIN;
while (availableSpace > 0) {
const isLast = availableSpace - BRICK_SIZE.width < 0;
let requiredSpace = BRICK_SIZE.width;
if (!isLast) {
requiredSpace += BRICKS_PADDING;
}
if (availableSpace > 0) {
rowBricks.push({
x: xStartingPoint,
y: yStartPosition,
hitpoints: 1,
});
}
availableSpace -= requiredSpace;
xStartingPoint += requiredSpace;
}
return rowBricks;
})
.flat();
};