-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
233 lines (204 loc) · 8.34 KB
/
renderer.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(function (pm) {
let container;
let playArea;
let gameBoard;
let pauseButton;
let restartButton;
let paused;
let gameOver;
let win;
let level;
let nextLevelButton;
let boardWidth;
let boardHeight;
function getTileState(row, col, selectedItem, targetItem, highlights) {
let isSelected = false;
let isMoving = false;
let remove = false;
let highlight = false;
if (selectedItem && selectedItem.row === row && selectedItem.col === col) {
isSelected = true;
if (targetItem) {
isMoving = true;
remove = true;
}
}
if (targetItem && targetItem.row === row && targetItem.col === col) {
isSelected = true;
remove = true;
}
if (highlights) {
for (let i = 0; i < highlights.length; i++) {
if (highlights[i].row === row && highlights[i].col === col) {
highlight = true;
break;
}
}
}
return {isSelected, isMoving, remove, highlight};
}
function findParentWithClass(element, className) {
let parent = element;
while (parent) {
if (parent.classList.contains(className)) {
// 找到包含特定class的父节点
return parent;
}
parent = parent.parentElement;
}
// 没有找到包含特定class的父节点
return null;
}
function handleTileClick(e) {
const tile = findParentWithClass(e.target, 'tile');
if (tile && !tile.classList.contains('empty')) {
const row = parseInt(tile.dataset.row);
const col = parseInt(tile.dataset.col);
pm.clickTile(row, col);
}
}
function handleNextLevelClick() {
pm.nextLevel();
level.innerText = pm.LEVEL + 1;
updateScale();
}
let resizeHandle;
function onResize() {
if (resizeHandle) clearTimeout(resizeHandle);
resizeHandle = setTimeout(() => {
updateScale();
resizeHandle = undefined;
}, 1000);
}
function updateScale() {
boardWidth = pm.BOARD_WIDTH * (pm.TILE_SIZE + pm.TILE_SPACING) + pm.TILE_SPACING;
boardHeight = pm.BOARD_HEIGHT * (pm.TILE_SIZE + pm.TILE_SPACING) + pm.TILE_SPACING;
const screenWidth = playArea.offsetWidth - 40 /* padding */;
const screenHeight = playArea.offsetHeight - 40 /* padding */;
const ratio = Math.min(screenWidth / boardWidth, screenHeight / boardHeight);
gameBoard.style.width = boardWidth + "px";
gameBoard.style.height = boardHeight + 'px';
gameBoard.style.marginLeft = (screenWidth - boardWidth * ratio) / 2 + 'px';
gameBoard.style.transform = `scale(${ratio})`
}
function initView() {
container = document.getElementById('container');
playArea = document.getElementById('play-area');
level = document.getElementById('level');
level.innerText = pm.LEVEL + 1;
nextLevelButton = document.getElementById('next-level');
nextLevelButton.addEventListener('click', handleNextLevelClick);
gameBoard = document.getElementById('gameBoard');
gameBoard.addEventListener('click', handleTileClick);
helpButton = document.getElementById("helpButton");
helpButton.addEventListener('click', pm.help);
restartButton = document.getElementById("restartButton");
restartButton.addEventListener('click', pm.restartGame);
pauseButton = document.getElementById('pauseButton');
pauseButton.addEventListener('click', pm.pauseGame);
paused = document.getElementById("paused");
gameOver = document.getElementById("game-over");
win = document.getElementById("win");
updateScale();
window.addEventListener('resize', onResize);
window.addEventListener('keypress', (e) => {
switch (e.key.toLowerCase()) {
case 'h': pm.help(); break;
case 'r': pm.restartGame(); break;
case 'p': pm.pauseGame(); break;
}
});
}
function updateState() {
switch (pm.state) {
case 'init':
case 'run':
pauseButton.innerText = '暂停游戏';
pauseButton.classList.remove('disabled');
pauseButton.disabled = false;
paused.style.visibility = 'hidden';
gameOver.style.visibility = 'hidden';
win.style.visibility = 'hidden';
break;
case 'paused':
pauseButton.innerText = '继续游戏';
pauseButton.classList.remove('disabled');
pauseButton.disabled = false;
paused.style.visibility = 'visible';
gameOver.style.visibility = 'hidden';
win.style.visibility = 'hidden';
break;
case 'over':
pauseButton.innerText = '暂停游戏';
pauseButton.classList.add('disabled');
pauseButton.disabled = true;
paused.style.visibility = 'hidden';
gameOver.style.visibility = 'visible';
win.style.visibility = 'hidden';
break;
case 'win':
pauseButton.innerText = '暂停游戏';
pauseButton.classList.add('disabled');
pauseButton.disabled = true;
paused.style.visibility = 'hidden';
gameOver.style.visibility = 'hidden';
win.style.visibility = 'visible';
break;
}
}
function resetBoard() {
gameBoard.innerHTML = "";
for (let row = 0; row < pm.BOARD_HEIGHT; row++) {
for (let col = 0; col < pm.BOARD_WIDTH; col++) {
const tile = pm.tiles[row][col];
const elem = document.createElement('div');
elem.classList.add('tile');
elem.dataset.row = row;
elem.dataset.col = col;
elem.style.backgroundPosition = `${-Math.floor(tile.value / 10) * pm.TILE_SIZE}px ${-(tile.value % 10) * pm.TILE_SIZE + 1}px`; // 计算背景位置
elem.style.left = col * (pm.TILE_SIZE + pm.TILE_SPACING) + pm.TILE_SPACING + 'px';
elem.style.top = row * (pm.TILE_SIZE + pm.TILE_SPACING) + pm.TILE_SPACING + 'px';
gameBoard.appendChild(elem);
tile.element = elem;
}
}
}
function updateBoard(selectItem, targetItem, movePath, highlights) {
for (let row = 0; row < pm.BOARD_HEIGHT; row++) {
for (let col = 0; col < pm.BOARD_WIDTH; col++) {
const tile = pm.tiles[row][col];
let {isSelected, isMoving, remove, highlight} = getTileState(row, col, selectItem, targetItem, highlights);
if (isSelected) {
tile.element.classList.add('selected');
} else {
tile.element.classList.remove('selected');
}
if (isMoving) {
tile.element.classList.add('moving');
tile.element.style.animationName = pm.getAnimationName(movePath);
tile.element.style.animationDuration = pm.getAnimationDuration(movePath);
tile.element.style.transitionDelay = pm.getAnimationDuration(movePath);
} else {
tile.element.classList.remove('moving');
tile.element.style.animationName = null;
}
if (remove) {
tile.element.classList.add('will-remove');
tile.element.style.transitionDelay = pm.getAnimationDuration(movePath);
}
if (tile.isEmpty) {
tile.element.classList.add('empty');
}
if (highlight) {
tile.element.classList.add('highlight');
} else {
tile.element.classList.remove('highlight');
}
}
}
}
pm.initView = initView;
pm.resetBoard = resetBoard;
pm.updateBoard = updateBoard;
pm.updateState = updateState;
}).call(null, window.__pm = window.__pm || {});