-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (67 loc) · 2.41 KB
/
script.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
import Updater from './Updater.js';
import Model from './Model.js';
import View from './View.js';
const updater = new Updater();
updater.update();
const pageLoadingEl = document.querySelector('.js-page-loading');
pageLoadingEl.remove();
const model = new Model();
const view = new View();
const modelHandlers = {
gameMove : {
moveUp: model.gameMoveUp.bind(model),
moveRight: model.gameMoveRight.bind(model),
moveDown: model.gameMoveDown.bind(model),
moveLeft: model.gameMoveLeft.bind(model)
}
}
/* ---------------------------------------------- */
model.addChangeListener('resetBestScoreEvent', () => {
const bestScore = model.getBestScore();
localStorage.setItem('game-2048-best-score', JSON.stringify(bestScore));
view.setBestScore(bestScore);
});
model.addChangeListener('initializeModelEvent', () => {
const bestScore = model.getBestScore();
localStorage.setItem('game-2048-best-score', JSON.stringify(bestScore));
view.initialize({bestScore});
});
model.addChangeListener('initializeGameModelEvent', async () => {
const game = model.getGameObj();
localStorage.setItem('game-2048', JSON.stringify(game));
await view.initializeGame({game, modelHandlers});
});
model.addChangeListener('gameMoveEvent', async () => {
const game = model.getGameObj();
const bestScore = model.getBestScore();
localStorage.setItem('game-2048-best-score', JSON.stringify(bestScore));
await Promise.all(view.slideGameTiles(game));
const mergePromises = view.mergeGameTiles(game);
view.updateGameScore(game);
view.setBestScore(bestScore);
await Promise.all([...view.addGameTiles(game), ...mergePromises]);
model.purgeGameModel();
});
model.addChangeListener('purgeGameModelEvent', () => {
const game = model.getGameObj();
localStorage.setItem('game-2048', JSON.stringify(game));
view.updateGameStatus(game);
});
model.addChangeListener('gameNoOpEvent', () => view.setGameReady());
/* ---------------------------------------------- */
view.bindResetBestScore(() => {
model.resetBestScore();
});
view.bindStartNewGame(() => {
startGame();
});
/* ---------------------------------------------- */
model.initialize({
bestScore: JSON.parse(localStorage.getItem('game-2048-best-score'))
});
startGame({
game: JSON.parse(localStorage.getItem('game-2048'))
});
function startGame(obj) {
model.initializeGame(obj);
}