-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.js
172 lines (154 loc) · 5.02 KB
/
piece.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class Piece {
constructor(pieceIndex = nextPieceIndex, x, y) {
// ? Position of the current piece
this.x = x ?? (widthBoard - 2 * gridSize) / 2
this.y = y ?? -4 * gridSize
// ? Type of piece (check pieces in constants.js for reference)
this.pieceIndex = pieceIndex ?? pieces.indexOf(random(pieces))
this.piece = pieces[this.pieceIndex]
// ? Shortcut variables to avoid typing 'this.piece.matrix' and 'this.piece.matrix.length' all over the file
this.matrix = this.piece.matrix
this.len = this.matrix.length
// ? Input filter
this.acceptedMoves = {
'ArrowLeft': 'left',
'A': 'left',
'a': 'left',
'ArrowRight': 'right',
'D': 'right',
'd': 'right',
'ArrowUp': 'up',
'W': 'up',
'w': 'up',
' ': 'spacebar',
'C': 'hold',
'c': 'hold'
}
}
show() {
fill(this.piece.color)
for (let row = 0; row < this.len; row++)
for (let col = 0; col < this.len; col++)
if (this.matrix[row][col]) square(this.x + col * gridSize, this.y + row * gridSize, gridSize)
}
update() {
if (buffer === 10) this.y += velocity
return
}
checkDownWardsCollision() {
for (let row = 0; row < this.len; row++) {
// ? Conditional to check if any rows of the matrix that contain a value of 1 have reached the bottom of the board
if (this.matrix[row].some(square => !!square) && this.y + row * gridSize >= heightBoard - gridSize)
return true
for (let col = 0; col < this.len; col++){
const collisionIsHappening = board.usedPlaces.some(
place => place[1] === this.x + col * gridSize && // ? Checks if your piece has the same x value as any particular place
place[2] === this.y + (row + 1) * gridSize // ? and is directly above that place
)
if (collisionIsHappening && this.matrix[row][col]) return true
}
}
return
}
collideDownWards() {
for (let row = 0; row < this.len; row++)
for (let col = 0; col < this.len; col++)
if (this.matrix[row][col])
// ? Adds the current matrix[row][col] to boards.usedPlaces
board.usedPlaces.push([
pieces[this.pieceIndex], // ? Allows the board object apply the right color
this.x + col * gridSize, // ? X position
this.y + row * gridSize // ? Y position
])
return true
}
receiveInput(key) {
if (!this.acceptedMoves[key]) return
let movement = this.acceptedMoves[key]
switch (movement) {
case 'right':
case 'left':
if (this.possibleToMove(movement)) this.move(movement)
break
case 'up':
this.rotate()
break
case 'spacebar':
while (!this.checkDownWardsCollision()) this.update()
break
case 'hold':
this.hold()
break
default: return
}
}
possibleToMove(movement, matrix = this.matrix) {
for (let row = 0; row < this.len; row++) {
for (let col = 0; col < this.len; col++) {
if (!matrix[row][col]) continue
const direction = movement === 'right' ? 1 : -1
const moveIsBlocked = board.usedPlaces.some(
place => (
place[1] === this.x + (col + direction) * gridSize &&
place[2] > this.y + (row - 1) * gridSize &&
place[2] < this.y + (row + 1) * gridSize
)
)
const moveHitsWall = this.x + col * gridSize === (movement === 'right' ? widthBoard - gridSize : 0)
if (moveHitsWall || moveIsBlocked) return
}
}
return true
}
move(movement) {
if (movement === 'left') this.x -= gridSize
if (movement === 'right') this.x += gridSize
}
rotate() {
let rotatedMatrix = []
// ? Adds a copy this.matrix rotated 90 degrees to the right to rotatedMatrix
// ? This process does not affect this.matrix
for (let row = 0; row < this.len; row++) {
rotatedMatrix.push(Array(this.len))
let k = this.len - 1
for (let col = 0; col < this.len; col++) {
rotatedMatrix[row][col] = this.matrix[k][row]
k--
}
}
// ? Checks if rotatedMatrix is in a valid position
for (let row = 0; row < this.len; row++) {
for (let col = 0; col < this.len; col++) {
// ? Only run the tests below if rotatedMatrix[row][col] is truthy
if (!rotatedMatrix[row][col]) continue
// ? Checks if rotation causes the piece to collide with other pieces or with the floor
const isGoingThroughFloor = this.y + row * gridSize === heightBoard - gridSize
const isGoingThroughPlace = board.usedPlaces.some(
place => (
place[1] === this.x + col * gridSize &&
place[2] > this.y + (row - 1) * gridSize &&
place[2] < this.y + (row + 1) * gridSize
)
)
// ? If any of the tests return true, abort the rotation
if (isGoingThroughFloor || isGoingThroughPlace) return
// ? Prevents rotation from causing the piece to go through walls
if (this.x + col * gridSize < 0) {
if (!this.possibleToMove('right', rotatedMatrix)) return
this.move('right')
} else if (this.x + col * gridSize > widthBoard - gridSize) {
if (!this.possibleToMove('left', rotatedMatrix)) return
this.move('left')
}
}
}
// ? Rotation didn't cause problems
return this.matrix = rotatedMatrix
}
hold() {
if (holding) return
heldPieceIndex = this.pieceIndex
held()
holding = true
}
}