refactoring
This commit is contained in:
parent
791f594670
commit
f85ad3f3e7
116
app.js
116
app.js
@ -72,11 +72,11 @@ class Matrix extends THREE.Group {
|
|||||||
!this.cells[p.y][p.x]
|
!this.cells[p.y][p.x]
|
||||||
}
|
}
|
||||||
|
|
||||||
lock(piece) {
|
lock() {
|
||||||
let minoes = Array.from(piece.children)
|
this.piece.locked = false
|
||||||
|
let minoes = Array.from(this.piece.children)
|
||||||
minoes.forEach(mino => {
|
minoes.forEach(mino => {
|
||||||
mino.position.add(piece.position)
|
mino.position.add(this.piece.position)
|
||||||
mino.material = piece.material
|
|
||||||
this.add(mino)
|
this.add(mino)
|
||||||
if (this.cellIsEmpty(mino.position)) {
|
if (this.cellIsEmpty(mino.position)) {
|
||||||
this.cells[mino.position.y][mino.position.x] = mino
|
this.cells[mino.position.y][mino.position.x] = mino
|
||||||
@ -260,14 +260,14 @@ class Tetromino extends THREE.Group {
|
|||||||
this.position.add(translation)
|
this.position.add(translation)
|
||||||
if (!testFacing) {
|
if (!testFacing) {
|
||||||
this.rotatedLast = false
|
this.rotatedLast = false
|
||||||
ghost.copy(this)
|
this.moveGhost()
|
||||||
}
|
}
|
||||||
if (this.canMove(TRANSLATION.DOWN)) {
|
if (this.canMove(TRANSLATION.DOWN)) {
|
||||||
this.locked = false
|
this.locked = false
|
||||||
scene.add(ghost)
|
scene.add(this.ghost)
|
||||||
} else {
|
} else {
|
||||||
this.locked = true
|
this.locked = true
|
||||||
scene.remove(ghost)
|
scene.remove(this.ghost)
|
||||||
scheduler.setTimeout(game.lockDown, stats.lockDelay)
|
scheduler.setTimeout(game.lockDown, stats.lockDelay)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -286,27 +286,42 @@ class Tetromino extends THREE.Group {
|
|||||||
this.rotatedLast = true
|
this.rotatedLast = true
|
||||||
if (rotationPoint == 4) this.rotationPoint4Used = true
|
if (rotationPoint == 4) this.rotationPoint4Used = true
|
||||||
//favicon.href = this.favicon_href
|
//favicon.href = this.favicon_href
|
||||||
ghost.copy(this)
|
this.moveGhost()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
moveGhost() {
|
||||||
|
this.ghost.position.copy(this.position)
|
||||||
|
this.ghost.facing = this.facing
|
||||||
|
this.ghost.minoesPosition = this.minoesPosition
|
||||||
|
this.children.forEach((mino, i) => {
|
||||||
|
this.ghost.children[i].position.copy(mino.position)
|
||||||
|
this.ghost.children[i].material = this.ghostMaterial
|
||||||
|
})
|
||||||
|
while (this.ghost.canMove(TRANSLATION.DOWN)) this.ghost.position.y--
|
||||||
|
}
|
||||||
|
|
||||||
get tSpin() {
|
get tSpin() {
|
||||||
return T_SPIN.NONE
|
return T_SPIN.NONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Super Rotation System
|
// Super Rotation System
|
||||||
// freedom of movement = srs[piece.facing][rotation]
|
// freedom of movement = srs[matrix.piece.facing][rotation]
|
||||||
Tetromino.prototype.srs = [
|
Tetromino.prototype.srs = [
|
||||||
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(1, 1), P(0, -2), P(1, -2)] },
|
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(1, 1), P(0, -2), P(1, -2)] },
|
||||||
{ [ROTATION.CW]: [P(0, 0), P(1, 0), P(1, -1), P(0, 2), P(1, 2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(1, -1), P(0, 2), P(1, 2)] },
|
{ [ROTATION.CW]: [P(0, 0), P(1, 0), P(1, -1), P(0, 2), P(1, 2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(1, -1), P(0, 2), P(1, 2)] },
|
||||||
{ [ROTATION.CW]: [P(0, 0), P(1, 0), P(1, 1), P(0, -2), P(1, -2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)] },
|
{ [ROTATION.CW]: [P(0, 0), P(1, 0), P(1, 1), P(0, -2), P(1, -2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)] },
|
||||||
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)] },
|
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)] },
|
||||||
]
|
]
|
||||||
|
|
||||||
Tetromino.prototype.lockedMaterial = new MinoMaterial(0xffffff)
|
Tetromino.prototype.lockedMaterial = new MinoMaterial(0xffffff)
|
||||||
|
|
||||||
|
class Ghost extends Tetromino {}
|
||||||
|
Ghost.prototype.minoesPosition = [
|
||||||
|
[P(0, 0, 0), P(0, 0, 0), P(0, 0, 0), P(0, 0, 0)],
|
||||||
|
]
|
||||||
|
|
||||||
class I extends Tetromino { }
|
class I extends Tetromino { }
|
||||||
I.prototype.minoesPosition = [
|
I.prototype.minoesPosition = [
|
||||||
[P(-1, 0), P(0, 0), P(1, 0), P(2, 0)],
|
[P(-1, 0), P(0, 0), P(1, 0), P(2, 0)],
|
||||||
@ -366,7 +381,7 @@ S.prototype.ghostMaterial = new GhostMaterial(COLORS.S)
|
|||||||
class T extends Tetromino {
|
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[matrix.piece.facing]
|
||||||
.map(p => !matrix.cellIsEmpty(p.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
|
||||||
@ -401,22 +416,6 @@ Z.prototype.minoesPosition = [
|
|||||||
Z.prototype.material = new MinoMaterial(COLORS.Z)
|
Z.prototype.material = new MinoMaterial(COLORS.Z)
|
||||||
Z.prototype.ghostMaterial = new GhostMaterial(COLORS.Z)
|
Z.prototype.ghostMaterial = new GhostMaterial(COLORS.Z)
|
||||||
|
|
||||||
class Ghost extends Tetromino {
|
|
||||||
copy(piece) {
|
|
||||||
this.position.copy(piece.position)
|
|
||||||
this.facing = piece.facing
|
|
||||||
this.minoesPosition = piece.minoesPosition
|
|
||||||
piece.children.forEach((mino, i) => {
|
|
||||||
this.children[i].position.copy(mino.position)
|
|
||||||
this.children[i].material = piece.ghostMaterial
|
|
||||||
})
|
|
||||||
while (this.canMove(TRANSLATION.DOWN)) this.position.y--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ghost.prototype.minoesPosition = [
|
|
||||||
[P(0, 0, 0), P(0, 0, 0), P(0, 0, 0), P(0, 0, 0)],
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
/* world */
|
/* world */
|
||||||
|
|
||||||
@ -527,7 +526,7 @@ scene.add(matrix)
|
|||||||
const nextQueue = new NextQueue()
|
const nextQueue = new NextQueue()
|
||||||
nextQueue.position.set(13, SKYLINE - 2)
|
nextQueue.position.set(13, SKYLINE - 2)
|
||||||
scene.add(nextQueue)
|
scene.add(nextQueue)
|
||||||
let ghost = new Ghost()
|
Tetromino.prototype.ghost = new Ghost()
|
||||||
|
|
||||||
const edgeMaterial = new THREE.MeshBasicMaterial({
|
const edgeMaterial = new THREE.MeshBasicMaterial({
|
||||||
color: 0x88abe0,
|
color: 0x88abe0,
|
||||||
@ -606,8 +605,6 @@ messagesSpan.onanimationend = function (event) {
|
|||||||
event.target.remove()
|
event.target.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
let piece = null
|
|
||||||
|
|
||||||
let game = {
|
let game = {
|
||||||
playing: false,
|
playing: false,
|
||||||
|
|
||||||
@ -618,13 +615,13 @@ let game = {
|
|||||||
gui.settings.close()
|
gui.settings.close()
|
||||||
|
|
||||||
holdQueue.remove(holdQueue.piece)
|
holdQueue.remove(holdQueue.piece)
|
||||||
holdQueue.piece = null
|
holdQueue.piece = undefined
|
||||||
if (nextQueue.pieces) nextQueue.pieces.forEach(piece => nextQueue.remove(piece))
|
if (nextQueue.pieces) nextQueue.pieces.forEach(piece => nextQueue.remove(piece))
|
||||||
Array.from(matrix.children).forEach(mino => matrix.remove(mino))
|
while(matrix.children.length) matrix.remove(matrix.children[0])
|
||||||
matrix.init()
|
matrix.init()
|
||||||
scene.remove(piece)
|
|
||||||
piece = null
|
scene.remove(matrix.piece)
|
||||||
scene.remove(ghost)
|
matrix.piece = null
|
||||||
world.music.currentTime = 0
|
world.music.currentTime = 0
|
||||||
matrix.edge.visible = true
|
matrix.edge.visible = true
|
||||||
|
|
||||||
@ -639,7 +636,7 @@ let game = {
|
|||||||
this.resume()
|
this.resume()
|
||||||
},
|
},
|
||||||
|
|
||||||
resume: function(event) {
|
resume: function() {
|
||||||
document.onkeydown = onkeydown
|
document.onkeydown = onkeydown
|
||||||
document.onkeyup = onkeyup
|
document.onkeyup = onkeyup
|
||||||
|
|
||||||
@ -648,23 +645,18 @@ let game = {
|
|||||||
stats.clock.elapsedTime = stats.elapsedTime
|
stats.clock.elapsedTime = stats.elapsedTime
|
||||||
world.music.play()
|
world.music.play()
|
||||||
|
|
||||||
if (piece) scheduler.setInterval(game.fall, stats.fallPeriod)
|
if (matrix.piece) scheduler.setInterval(game.fall, stats.fallPeriod)
|
||||||
else this.generate()
|
else this.generate()
|
||||||
},
|
},
|
||||||
|
|
||||||
generate: function(heldPiece) {
|
generate: function(nextPiece=nextQueue.shift()) {
|
||||||
if (heldPiece) {
|
matrix.piece = nextPiece
|
||||||
piece = heldPiece
|
matrix.piece.position.set(4, SKYLINE)
|
||||||
} else {
|
scene.add(matrix.piece)
|
||||||
piece = nextQueue.shift()
|
matrix.piece.moveGhost()
|
||||||
}
|
scene.add(matrix.piece.ghost)
|
||||||
piece.position.set(4, SKYLINE)
|
|
||||||
scene.add(piece)
|
|
||||||
ghost.copy(piece)
|
|
||||||
//world.directionalLight.target = piece
|
|
||||||
scene.add(ghost)
|
|
||||||
|
|
||||||
if (piece.canMove(TRANSLATION.NONE)) {
|
if (matrix.piece.canMove(TRANSLATION.NONE)) {
|
||||||
scheduler.setInterval(game.fall, stats.fallPeriod)
|
scheduler.setInterval(game.fall, stats.fallPeriod)
|
||||||
} else {
|
} else {
|
||||||
game.over() // block out
|
game.over() // block out
|
||||||
@ -672,16 +664,16 @@ let game = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
fall: function() {
|
fall: function() {
|
||||||
piece.move(TRANSLATION.DOWN)
|
matrix.piece.move(TRANSLATION.DOWN)
|
||||||
},
|
},
|
||||||
|
|
||||||
lockDown: function() {
|
lockDown: function() {
|
||||||
scheduler.clearTimeout(game.lockDown)
|
scheduler.clearTimeout(game.lockDown)
|
||||||
scheduler.clearInterval(game.fall)
|
scheduler.clearInterval(game.fall)
|
||||||
|
|
||||||
if (matrix.lock(piece)) {
|
if (matrix.lock(matrix.piece)) {
|
||||||
scene.remove(piece)
|
scene.remove(matrix.piece)
|
||||||
let tSpin = piece.tSpin
|
let tSpin = matrix.piece.tSpin
|
||||||
let nbClearedLines = matrix.clearLines()
|
let nbClearedLines = matrix.clearLines()
|
||||||
if (settings.sfxVolume) {
|
if (settings.sfxVolume) {
|
||||||
if (nbClearedLines == 4 || (tSpin && nbClearedLines)) {
|
if (nbClearedLines == 4 || (tSpin && nbClearedLines)) {
|
||||||
@ -717,7 +709,7 @@ let game = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
over: function() {
|
over: function() {
|
||||||
piece.locked = false
|
matrix.piece.locked = false
|
||||||
|
|
||||||
document.onkeydown = null
|
document.onkeydown = null
|
||||||
renderer.domElement.onblur = null
|
renderer.domElement.onblur = null
|
||||||
@ -734,16 +726,16 @@ let game = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let playerActions = {
|
let playerActions = {
|
||||||
moveLeft: () => piece.move(TRANSLATION.LEFT),
|
moveLeft: () => matrix.piece.move(TRANSLATION.LEFT),
|
||||||
|
|
||||||
moveRight: () => piece.move(TRANSLATION.RIGHT),
|
moveRight: () => matrix.piece.move(TRANSLATION.RIGHT),
|
||||||
|
|
||||||
rotateCW: () => piece.rotate(ROTATION.CW),
|
rotateCW: () => matrix.piece.rotate(ROTATION.CW),
|
||||||
|
|
||||||
rotateCCW: () => piece.rotate(ROTATION.CCW),
|
rotateCCW: () => matrix.piece.rotate(ROTATION.CCW),
|
||||||
|
|
||||||
softDrop: function () {
|
softDrop: function () {
|
||||||
if (piece.move(TRANSLATION.DOWN)) stats.score++
|
if (matrix.piece.move(TRANSLATION.DOWN)) stats.score++
|
||||||
},
|
},
|
||||||
|
|
||||||
hardDrop: function () {
|
hardDrop: function () {
|
||||||
@ -753,19 +745,19 @@ let playerActions = {
|
|||||||
world.hardDropSound.currentTime = 0
|
world.hardDropSound.currentTime = 0
|
||||||
world.hardDropSound.play()
|
world.hardDropSound.play()
|
||||||
}
|
}
|
||||||
while (piece.move(TRANSLATION.DOWN)) stats.score += 2
|
while (matrix.piece.move(TRANSLATION.DOWN)) stats.score += 2
|
||||||
game.lockDown()
|
game.lockDown()
|
||||||
hardDroppedMatrix.reset()
|
hardDroppedMatrix.reset()
|
||||||
hardDroppedMatrix.play()
|
hardDroppedMatrix.play()
|
||||||
},
|
},
|
||||||
|
|
||||||
hold: function () {
|
hold: function () {
|
||||||
if (piece.holdEnabled) {
|
if (matrix.piece.holdEnabled) {
|
||||||
scheduler.clearInterval(game.fall)
|
scheduler.clearInterval(game.fall)
|
||||||
scheduler.clearTimeout(game.lockDown)
|
scheduler.clearTimeout(game.lockDown)
|
||||||
|
|
||||||
let heldpiece = holdQueue.piece
|
let heldpiece = holdQueue.piece
|
||||||
holdQueue.piece = piece
|
holdQueue.piece = matrix.piece
|
||||||
holdQueue.piece.holdEnabled = false
|
holdQueue.piece.holdEnabled = false
|
||||||
holdQueue.piece.locked = false
|
holdQueue.piece.locked = false
|
||||||
holdQueue.piece.position.set(0, 0)
|
holdQueue.piece.position.set(0, 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user