-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path@
253 lines (226 loc) · 6.02 KB
/
@
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Creates a single square block
class TetrisBlock {
// Color is a essentially a filter for the texture should be a 3-float vector of values in [0.0; 1.0]
// Position should be a 3-int vector of [0, 5], [0, 20], [0, 5]
//
// Requires:
// EasyWebGL as a static variable
// GLObject as static variable
constructor (color) {
this.color = color
this.shouldDraw = true
}
draw (translation) {
const color = new Float32Array(this.color)
// TetrisBlock.program.setUniforms(translation, color)
TetrisBlock.program.setUniforms({ translation, color })
if (this.shouldDraw) {
TetrisBlock.GLObject.draw()
}
}
}
let uparrow = 38
let lfarrow = 37
let rgarrow = 39
let dnarrow = 40
let space = 32
let comma = 188
let semic = 59
let dot = 190
let a = 65
let o = 79
let e = 69
let q = 81
let j = 74
let h = 72
let t = 84
let n = 78
let s = 83
let u = 85
let w = 87
let r = 82
let y = 89
// TODO:
// Out of bounds function
// Move left
// Move right
// Move front
// Move back
// Layer is full
// Points
// Restart
// Speed up
class Tetris {
constructor () {
// Initialize the game array with false
// this array will be populated with GLObjects to render
// layer = y, row = z, col = x as per normal webgl coordinates
this.blocks = new Array(20).fill(false).map(layer =>
new Array(6).fill(false).map(row =>
new Array(6).fill(false)
)
)
this.render = this.render.bind(this)
this.endGame = this.endGame.bind(this)
this.startGame = this.startGame.bind(this)
this.clockTick = null
this.bindKeys = this.bindKeys.bind(this)
}
bindKeys () {
document.onkeydown = kbdEvent => {
switch(kbdEvent.which) {
case uparrow: this.moveRow(1); break;
case dnarrow: this.moveRow(-1); break;
case rgarrow: this.moveCol(1); break;
case lfarrow: this.moveCol(-1); break;
case space : this.moveAllTheWayDown(); break;
}
}
}
forAll (doThis) {
this.blocks.forEach(layer => {
layer.forEach(row => {
row.forEach(block => {
if (block)
doThis(block)
})
})
})
}
reset () {
this.blocks.forEach(layer => {
layer.forEach((row, i) => {
row[i] = false
})
})
}
startGame () {
this.createBlock();
this.clockTick = setInterval(() => {
this.moveDown()
}, 75)
}
endGame () {
console.log('You suck lost bruuuuuuuh!')
let out = -5
window.clearInterval(this.clockTick)
const interval = setInterval(() => {
if (out++ >= 0) {
window.clearInterval(interval)
}
this.forAll(block => {
block.shouldDraw = !block.shouldDraw
})
}, 500)
}
createBlock () {
if (this.blocks[19][3][3] || this.blocks[19][3][2]) {
window.clearInterval(this.clockTick)
this.endGame()
}
const rand = parseInt(Math.random() * 3) + 2
const x = rand
const isStraight = x == 3
const z = isStraight ? 4 : 3
let color = [Math.random(), Math.random(), Math.random()];
const blockPositions = [[3, 19, 3], [3, 19, 2], [x, 19, z]]
blockPositions.forEach(position => {
const [x, y, z] = position
this.blocks[y][z][x] = new TetrisBlock(color)
})
this.currentBlockPositions = blockPositions
}
render () {
this.blocks.forEach((layer, y) => {
layer.forEach((row, z)=> {
row.forEach((block, x) => {
if (block)
block.draw(new Float32Array([x, y, z]))
})
})
})
}
moveAllTheWayDown() {
while (this.canMoveDown(this.currentBlockPositions))
this.moveDown(this.currentBlockPositions)
this.createBlock()
}
moveCol (n) {
console.log('Moving COL by ' + n)
if (!this.canMoveCol(n))
return
console.log('CAN MOVE')
const nextBlockPositions = []
this.currentBlockPositions.forEach(position => {
let [x, y, z] = position
let block = this.blocks[y][z][x]
this.blocks[y][z][x+n] = block
this.blocks[y][z][x] = false
nextBlockPositions.push([x+n, y, z])
})
this.currentBlockPositions = nextBlockPositions
}
moveRow (n) {
console.log('Moving ROW by ' + n)
if (!this.canMoveRow(n))
return
console.log('CAN MOVE')
const nextBlockPositions = []
this.currentBlockPositions.forEach(position => {
let [x, y, z] = position
let block = this.blocks[y][z][x]
this.blocks[y][z][x] = false
this.blocks[y][z+n][x] = block
nextBlockPositions.push([x, y, z+n])
})
this.currentBlockPositions = nextBlockPositions
}
moveDown () {
if (!this.canMoveDown()) {
this.createBlock()
return
}
const nextBlockPositions = []
this.currentBlockPositions.forEach(position => {
let [x, y, z] = position
let block = this.blocks[y][z][x]
this.blocks[y][z][x] = false
this.blocks[y-1][z][x] = block
nextBlockPositions.push([x, y-1, z])
})
this.currentBlockPositions = nextBlockPositions
}
canMove (n, xyzAs012, max) {
let isPossible = true
let currentBlocks = this.currentBlockPositions.map(pos => {
const [x, y, z] = pos
return this.blocks[y][z][x]
})
this.currentBlockPositions.forEach(position => {
let pos = position.slice()
pos[xyzAs012] += n
a = pos[xyzAs012]
let [x, y, z] = pos
if (xyzAs012 != 1)
console.log(a)
// Value says if there is a block at position [x, y, z] that is not part of the currently falling block
if (a > max || a < 0) {
console.log("BLOCKED")
isPossible = false
} else if (this.blocks[y][z][x]) {
let isSameBlock = false
currentBlocks.forEach(block => { if (block == this.blocks[y][z][x]) isSameBlock == true })
isPossible = isSameBlock
}
})
return isPossible
}
// Magic constants: 0=x, 1=y, 2=z
canMoveRow (n) { return this.canMove(n, 2, 5) }
canMoveCol (n) { return this.canMove(n, 0, 5) }
canMoveDown () { return this.canMove(-1, 1, 19) }
}
export {
Tetris,
TetrisBlock
}