-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameScreen.js
58 lines (52 loc) · 1.19 KB
/
gameScreen.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
// ? Choose next piece
function next() {
nextPieceIndex = pieces.indexOf(random(pieces))
let pos = findPos(nextPieceIndex)
nextPiece = new Piece(nextPieceIndex, (12 + pos[0]) * gridSize, (2 + pos[1]) * gridSize)
}
// ? Handle held piece
function held() {
let pos = findPos(heldPieceIndex)
if(!heldPiece) {
piece = new Piece(nextPieceIndex)
next()
} else piece = new Piece(heldPiece.pieceIndex)
heldPiece = new Piece(heldPieceIndex, (12 + pos[0]) * gridSize, (13 + pos[1]) * gridSize)
}
// ? Finds the pos array to centralize the pieces in the next and hold boxes
function findPos(index) {
let pos = []
switch (index) {
case 0:
pos = [1.5, 1.5]
break
case 1:
case 2:
case 3:
case 6:
pos = [1, 0.5]
break
case 4:
pos = [1.5, 1]
break
case 5:
pos = [0.5, 1]
break
}
return pos
}
function showScore() {
scoreCounter.innerText = `Score: ${score}`
highScoreCounter.innerText = `Highscore: ${highscore}`
}
function gameOver() {
gameOverScreen.setAttribute('data-gameover-screen', 'show')
}
function restartGame() {
gameOverScreen.setAttribute('data-gameover-screen', 'hide')
setup()
loop()
score = 0
showScore()
}
restartBtn.addEventListener('click', restartGame)