optimize
This commit is contained in:
parent
d2dbdf10bb
commit
b054401625
@ -69,6 +69,21 @@ const STATE = {
|
|||||||
PAUSED: "PAUSE",
|
PAUSED: "PAUSE",
|
||||||
GAME_OVER: "GAME OVER"
|
GAME_OVER: "GAME OVER"
|
||||||
}
|
}
|
||||||
|
var ACTIONS = {}
|
||||||
|
ACTIONS[STATE.PLAYING] = {
|
||||||
|
"ArrowLeft": moveLeft,
|
||||||
|
"ArrowRight": moveRight,
|
||||||
|
"ArrowDown": softDrop,
|
||||||
|
" ": hardDrop,
|
||||||
|
"ArrowUp": rotateCW,
|
||||||
|
"z": rotateCCW,
|
||||||
|
"c": hold,
|
||||||
|
"Escape": pause
|
||||||
|
}
|
||||||
|
ACTIONS[STATE.PAUSED] = {
|
||||||
|
"Escape": resume
|
||||||
|
}
|
||||||
|
ACTIONS[STATE.GAME_OVER] = {}
|
||||||
|
|
||||||
|
|
||||||
class Scheduler {
|
class Scheduler {
|
||||||
@ -465,6 +480,7 @@ function fallingPhase() {
|
|||||||
function lockPhase() {
|
function lockPhase() {
|
||||||
if (!move(MOVEMENT.DOWN))
|
if (!move(MOVEMENT.DOWN))
|
||||||
locksDown()
|
locksDown()
|
||||||
|
requestAnimationFrame(draw)
|
||||||
}
|
}
|
||||||
|
|
||||||
function move(movement, lock=true, testMinoesPos=matrix.piece.minoesPos) {
|
function move(movement, lock=true, testMinoesPos=matrix.piece.minoesPos) {
|
||||||
@ -550,11 +566,14 @@ function gameOver() {
|
|||||||
state = STATE.GAME_OVER
|
state = STATE.GAME_OVER
|
||||||
scheduler.clearTimeout(lockPhase)
|
scheduler.clearTimeout(lockPhase)
|
||||||
scheduler.clearTimeout(locksDown)
|
scheduler.clearTimeout(locksDown)
|
||||||
|
scheduler.clearInterval(clock)
|
||||||
|
requestAnimationFrame(draw)
|
||||||
}
|
}
|
||||||
|
|
||||||
function autorepeat() {
|
function autorepeat() {
|
||||||
if (actionsToRepeat.length) {
|
if (actionsToRepeat.length) {
|
||||||
actionsToRepeat[0]()
|
actionsToRepeat[0]()
|
||||||
|
requestAnimationFrame(draw)
|
||||||
if (scheduler.timeoutTasks.has(autorepeat)) {
|
if (scheduler.timeoutTasks.has(autorepeat)) {
|
||||||
scheduler.clearTimeout(autorepeat)
|
scheduler.clearTimeout(autorepeat)
|
||||||
scheduler.setInterval(autorepeat, AUTOREPEAT_PERIOD)
|
scheduler.setInterval(autorepeat, AUTOREPEAT_PERIOD)
|
||||||
@ -567,11 +586,12 @@ function autorepeat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function keyDownHandler(e) {
|
function keyDownHandler(e) {
|
||||||
if (e.key in actions) {
|
if (!pressedKeys.has(e.key)) {
|
||||||
if (!pressedKeys.has(e.key)) {
|
pressedKeys.add(e.key)
|
||||||
pressedKeys.add(e.key)
|
if (e.key in ACTIONS[state]) {
|
||||||
action = actions[e.key]
|
action = ACTIONS[state][e.key]
|
||||||
action()
|
action()
|
||||||
|
requestAnimationFrame(draw)
|
||||||
if (REPEATABLE_ACTIONS.includes(action)) {
|
if (REPEATABLE_ACTIONS.includes(action)) {
|
||||||
actionsToRepeat.unshift(action)
|
actionsToRepeat.unshift(action)
|
||||||
scheduler.clearTimeout(autorepeat)
|
scheduler.clearTimeout(autorepeat)
|
||||||
@ -586,9 +606,9 @@ function keyDownHandler(e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function keyUpHandler(e) {
|
function keyUpHandler(e) {
|
||||||
if (e.key in actions) {
|
pressedKeys.delete(e.key)
|
||||||
pressedKeys.delete(e.key)
|
if (e.key in ACTIONS[state]) {
|
||||||
action = actions[e.key]
|
action = ACTIONS[state][e.key]
|
||||||
if (actionsToRepeat.includes(action)) {
|
if (actionsToRepeat.includes(action)) {
|
||||||
actionsToRepeat.splice(actionsToRepeat.indexOf(action), 1)
|
actionsToRepeat.splice(actionsToRepeat.indexOf(action), 1)
|
||||||
if (!actionsToRepeat.length) {
|
if (!actionsToRepeat.length) {
|
||||||
@ -642,21 +662,22 @@ function hold() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function pause() {
|
function pause() {
|
||||||
if (state == STATE.PLAYING) {
|
state = STATE.PAUSED
|
||||||
state = STATE.PAUSED
|
stats.pauseTime = Date.now() - stats.startTime
|
||||||
stats.pauseTime = Date.now() - stats.startTime
|
scheduler.clearTimeout(lockPhase)
|
||||||
scheduler.clearTimeout(lockPhase)
|
scheduler.clearTimeout(locksDown)
|
||||||
scheduler.clearTimeout(locksDown)
|
scheduler.clearTimeout(autorepeat)
|
||||||
scheduler.clearTimeout(autorepeat)
|
scheduler.clearInterval(clock)
|
||||||
}
|
}
|
||||||
else if (state == STATE.PAUSED) {
|
|
||||||
state = STATE.PLAYING
|
function resume() {
|
||||||
stats.startTime = Date.now() - stats.pauseTime
|
state = STATE.PLAYING
|
||||||
scheduler.setTimeout(lockPhase, stats.fallDelay)
|
stats.startTime = Date.now() - stats.pauseTime
|
||||||
if (matrix.piece.locked)
|
scheduler.setTimeout(lockPhase, stats.fallDelay)
|
||||||
scheduler.setTimeout(locksDown, stats.lockDelay)
|
if (matrix.piece.locked)
|
||||||
requestAnimationFrame(draw)
|
scheduler.setTimeout(locksDown, stats.lockDelay)
|
||||||
}
|
requestAnimationFrame(draw)
|
||||||
|
scheduler.setInterval(clock, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
function printTempTexts(texts) {
|
function printTempTexts(texts) {
|
||||||
@ -672,40 +693,32 @@ function delTempTexts(self) {
|
|||||||
scheduler.clearInterval(delTempTexts)
|
scheduler.clearInterval(delTempTexts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clock() {
|
||||||
|
stats.print()
|
||||||
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
holdQueue.draw()
|
holdQueue.draw()
|
||||||
stats.print()
|
stats.print()
|
||||||
matrix.draw()
|
matrix.draw()
|
||||||
nextQueue.draw()
|
nextQueue.draw()
|
||||||
|
|
||||||
if (state == STATE.PLAYING)
|
|
||||||
requestAnimationFrame(draw)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
|
tempTexts = []
|
||||||
|
|
||||||
holdQueue = new HoldQueue(document.getElementById("hold").getContext("2d"))
|
holdQueue = new HoldQueue(document.getElementById("hold").getContext("2d"))
|
||||||
stats = new Stats(document.getElementById("stats-values"))
|
stats = new Stats(document.getElementById("stats-values"))
|
||||||
matrix = new Matrix(document.getElementById("matrix").getContext("2d"))
|
matrix = new Matrix(document.getElementById("matrix").getContext("2d"))
|
||||||
nextQueue = new NextQueue(document.getElementById("next").getContext("2d"))
|
nextQueue = new NextQueue(document.getElementById("next").getContext("2d"))
|
||||||
scheduler = new Scheduler()
|
|
||||||
tempTexts = []
|
|
||||||
|
|
||||||
actions = {
|
|
||||||
"ArrowLeft": moveLeft,
|
|
||||||
"ArrowRight": moveRight,
|
|
||||||
"ArrowDown": softDrop,
|
|
||||||
" ": hardDrop,
|
|
||||||
"ArrowUp": rotateCW,
|
|
||||||
"z": rotateCCW,
|
|
||||||
"c": hold,
|
|
||||||
"Escape": pause
|
|
||||||
}
|
|
||||||
pressedKeys = new Set()
|
pressedKeys = new Set()
|
||||||
actionsToRepeat = []
|
actionsToRepeat = []
|
||||||
addEventListener("keydown", keyDownHandler, false)
|
addEventListener("keydown", keyDownHandler, false)
|
||||||
addEventListener("keyup", keyUpHandler, false)
|
addEventListener("keyup", keyUpHandler, false)
|
||||||
requestAnimationFrame(draw)
|
|
||||||
|
|
||||||
state = STATE.PLAYING
|
state = STATE.PLAYING
|
||||||
|
scheduler = new Scheduler()
|
||||||
|
scheduler.setInterval(clock, 1000)
|
||||||
this.newLevel(1)
|
this.newLevel(1)
|
||||||
}
|
}
|
Reference in New Issue
Block a user