fix lock
This commit is contained in:
parent
11675fa9a2
commit
d590c41f95
71
app.js
71
app.js
@ -46,6 +46,7 @@ const TRANSLATION = {
|
|||||||
NONE : P( 0, 0),
|
NONE : P( 0, 0),
|
||||||
LEFT : P(-1, 0),
|
LEFT : P(-1, 0),
|
||||||
RIGHT: P( 1, 0),
|
RIGHT: P( 1, 0),
|
||||||
|
UP : P( 0, 1),
|
||||||
DOWN : P( 0, -1),
|
DOWN : P( 0, -1),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,10 +74,10 @@ class Matrix extends THREE.Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lock() {
|
lock() {
|
||||||
|
this.piece.locking = false
|
||||||
let minoes = Array.from(this.piece.children)
|
let minoes = Array.from(this.piece.children)
|
||||||
minoes.forEach(mino => {
|
minoes.forEach(mino => {
|
||||||
mino.position.add(this.piece.position)
|
mino.position.add(this.piece.position)
|
||||||
mino.material = this.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
|
||||||
@ -236,6 +237,8 @@ Ghost.prototype.minoesPosition = [
|
|||||||
[P(0, 0, 0), P(0, 0, 0), P(0, 0, 0), P(0, 0, 0)],
|
[P(0, 0, 0), P(0, 0, 0), P(0, 0, 0), P(0, 0, 0)],
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const lockEvent = new Event("pieceLocked")
|
||||||
|
|
||||||
class Tetromino extends AbstractTetromino {
|
class Tetromino extends AbstractTetromino {
|
||||||
static randomBag = []
|
static randomBag = []
|
||||||
static get random() {
|
static get random() {
|
||||||
@ -249,41 +252,30 @@ class Tetromino extends AbstractTetromino {
|
|||||||
this.rotationPoint4Used = false
|
this.rotationPoint4Used = false
|
||||||
this.holdEnabled = true
|
this.holdEnabled = true
|
||||||
this.facing = 0
|
this.facing = 0
|
||||||
this.locked = false
|
this.locking = false
|
||||||
}
|
|
||||||
|
|
||||||
set locked(locked) {
|
|
||||||
this._locked = locked
|
|
||||||
if (locked) {
|
|
||||||
this.children.forEach(mino => mino.material = this.lockedMaterial)
|
|
||||||
scene.remove(this.ghost)
|
|
||||||
scheduler.resetTimeout(game.lockDown, stats.lockDelay)
|
|
||||||
} else {
|
|
||||||
//this.children.forEach(mino => mino.material = this.material)
|
|
||||||
scene.add(this.ghost)
|
|
||||||
scheduler.clearTimeout(game.lockDown, stats.lockDelay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get locked() {
|
|
||||||
return this._locked
|
|
||||||
}
|
}
|
||||||
|
|
||||||
move(translation, rotatedFacing, rotationPoint) {
|
move(translation, rotatedFacing, rotationPoint) {
|
||||||
if (this.canMove(translation, rotatedFacing)) {
|
if (this.canMove(translation, rotatedFacing)) {
|
||||||
scheduler.clearTimeout(game.lockDown)
|
|
||||||
this.position.add(translation)
|
this.position.add(translation)
|
||||||
this.rotatedLast = rotatedFacing
|
this.rotatedLast = rotatedFacing
|
||||||
if (rotatedFacing != undefined) {
|
if (rotatedFacing != undefined) {
|
||||||
this.facing = rotatedFacing
|
this.facing = rotatedFacing
|
||||||
if (rotationPoint == 4) this.rotationPoint4Used = true
|
if (rotationPoint == 4) this.rotationPoint4Used = true
|
||||||
}
|
}
|
||||||
this.locked = !this.canMove(TRANSLATION.DOWN)
|
if (this.canMove(TRANSLATION.DOWN)) {
|
||||||
|
this.locking = false
|
||||||
|
scheduler.clearTimeout(this.lock)
|
||||||
|
} else {
|
||||||
|
scheduler.resetTimeout(this.lock, stats.lockDelay)
|
||||||
|
this.locking = true
|
||||||
|
}
|
||||||
this.updateGhost()
|
this.updateGhost()
|
||||||
return true
|
return true
|
||||||
} else if (translation == TRANSLATION.DOWN) {
|
} else if (translation == TRANSLATION.DOWN) {
|
||||||
this.locked = true
|
this.locking = true
|
||||||
scheduler.setTimeout(game.lockDown, stats.lockDelay)
|
if (!scheduler.timeoutTasks.has(this.lock))
|
||||||
|
scheduler.setTimeout(this.lock, stats.lockDelay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,6 +286,20 @@ class Tetromino extends AbstractTetromino {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set locking(locking) {
|
||||||
|
if (locking) {
|
||||||
|
this.children.forEach(mino => mino.material = this.lockedMaterial)
|
||||||
|
scene.remove(this.ghost)
|
||||||
|
} else {
|
||||||
|
this.children.forEach(mino => mino.material = this.material)
|
||||||
|
scene.add(this.ghost)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lock() {
|
||||||
|
this.dispatchEvent(lockEvent)
|
||||||
|
}
|
||||||
|
|
||||||
updateGhost() {
|
updateGhost() {
|
||||||
this.ghost.position.copy(this.position)
|
this.ghost.position.copy(this.position)
|
||||||
this.ghost.minoesPosition = this.minoesPosition
|
this.ghost.minoesPosition = this.minoesPosition
|
||||||
@ -664,12 +670,7 @@ let game = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
lockDown: function() {
|
lockDown: function() {
|
||||||
if (matrix.piece.canMove(TRANSLATION.DOWN)) {
|
scheduler.clearTimeout(matrix.piece.lock)
|
||||||
scheduler.resetTimeout(game.lockDown)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
scheduler.clearTimeout(game.lockDown)
|
|
||||||
scheduler.clearInterval(game.fall)
|
scheduler.clearInterval(game.fall)
|
||||||
|
|
||||||
if (matrix.lock(matrix.piece)) {
|
if (matrix.lock(matrix.piece)) {
|
||||||
@ -698,7 +699,7 @@ let game = {
|
|||||||
stats.clock.stop()
|
stats.clock.stop()
|
||||||
|
|
||||||
scheduler.clearInterval(game.fall)
|
scheduler.clearInterval(game.fall)
|
||||||
scheduler.clearTimeout(game.lockDown)
|
scheduler.clearTimeout(matrix.piece.lock)
|
||||||
scheduler.clearTimeout(repeat)
|
scheduler.clearTimeout(repeat)
|
||||||
scheduler.clearInterval(autorepeat)
|
scheduler.clearInterval(autorepeat)
|
||||||
|
|
||||||
@ -710,7 +711,7 @@ let game = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
over: function() {
|
over: function() {
|
||||||
matrix.piece.locked = false
|
matrix.piece.locking = false
|
||||||
|
|
||||||
document.onkeydown = null
|
document.onkeydown = null
|
||||||
renderer.domElement.onblur = null
|
renderer.domElement.onblur = null
|
||||||
@ -726,6 +727,8 @@ let game = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.addEventListener("pieceLocked", game.lockDown)
|
||||||
|
|
||||||
let playerActions = {
|
let playerActions = {
|
||||||
moveLeft: () => matrix.piece.move(TRANSLATION.LEFT),
|
moveLeft: () => matrix.piece.move(TRANSLATION.LEFT),
|
||||||
|
|
||||||
@ -740,7 +743,7 @@ let playerActions = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
hardDrop: function () {
|
hardDrop: function () {
|
||||||
scheduler.clearTimeout(game.lockDown)
|
scheduler.clearTimeout(matrix.piece.lock)
|
||||||
world.hardDropSound.play()
|
world.hardDropSound.play()
|
||||||
if (settings.sfxVolume) {
|
if (settings.sfxVolume) {
|
||||||
world.hardDropSound.currentTime = 0
|
world.hardDropSound.currentTime = 0
|
||||||
@ -755,12 +758,12 @@ let playerActions = {
|
|||||||
hold: function () {
|
hold: function () {
|
||||||
if (matrix.piece.holdEnabled) {
|
if (matrix.piece.holdEnabled) {
|
||||||
scheduler.clearInterval(game.fall)
|
scheduler.clearInterval(game.fall)
|
||||||
scheduler.clearTimeout(game.lockDown)
|
scheduler.clearTimeout(matrix.piece.lock)
|
||||||
|
|
||||||
let heldpiece = holdQueue.piece
|
let heldpiece = holdQueue.piece
|
||||||
holdQueue.piece = matrix.piece
|
holdQueue.piece = matrix.piece
|
||||||
holdQueue.piece.holdEnabled = false
|
holdQueue.piece.holdEnabled = false
|
||||||
holdQueue.piece.locked = false
|
holdQueue.piece.locking = false
|
||||||
holdQueue.piece.position.set(0, 0)
|
holdQueue.piece.position.set(0, 0)
|
||||||
holdQueue.piece.facing = FACING.NORTH
|
holdQueue.piece.facing = FACING.NORTH
|
||||||
holdQueue.add(holdQueue.piece)
|
holdQueue.add(holdQueue.piece)
|
||||||
|
15
jsm/utils.js
15
jsm/utils.js
@ -9,25 +9,26 @@ class Scheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearInterval(func) {
|
clearInterval(func) {
|
||||||
if (this.intervalTasks.has(func))
|
if (this.intervalTasks.has(func)) {
|
||||||
window.clearInterval(this.intervalTasks.get(func))
|
window.clearInterval(this.intervalTasks.get(func))
|
||||||
this.intervalTasks.delete(func)
|
this.intervalTasks.delete(func)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(func, delay, ...args) {
|
setTimeout(func, delay, ...args) {
|
||||||
if (!this.timeoutTasks.has(func))
|
this.timeoutTasks.set(func, window.setTimeout(func, delay, ...args))
|
||||||
this.timeoutTasks.set(func, window.setTimeout(func, delay, ...args))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clearTimeout(func) {
|
clearTimeout(func) {
|
||||||
if (this.timeoutTasks.has(func))
|
if (this.timeoutTasks.has(func)) {
|
||||||
window.clearTimeout(this.timeoutTasks.get(func))
|
window.clearTimeout(this.timeoutTasks.get(func))
|
||||||
this.timeoutTasks.delete(func)
|
this.timeoutTasks.delete(func)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resetTimeout(func, delay, ...args) {
|
resetTimeout(func, delay, ...args) {
|
||||||
this.clearTimeout(func)
|
this.clearTimeout(func)
|
||||||
this.timeoutTasks.set(func, window.setTimeout(func, delay, ...args))
|
this.setTimeout(func, delay, ...args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user