2019-04-06 17:19:26 +00:00
|
|
|
const sounds = {
|
2019-04-10 15:58:02 +00:00
|
|
|
hitBlock: new SoundManager('assets/hit_block.wav'),
|
|
|
|
hitCoin: new SoundManager('assets/hit_coin.wav'),
|
|
|
|
hitBall: new SoundManager('assets/hit_ball.wav'),
|
|
|
|
roundOver: new SoundManager('assets/round_success.wav'),
|
|
|
|
gameOver: new SoundManager('assets/game_over.wav'),
|
2019-04-06 17:19:26 +00:00
|
|
|
};
|
|
|
|
|
2019-04-10 16:02:34 +00:00
|
|
|
const containerEl = document.getElementsByClassName('container')[0];
|
|
|
|
const highscoreEl = document.getElementById('highscore');
|
|
|
|
const coinCountEl = document.getElementById('coin-count');
|
|
|
|
|
|
|
|
const resumeBtn = document.getElementById('resumeBtn');
|
|
|
|
const mainMenuBtn = document.getElementById('mainMenuBtn');
|
|
|
|
const restartBtn = document.getElementById('restartBtn');
|
|
|
|
|
|
|
|
let game;
|
|
|
|
|
2019-04-06 17:19:26 +00:00
|
|
|
let coinCount = 0;
|
2019-04-10 16:02:34 +00:00
|
|
|
let highscore = 0;
|
|
|
|
|
|
|
|
const unlockedSkins = [];
|
|
|
|
let selectedSkin = 0;
|
|
|
|
|
|
|
|
function loadFromLocalstorage() {
|
|
|
|
const data = localStorage.getItem('data');
|
|
|
|
|
|
|
|
if (!!data) {
|
|
|
|
const items = data.split(';');
|
|
|
|
setHighScore(parseInt(items[0]));
|
|
|
|
setCoinCount(parseInt(items[1]));
|
|
|
|
selectedSkin = parseInt(items[2]);
|
|
|
|
unlockedSkins.push(...JSON.parse(items[3]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveToLocalstorage() {
|
|
|
|
let data = highscore + ';' + coinCount + ';' + selectedSkin + ';';
|
|
|
|
data += JSON.stringify(unlockedSkins);
|
|
|
|
|
|
|
|
localStorage.setItem('data', data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setHighScore(newHighScore) {
|
|
|
|
highscore = newHighScore;
|
|
|
|
|
|
|
|
highscoreEl.innerText = highscore;
|
|
|
|
}
|
2019-04-06 17:19:26 +00:00
|
|
|
|
2019-04-10 16:02:34 +00:00
|
|
|
function setCoinCount(newCoinCount) {
|
|
|
|
coinCount = newCoinCount;
|
|
|
|
|
|
|
|
coinCountEl.innerText = coinCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadFromLocalstorage();
|
2019-04-06 17:19:26 +00:00
|
|
|
|
|
|
|
function onGameOver() {
|
2019-04-10 16:02:34 +00:00
|
|
|
if (game.roundNumber > highscore) {
|
|
|
|
setHighScore(game.roundNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveToLocalstorage();
|
|
|
|
|
|
|
|
containerEl.classList.add('game-over');
|
|
|
|
}
|
2019-04-06 17:19:26 +00:00
|
|
|
|
2019-04-10 16:02:34 +00:00
|
|
|
function startGame() {
|
|
|
|
game = new Game(containerEl);
|
|
|
|
game.start();
|
2019-04-06 17:19:26 +00:00
|
|
|
}
|
2019-04-10 16:02:34 +00:00
|
|
|
|
|
|
|
function stopGame() {
|
|
|
|
game.destroy();
|
|
|
|
game = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
startGame();
|
|
|
|
|
|
|
|
restartBtn.addEventListener('click', () => {
|
|
|
|
stopGame();
|
|
|
|
startGame();
|
|
|
|
|
|
|
|
containerEl.classList.remove('game-over');
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
saveToLocalstorage();
|
|
|
|
});
|