|
|
|
@ -15,6 +15,7 @@ |
|
|
|
|
const main=document.querySelector('main'); |
|
|
|
|
const nav=main.querySelector('nav'); |
|
|
|
|
const canvas=main.querySelector('canvas'); |
|
|
|
|
const hud=main.querySelector('#hud'); |
|
|
|
|
|
|
|
|
|
// load config
|
|
|
|
|
const config=assets.get('config'); //TODO use an actual config module
|
|
|
|
@ -26,7 +27,7 @@ |
|
|
|
|
let currentGame=null; |
|
|
|
|
|
|
|
|
|
// forward-declare functions
|
|
|
|
|
let resizeCanvas, getLevel, startGame, handleWin, handleDeath, menu, help; |
|
|
|
|
let resizeCanvas, getLevel, startGame, handleWin, handleDeath, menu, help, restart; |
|
|
|
|
|
|
|
|
|
// handle window resize and fullscreen
|
|
|
|
|
resizeCanvas=() => { |
|
|
|
@ -119,6 +120,7 @@ |
|
|
|
|
// setup the DOM
|
|
|
|
|
nav.classList.add('hidden'); |
|
|
|
|
canvas.classList.remove('hidden'); |
|
|
|
|
hud.classList.remove('hidden'); |
|
|
|
|
|
|
|
|
|
// push some userdata to the snake
|
|
|
|
|
snek.userdata={ |
|
|
|
@ -142,12 +144,13 @@ |
|
|
|
|
// setup the DOM
|
|
|
|
|
nav.classList.remove('hidden'); |
|
|
|
|
canvas.classList.add('hidden'); |
|
|
|
|
hud.classList.add('hidden'); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// display the win popup
|
|
|
|
|
handleWin=async snek => { |
|
|
|
|
// get userdata back
|
|
|
|
|
const {category, levelId, filename}=snek.userdata; |
|
|
|
|
// hide the HUD
|
|
|
|
|
hud.classList.add('hidden'); |
|
|
|
|
|
|
|
|
|
// create and configure popup
|
|
|
|
|
let popup=new Popup("Finished!"); |
|
|
|
@ -171,10 +174,11 @@ |
|
|
|
|
|
|
|
|
|
// act on it
|
|
|
|
|
if(result=='retry') { |
|
|
|
|
startGame(category, levelId, filename); |
|
|
|
|
restart(); |
|
|
|
|
} else if(result=='menu') { |
|
|
|
|
location.hash='menu'; |
|
|
|
|
} else if(result=='next') { |
|
|
|
|
const {category, levelId}=snek.userdata; |
|
|
|
|
let nextId=(+levelId)+1; |
|
|
|
|
let {levelString}=getLevel(category, nextId) |
|
|
|
|
location.hash=levelString; |
|
|
|
@ -183,8 +187,8 @@ |
|
|
|
|
|
|
|
|
|
// display the death popup
|
|
|
|
|
handleDeath=async snek => { |
|
|
|
|
// get userdata back
|
|
|
|
|
const {category, levelId, filename}=snek.userdata; |
|
|
|
|
// hide the HUD
|
|
|
|
|
hud.classList.add('hidden'); |
|
|
|
|
|
|
|
|
|
// create and configure popup
|
|
|
|
|
let popup=new Popup("Finished!"); |
|
|
|
@ -204,12 +208,29 @@ |
|
|
|
|
|
|
|
|
|
// act on it
|
|
|
|
|
if(result=='retry') { |
|
|
|
|
startGame(category, levelId, filename); |
|
|
|
|
restart(); |
|
|
|
|
} else if(result=='menu') { |
|
|
|
|
location.hash='menu'; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// quick restart
|
|
|
|
|
restart=() => { |
|
|
|
|
if(currentGame && currentGame.playing) { |
|
|
|
|
const {category, levelId, filename}=currentGame.userdata; |
|
|
|
|
startGame(category, levelId, filename); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
window.addEventListener('keydown', e => { |
|
|
|
|
if(e.key=='r') restart(); |
|
|
|
|
}); |
|
|
|
|
(() => { |
|
|
|
|
let restartbtn=hud.appendChild(document.createElement('span')); |
|
|
|
|
restartbtn.classList.add('restart'); |
|
|
|
|
restartbtn.addEventListener('click', restart); |
|
|
|
|
restartbtn.addEventListener('touchend', restart); |
|
|
|
|
})(); |
|
|
|
|
|
|
|
|
|
// handle page navigation
|
|
|
|
|
window.addEventListener('hashchange', () => { |
|
|
|
|
const hash=location.hash.substr(1); |
|
|
|
@ -222,6 +243,7 @@ |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// enable input methods according to config
|
|
|
|
|
input.setHud(hud); |
|
|
|
|
if(config.keyboard.enabled) { |
|
|
|
|
input.enableHandler(input.availableHandlers.keyboard); |
|
|
|
|
} |
|
|
|
|