-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardView.js
212 lines (198 loc) · 8.57 KB
/
BoardView.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import BoardViewColorModel from './BoardViewColorModel.js';
export default class BoardView {
constructor() {
this.boardViewColorModel = new BoardViewColorModel();
this.grid = document.createElement('div');
this.pointerListeners = null;
}
_waitForEvent(element, eventType) {
return new Promise(resolve => {
element.addEventListener(eventType, resolve, {once: true})
});
}
_initializeTile(tileElement, value) {
tileElement.textContent = value;
const {color, backgroundColor} = this.boardViewColorModel.getTileColor(value);
tileElement.style.color = color;
tileElement.style.backgroundColor = backgroundColor;
const valueLength = value.toString().length;
tileElement.classList.add(valueLength > 10 ? 'tile-font-size-10' : 'tile-font-size-' + valueLength);
}
_initializeEndGameOverlay() {
const endGameOverlay =
`<div class="end-game-overlay js-end-game-overlay">
<p class="end-game-msg js-end-game-msg"></p>
<button class="btn hide-overlay-btn js-hide-overlay-btn">Dismiss</button>
</div>`
this.grid.insertAdjacentHTML('beforeend', endGameOverlay);
this.endGameOverlay = this.grid.lastChild;
this.endGameMsg = this.endGameOverlay.querySelector('.js-end-game-msg');
this.endGameOverlay.querySelector('.js-hide-overlay-btn')
.addEventListener('click', () => this._hideEndGameOverlay());
}
_showEndGameOverlay() {
this.endGameOverlay.classList.add('js-visible');
}
_hideEndGameOverlay() {
this.endGameOverlay.classList.remove('js-visible');
}
showWinMsg() {
this.endGameMsg.textContent = 'Game is Won!';
this._showEndGameOverlay();
}
showLoseMsg() {
this.endGameMsg.textContent = 'Game is Lost!';
this._showEndGameOverlay();
}
addPointerListeners(listeners) {
this.grid.addEventListener('mousedown', listeners.onMouseDown);
this.grid.addEventListener('touchstart', listeners.onTouchStart, {passive: false});
this.pointerListeners = listeners;
}
removePointerListeners() {
this.grid.removeEventListener('mousedown', this.pointerListeners.onMouseDown);
this.grid.removeEventListener('touchstart', this.pointerListeners.onTouchStart);
this.pointerListeners = null;
}
initialize(grid) {
const { gridArray } = grid;
const promises = [];
this.boardViewColorModel.updateTileColors(gridArray);
this.gridRows = gridArray.length;
this.gridCols = this.gridRows ? gridArray[0].length : 0;
this.grid.classList.add('grid');
this.grid.style.setProperty('--grid-rows', this.gridRows);
this.grid.style.setProperty('--grid-columns', this.gridCols);
for (let i = 0; i < this.gridRows; i++) {
for (let j = 0; j < this.gridCols; j++) {
const cell = document.createElement('div');
const cellObj = gridArray[i][j];
if (cellObj) {
cell.classList.add('cell');
const innerCell = document.createElement('div');
if (cellObj.tiles[0]) {
this._initializeTile(innerCell, cellObj.tiles[0].value);
innerCell.classList.add('tile');
if (cellObj.newTileAdded) {
promises.push(this._waitForEvent(innerCell, 'animationend'));
innerCell.addEventListener('animationend', () => innerCell.classList.remove('zoomin'), {once: true});
innerCell.classList.add('zoomin');
}
}
cell.appendChild(innerCell);
}
else {
cell.classList.add('missing-cell');
}
this.grid.appendChild(cell);
}
}
this._initializeEndGameOverlay();
const boardContainer = document.querySelector('.js-board-container');
boardContainer.innerHTML = '';
boardContainer.appendChild(this.grid);
return promises;
}
slide(grid) {
const { gridArray } = grid;
const promises = [];
const cells = this.grid.children;
for (let i = 0; i < this.gridRows; i++) {
for (let j = 0; j < this.gridCols; j++) {
const cellObj = gridArray[i][j];
if (!cellObj) {
continue;
}
let zIndex = 100;
for (let i = 0; i < cellObj.tiles.length; i++) {
const tileObj = cellObj.tiles[i];
if (!tileObj) {
continue;
}
const slidingTile = cells.item(tileObj.row * gridArray[0].length + tileObj.column).children[0];
slidingTile.style.zIndex = zIndex--;
if (cellObj.column !== tileObj.column || cellObj.row !== tileObj.row) {
const finalOpacity = i > 0 && cellObj.mergeValue !== null ? 0 : 1;
const tileStyle = window.getComputedStyle(slidingTile);
const cellSize = Number(tileStyle.getPropertyValue('--cell-size').slice(0, -3));
const cellGap = Number(tileStyle.getPropertyValue('--cell-gap').slice(0, -3));
slidingTile.style.setProperty('--final-opacity', finalOpacity);
if (cellObj.column !== tileObj.column) {
const x = 1 - cellSize / (Math.abs(cellObj.column - tileObj.column) * (cellSize + cellGap));
const slideDuration = Number(tileStyle.getPropertyValue('--horizontal-slide-duration').slice(0, -2));
slidingTile.style.setProperty('--cell-column', cellObj.column);
slidingTile.style.setProperty('--tile-column', tileObj.column);
slidingTile.style.setProperty('--opacity-delay', slideDuration * x + 'ms');
slidingTile.style.setProperty('--opacity-duration', slideDuration * (1 - x) + 'ms');
promises.push(this._waitForEvent(slidingTile, 'transitionend'));
slidingTile.classList.add('horizontal-slide');
}
else if (cellObj.row !== tileObj.row) {
const x = 1 - cellSize / (Math.abs(cellObj.row - tileObj.row) * (cellSize + cellGap));
const slideDuration = Number(tileStyle.getPropertyValue('--vertical-slide-duration').slice(0, -2));
slidingTile.style.setProperty('--cell-row', cellObj.row);
slidingTile.style.setProperty('--tile-row', tileObj.row);
slidingTile.style.setProperty('--opacity-delay', slideDuration * x + 'ms');
slidingTile.style.setProperty('--opacity-duration', slideDuration * (1 - x) + 'ms');
promises.push(this._waitForEvent(slidingTile, 'transitionend'));
slidingTile.classList.add('vertical-slide');
}
}
}
}
}
return promises;
}
merge(grid) {
const { gridArray } = grid;
this.boardViewColorModel.updateTileColors(gridArray);
const promises = [];
const cells = this.grid.children;
for (let i = 0; i < this.gridRows; i++) {
for (let j = 0; j < this.gridCols; j++) {
const cellObj = gridArray[i][j];
if (!cellObj) {
continue;
}
const cell = cells.item(i * gridArray[0].length + j);
const innerCell = cell.children[0];
innerCell.removeAttribute('style');
innerCell.classList = '';
innerCell.textContent = '';
if (cellObj.tiles[0] && !cellObj.newTileAdded) {
innerCell.classList.add('tile');
const tileValue = cellObj.mergeValue !== null ? cellObj.mergeValue : cellObj.tiles[0].value;
this._initializeTile(innerCell, tileValue);
if (cellObj.mergeValue !== null && cellObj.mergeValue !== undefined) {
promises.push(this._waitForEvent(innerCell, 'animationend'));
innerCell.addEventListener('animationend', () => innerCell.classList.remove('zoomin'), {once: true});
innerCell.classList.add('zoomin');
}
}
}
}
return promises;
}
addTiles(grid) {
const { gridArray } = grid;
const promises = [];
const cells = this.grid.children;
for (let i = 0; i < this.gridRows; i++) {
for (let j = 0; j < this.gridCols; j++) {
const cellObj = gridArray[i][j];
if (!cellObj) {
continue;
}
const cell = cells.item(i * gridArray[0].length + j);
const innerCell = cell.children[0];
if (cellObj.newTileAdded) {
promises.push(this._waitForEvent(innerCell, 'animationend'));
this._initializeTile(innerCell, cellObj.tiles[0].value);
innerCell.addEventListener('animationend', () => innerCell.classList.remove('zoomin'), {once: true});
innerCell.classList.add('tile', 'zoomin');
}
}
}
return promises;
}
}