-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
115 lines (85 loc) · 2.89 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
function main() {
var mainElement = document.querySelector('#site-main');
var stage;
var game;
var dragonBallSong = new Audio('./sounds/dragonSongCatala.mp3');
// -- SPLASH
var splashElement;
var startGameButton;
var handleStartClick = function () {
destroySplash();
buildGame();
};
function buildSplash() {
stage = 'splash';
// create dom elements
splashElement = document.createElement('div');
splashElement.setAttribute('id', 'splash');
// create tittle
// var title = document.createElement('h1');
// title.innerText = 'TH3 G4M3';
// splashElement.appendChild(title);
// create button
startGameButton = document.createElement('button');
startGameButton.innerText = 'Start';
splashElement.appendChild(startGameButton);
// apppend to site-main
mainElement.appendChild(splashElement);
// bind click on start play button
startGameButton.addEventListener('click', handleStartClick);
dragonBallSong.play();
}
function destroySplash() {
// unbind click on start play button
startGameButton.removeEventListener('click', handleStartClick);
// remove splash from dom
splashElement.remove();
}
// -- GAME
function buildGame() {
stage = '1';
game = new Game(mainElement);
game.onGameOver(function () {
destroyGame();
buildGameOver(game.score);
});
}
function destroyGame() {
game.destroy();
}
// -- GAME OVER
var gameOverElement;
var playAgainButton;
var handlePlayAgainClick = function () {
destroyGameOver();
buildGame();
};
function buildGameOver(score) {
stage = 'gameOver';
// create dom elements
gameOverElement = document.createElement('div');
gameOverElement.setAttribute('id', 'game-over');
var title = document.createElement('h1');
title.innerText = 'Game Over';
gameOverElement.appendChild(title);
var yourScore = document.createElement('h2');
yourScore.innerText = 'Your score: ' + score;
gameOverElement.appendChild(yourScore);
playAgainButton = document.createElement('button');
playAgainButton.innerText = 'Play Again';
gameOverElement.appendChild(playAgainButton);
// apppend to site-main
mainElement.appendChild(gameOverElement);
// bind click on start play button
playAgainButton.addEventListener('click', handlePlayAgainClick);
}
function destroyGameOver() {
// unbind click on start play button
playAgainButton.removeEventListener('click', handlePlayAgainClick);
// remove gameOver from dom
gameOverElement.remove();
}
buildSplash();
}
window.onload = main;