|
|
@ -26,7 +26,7 @@ |
|
|
|
let currentGame=null; |
|
|
|
let currentGame=null; |
|
|
|
|
|
|
|
|
|
|
|
// forward-declare functions
|
|
|
|
// forward-declare functions
|
|
|
|
let resizeCanvas, getLevel, startGame, stopGame, handleWin, handleDeath, menu, help, settings, restart; |
|
|
|
let resizeCanvas, getLevel, startGame, stopGame, handleWin, handleDeath, menu, help, settings, restart, updateHud; |
|
|
|
|
|
|
|
|
|
|
|
// handle window resize and fullscreen
|
|
|
|
// handle window resize and fullscreen
|
|
|
|
resizeCanvas=() => { |
|
|
|
resizeCanvas=() => { |
|
|
@ -126,6 +126,7 @@ |
|
|
|
if(evt=='tick') { |
|
|
|
if(evt=='tick') { |
|
|
|
input.framefn(); |
|
|
|
input.framefn(); |
|
|
|
snek.handleInputs(input.inputs); |
|
|
|
snek.handleInputs(input.inputs); |
|
|
|
|
|
|
|
updateHud(); |
|
|
|
} else if(evt=='win') { |
|
|
|
} else if(evt=='win') { |
|
|
|
handleWin(snek); |
|
|
|
handleWin(snek); |
|
|
|
} else if(evt=='die') { |
|
|
|
} else if(evt=='die') { |
|
|
@ -195,9 +196,10 @@ |
|
|
|
let popup=new Popup("Finished!"); |
|
|
|
let popup=new Popup("Finished!"); |
|
|
|
popup.addStrong("You won!"); |
|
|
|
popup.addStrong("You won!"); |
|
|
|
popup.addContent({ |
|
|
|
popup.addContent({ |
|
|
|
"Time": snek.playTime/1000+'s', |
|
|
|
"Time": snek.endPlayTime/1000+'s', |
|
|
|
"Score": snek.score, |
|
|
|
"Score": snek.score, |
|
|
|
"Final length": snek.length |
|
|
|
"Final length": snek.length, |
|
|
|
|
|
|
|
"Final speed": snek.speed+'tps' |
|
|
|
}); |
|
|
|
}); |
|
|
|
popup.buttons={ |
|
|
|
popup.buttons={ |
|
|
|
retry: "Retry", |
|
|
|
retry: "Retry", |
|
|
@ -233,9 +235,10 @@ |
|
|
|
let popup=new Popup("Finished!"); |
|
|
|
let popup=new Popup("Finished!"); |
|
|
|
popup.addStrong("You died..."); |
|
|
|
popup.addStrong("You died..."); |
|
|
|
popup.addContent({ |
|
|
|
popup.addContent({ |
|
|
|
"Time": snek.playTime/1000+'s', |
|
|
|
"Time": snek.endPlayTime/1000+'s', |
|
|
|
"Score": snek.score, |
|
|
|
"Score": snek.score, |
|
|
|
"Final length": snek.length |
|
|
|
"Final length": snek.length, |
|
|
|
|
|
|
|
"Final speed": snek.speed+'tps' |
|
|
|
}); |
|
|
|
}); |
|
|
|
popup.buttons={ |
|
|
|
popup.buttons={ |
|
|
|
retry: "Retry", |
|
|
|
retry: "Retry", |
|
|
@ -254,6 +257,28 @@ |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// draw status hud
|
|
|
|
|
|
|
|
updateHud=() => { |
|
|
|
|
|
|
|
// stay safe
|
|
|
|
|
|
|
|
if(!currentGame) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the actual elements
|
|
|
|
|
|
|
|
const speedDisplay=document.querySelector('#hud .speed'); |
|
|
|
|
|
|
|
const scoreDisplay=document.querySelector('#hud .score'); |
|
|
|
|
|
|
|
const timeDisplay=document.querySelector('#hud .time'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// rpad is useful
|
|
|
|
|
|
|
|
const rpad=(n, digits=2, pad=' ') => |
|
|
|
|
|
|
|
((''+n).length>=digits)?(''+n):(rpad(pad+n, digits, pad)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// actually do the hud
|
|
|
|
|
|
|
|
speedDisplay.innerText=rpad(currentGame.speed, 2, '0')+'tps'; |
|
|
|
|
|
|
|
scoreDisplay.innerText=currentGame.score; |
|
|
|
|
|
|
|
timeDisplay.innerText=rpad(Math.floor(currentGame.playTime/60000), 2, '0')+ |
|
|
|
|
|
|
|
':'+rpad(Math.floor(currentGame.playTime/1000)%60, 2, '0')+ |
|
|
|
|
|
|
|
':'+rpad(currentGame.playTime%1000, 3, '0'); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// quick restart
|
|
|
|
// quick restart
|
|
|
|
restart=() => { |
|
|
|
restart=() => { |
|
|
|
if(currentGame && currentGame.playing) { |
|
|
|
if(currentGame && currentGame.playing) { |
|
|
|