-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
362 lines (296 loc) · 10.2 KB
/
app.js
File metadata and controls
362 lines (296 loc) · 10.2 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import * as THREE from 'three'
import { scheduler } from './jsm/scheduler.js'
import { TRANSLATION, ROTATION, environment, Mino, HoldQueue, NextQueue } from './jsm/Tetrominoes.js'
import Settings from './jsm/Settings.js'
import { Stats } from './jsm/Stats.js'
import { Menu } from './jsm/Menu.js'
import { TetraScene } from './jsm/TetraScene.js'
import * as FPS from 'three/addons/libs/stats.module.js'
HTMLElement.prototype.addNewChild = function (tag, properties) {
let child = document.createElement(tag)
for (let key in properties) {
child[key] = properties[key]
}
this.appendChild(child)
}
/* Game logic */
let game = {
playing: false,
start: function() {
stats.init()
menu.startButton.hide()
menu.stats.show()
menu.settings.close()
Mino.instances.clear()
nextQueue.init()
holdQueue.piece = undefined
holdQueue.clear()
scene.playfield.init()
scene.music.currentTime = 0
this.playing = true
stats.clock.start()
stats.level = settings.startLevel
this.resume()
},
resume: function() {
pauseSpan.innerHTML = "3"
setTimeout(() => pauseSpan.innerHTML = "2", 1000)
setTimeout(() => pauseSpan.innerHTML = "1", 2000)
setTimeout(() => {
pauseSpan.innerHTML = ""
document.onkeydown = onkeydown
document.onkeyup = onkeyup
window.onblur = game.pause
menu.settings.domElement.onclick = game.pause
document.body.classList.remove("pause")
menu.resumeButton.hide()
menu.pauseButton.show()
stats.clock.start()
stats.clock.elapsedTime = stats.elapsedTime
if (settings.musicVolume) scene.music.play()
if (scene.playfield.piece) {
scheduler.resetInterval(game.fall, stats.fallPeriod)
} else {
this.generate()
}
}, 3000)
},
generate: function(nextPiece=nextQueue.shift()) {
nextPiece.lockDelay = stats.lockDelay
scene.playfield.piece = nextPiece
scene.playfield.piece.onLockDown = game.lockDown
if (scene.playfield.piece.canMove(TRANSLATION.NONE)) {
scheduler.resetInterval(game.fall, stats.fallPeriod)
} else {
game.over() // block out
}
},
fall: function() {
scene.playfield.piece.move(TRANSLATION.DOWN)
},
lockDown: function() {
scheduler.clearTimeout(game.lockDown)
scheduler.clearInterval(game.fall)
if (scene.playfield.lock(scene.playfield.piece)) {
let tSpin = scene.playfield.piece.tSpin
let nbClearedLines = scene.playfield.clearLines()
stats.lockDown(nbClearedLines, tSpin)
if (settings.sfxVolume) {
if (nbClearedLines == 4 || (tSpin && nbClearedLines)) {
playSound(scene.tetrisSound, stats.combo)
} else if (nbClearedLines || tSpin) {
playSound(scene.lineClearSound, stats.combo)
}
}
game.generate()
} else {
game.over() // lock out
}
},
pause: function() {
menu.settings.domElement.onclick = null
stats.elapsedTime = stats.clock.elapsedTime
stats.clock.stop()
scheduler.clearInterval(game.fall)
scheduler.clearTimeout(game.lockDown)
scheduler.clearTimeout(repeat)
scheduler.clearInterval(autorepeat)
scene.music.pause()
document.onkeydown = resumeOnKeyDown
document.onkeyup = null
window.onblur = null
pauseSpan.onfocus = game.resume
pauseSpan.innerHTML = "II"
document.body.classList.add("pause")
menu.pauseButton.hide()
menu.resumeButton.show()
},
over: function() {
scene.playfield.piece.locking = false
document.onkeydown = null
window.onblur = null
scene.renderer.domElement.onfocus = null
menu.settings.domElement.onfocus = null
this.playing = false
scene.music.pause()
stats.clock.stop()
messagesSpan.addNewChild("div", { className: "show-level-animation", innerHTML: `<h1>GAME<br/>OVER</h1>` })
stats.speak("Game Over")
menu.pauseButton.hide()
menu.startButton.name("Rejouer")
menu.startButton.show()
},
fullscreen: function() {
if (document.fullscreenElement) {
if (document.exitFullscreen) {
document.exitFullscreen()
}
} else {
document.body.requestFullscreen()
}
}
}
document.onfullscreenchange = function() {
if (document.fullscreenElement) {
menu.settings.fullscreenButton.name("Quitter le plein écran")
} else {
menu.settings.fullscreenButton.name("Plein écran")
game.pause()
}
}
function playSound(sound, note=0) {
sound.stop()
sound.currentTime = 0
sound.playbackRate = Math.pow(5/4, note)
sound.play()
}
/* Handle player inputs */
let playerActions = {
moveLeft: () => scene.playfield.piece.move(TRANSLATION.LEFT)? scene.moveSound.play() : scene.hitSound.stop() && scene.hitSound.play(),
moveRight: () => scene.playfield.piece.move(TRANSLATION.RIGHT)? scene.moveSound.play() : scene.hitSound.stop() && scene.hitSound.play(),
rotateCW: () => scene.playfield.piece.rotate(ROTATION.CW)? scene.rotateSound.stop() && scene.rotateSound.play() : scene.spinEndSound.stop() && scene.spinEndSound.play(),
rotateCCW: () => scene.playfield.piece.rotate(ROTATION.CCW)? scene.rotateSound.stop() && scene.rotateSound.play() : scene.spinEndSound.stop() && scene.spinEndSound.play(),
softDrop: function () {
if (scene.playfield.piece.move(TRANSLATION.DOWN)) {
stats.score++
scene.moveSound.stop()
scene.moveSound.play()
} else {
scene.floorSound.stop()
scene.floorSound.play()
}
},
hardDrop: function () {
scheduler.clearTimeout(game.lockDown)
if (settings.sfxVolume) {
scene.hardDropSound.stop()
scene.hardDropSound.play()
}
while (scene.playfield.piece.move(TRANSLATION.DOWN)) stats.score += 2
game.lockDown()
scene.playfield.hardDropAnimation.reset()
scene.playfield.hardDropAnimation.play()
},
hold: function () {
if (scene.playfield.piece.holdEnabled) {
scheduler.clearInterval(game.fall)
scheduler.clearTimeout(game.lockDown)
let heldpiece = holdQueue.piece
holdQueue.piece = scene.playfield.piece
game.generate(heldpiece)
}
},
pause: game.pause,
}
const REPEATABLE_ACTIONS = [
playerActions.moveLeft,
playerActions.moveRight,
playerActions.softDrop
]
let pressedKeys = new Set()
let actionsQueue = []
function onkeydown(event) {
let key = event.key
if (key in settings.action) {
event.preventDefault()
if (!pressedKeys.has(key)) {
pressedKeys.add(key)
let action = playerActions[settings.action[key]]
action()
if (REPEATABLE_ACTIONS.includes(action)) {
actionsQueue.unshift(action)
scheduler.clearTimeout(repeat)
scheduler.clearInterval(autorepeat)
if (action == playerActions.softDrop) scheduler.resetInterval(autorepeat, settings.fallPeriod / 20)
else scheduler.resetTimeout(repeat, settings.dasDelay)
}
}
}
}
function repeat() {
if (actionsQueue.length) {
actionsQueue[0]()
scheduler.resetInterval(autorepeat, settings.arrDelay)
}
}
function autorepeat() {
if (actionsQueue.length) {
actionsQueue[0]()
} else {
scheduler.clearInterval(autorepeat)
}
}
function onkeyup(event) {
let key = event.key
if (key in settings.action) {
event.preventDefault()
pressedKeys.delete(key)
let action = playerActions[settings.action[key]]
if (actionsQueue.includes(action)) {
actionsQueue.splice(actionsQueue.indexOf(action), 1)
if (!actionsQueue.length) {
scheduler.clearTimeout(repeat)
scheduler.clearInterval(autorepeat)
}
}
}
}
function resumeOnKeyDown(event) {
let key = event.key
if(playerActions[settings.action[key]] == playerActions.pause) {
event.preventDefault()
game.resume()
}
}
/* Scene */
let loadingManager = new THREE.LoadingManager(
function() {
loadingDiv.style.display = "none"
if (!game.playing) menu.startButton.show()
scene.renderer.setAnimationLoop(animate)
},
function (url, itemsLoaded, itemsTotal) {
loadingPercent.innerText = Math.floor(100 * itemsLoaded / itemsTotal) + '%'
},
function (url) {
loadingPercent.innerText = "Erreur"
}
)
loadingManager.onStart = function (url, itemsLoaded, itemsTotal) {
loadingPercent.innerText = "0%"
loadingDiv.style.display = "flex"
}
const settings = new Settings()
const stats = new Stats(settings)
const scene = new TetraScene(settings, loadingManager)
const holdQueue = new HoldQueue()
scene.add(holdQueue)
const nextQueue = new NextQueue()
scene.add(nextQueue)
const menu = new Menu(game, settings, stats, scene)
menu.load()
let fps
if (window.location.search.includes("fps")) {
let fps = new FPS.default()
document.body.appendChild(fps.dom)
}
messagesSpan.onanimationend = function (event) {
event.target.remove()
}
const clock = new THREE.Clock()
function animate() {
const delta = clock.getDelta()
scene.update(delta)
environment.camera.update(scene.renderer, scene)
fps?.update()
}
window.addEventListener("resize", () => {
scene.renderer.setSize(window.innerWidth, window.innerHeight)
scene.camera.aspect = window.innerWidth / window.innerHeight
scene.camera.updateProjectionMatrix()
})
window.onbeforeunload = function (event) {
menu.save()
localStorage["teTraHighScore"] = stats.highScore
if (game.playing) return false;
}