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