-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame.js
More file actions
50 lines (38 loc) · 1.15 KB
/
game.js
File metadata and controls
50 lines (38 loc) · 1.15 KB
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
import welcomeView from './views/welcomeView.js';
import playView from './views/playView.js';
import endGameView from './views/endGameView.js';
import { viewIds } from './constants.js';
console.log('game script loaded');
const persistedGameState = localStorage.getItem('gameState');
const gameContent = document.getElementById('gameContent');
const gameState = persistedGameState ? JSON.parse(persistedGameState) : {
name: '',
activeView: viewIds.welcome,
selectedLetters: [],
secretPhrase: '',
mistakes: 0,
};
function stateUpdate(newGameState) {
setTimeout(() => {
Object.assign(gameState, newGameState);
localStorage.setItem('gameState', JSON.stringify(gameState));
render(gameState, stateUpdate);
});
}
function clearNode(node) {
node.textContent = '';
}
const views = {
[viewIds.welcome]: welcomeView,
[viewIds.play]: playView,
[viewIds.endGame]: endGameView,
};
function getActiveView(activeView) {
return views[activeView];
}
function render(state, stateUpdate) {
const view = getActiveView(state.activeView);
clearNode(gameContent);
gameContent.appendChild(view(state, stateUpdate));
}
render(gameState, stateUpdate);