5 next pieces

This commit is contained in:
Adrien MALINGREY 2023-05-16 23:13:39 +02:00
parent 7d3a75872c
commit 6a815097b7

60
app.js
View File

@ -121,18 +121,14 @@ class Scheduler {
class Matrix extends THREE.Group { class Matrix extends THREE.Group {
constructor() {
super()
}
init() { init() {
this.cells = Array(ROWS).fill().map(() => Array(COLUMNS)) this.cells = Array(ROWS).fill().map(() => Array(COLUMNS))
} }
cellIsEmpty(position) { cellIsEmpty(p) {
return 0 <= position.x && position.x < COLUMNS && return 0 <= p.x && p.x < COLUMNS &&
0 <= position.y && position.y < ROWS && 0 <= p.y && p.y < ROWS &&
!this.cells[position.y][position.x] !this.cells[p.y][p.x]
} }
lock(piece) { lock(piece) {
@ -171,6 +167,30 @@ class Matrix extends THREE.Group {
} }
class NextQueue extends THREE.Group {
init() {
this.pieces = this.positions.map((p) => {
let piece = new Tetromino.random()
piece.position.set(p.x, p.y, p.z)
this.add(piece)
return piece
})
}
shift() {
let fistPiece = this.pieces.shift()
let lastPiece = new Tetromino.random()
this.add(lastPiece)
this.pieces.push(lastPiece)
this.positions.forEach((p, i) => {
this.pieces[i].position.set(p.x, p.y, p.z)
})
return fistPiece
}
}
NextQueue.prototype.positions = [P(0, 0, 0), P(0, -4, 0), P(0, -8, 0), P(0, -12, 0), P(0, -16, 0)]
class Mino extends THREE.Mesh { class Mino extends THREE.Mesh {
constructor() { constructor() {
super(Mino.prototype.geometry) super(Mino.prototype.geometry)
@ -218,7 +238,7 @@ class Tetromino extends THREE.Group {
set facing(facing) { set facing(facing) {
this._facing = facing this._facing = facing
this.minoesPosition[this.facing].forEach( this.minoesPosition[this.facing].forEach(
(position, i) => this.children[i].position.set(position.x, position.y, position.z) (p, i) => this.children[i].position.set(p.x, p.y, p.z)
) )
} }
@ -415,7 +435,7 @@ class T extends Tetromino {
get tSpin() { get tSpin() {
if (this.rotatedLast) { if (this.rotatedLast) {
let [a, b, c, d] = this.tSlots[piece.facing] let [a, b, c, d] = this.tSlots[piece.facing]
.map(position => !matrix.cellIsEmpty(position.clone().add(this.position))) .map(p => !matrix.cellIsEmpty(p.clone().add(this.position)))
if (a && b && (c || d)) if (a && b && (c || d))
return T_SPIN.T_SPIN return T_SPIN.T_SPIN
else if (c && d && (a || b)) else if (c && d && (a || b))
@ -835,7 +855,7 @@ holdQueue.position.set(-5, 16, 0)
scene.add(holdQueue) scene.add(holdQueue)
const matrix = new Matrix() const matrix = new Matrix()
scene.add(matrix) scene.add(matrix)
const nextQueue = new THREE.Group() const nextQueue = new NextQueue()
nextQueue.position.set(13, 16, 0) nextQueue.position.set(13, 16, 0)
scene.add(nextQueue) scene.add(nextQueue)
let ghost = new Ghost() let ghost = new Ghost()
@ -885,9 +905,13 @@ function restart() {
stats.init() stats.init()
settings.init() settings.init()
holdQueue.remove(holdQueue.piece) holdQueue.remove(holdQueue.piece)
holdQueue.piece = null
if (nextQueue.pieces) nextQueue.pieces.forEach(piece => nextQueue.remove(piece))
Array.from(matrix.children).forEach(mino => matrix.remove(mino)) Array.from(matrix.children).forEach(mino => matrix.remove(mino))
matrix.init() matrix.init()
nextQueue.remove(nextQueue.piece) scene.remove(piece)
piece = null
scene.remove(ghost)
music.currentTime = 0 music.currentTime = 0
pauseSettings() pauseSettings()
} }
@ -904,9 +928,8 @@ function pauseSettings() {
settings.show() settings.show()
} }
onblur = pauseSettings
function newGame() { function newGame(event) {
if (!settings.form.checkValidity()) { if (!settings.form.checkValidity()) {
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
@ -918,12 +941,11 @@ function newGame() {
titleHeader.innerHTML = "PAUSE" titleHeader.innerHTML = "PAUSE"
resumeButton.innerHTML = "Reprendre" resumeButton.innerHTML = "Reprendre"
event.target.onsubmit = resume event.target.onsubmit = resume
holdQueue.piece = null nextQueue.init()
nextQueue.piece = new Tetromino.random()
nextQueue.add(nextQueue.piece)
stats.level = levelInput.valueAsNumber stats.level = levelInput.valueAsNumber
localStorage["startLevel"] = levelInput.value localStorage["startLevel"] = levelInput.value
playing = true playing = true
onblur = pauseSettings
resume(event) resume(event)
} }
} }
@ -961,9 +983,7 @@ function generate(heldPiece) {
if (heldPiece) { if (heldPiece) {
piece = heldPiece piece = heldPiece
} else { } else {
piece = nextQueue.piece piece = nextQueue.shift()
nextQueue.piece = new Tetromino.random()
nextQueue.add(nextQueue.piece)
} }
piece.position.set(4, SKYLINE, 0) piece.position.set(4, SKYLINE, 0)
scene.add(piece) scene.add(piece)