-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
193 lines (159 loc) · 5.77 KB
/
Copy pathmain.js
File metadata and controls
193 lines (159 loc) · 5.77 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
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
// Глобальный экземпляр игры
let game;
// Инициализация игры
function initGame() {
game = new Game2048();
game.init();
}
// === ОБРАБОТКА КЛАВИАТУРЫ ===
document.addEventListener('keydown', (e) => {
if (!game) return;
// Предотвращаем скролл страницы
if (e.key.startsWith('Arrow')) {
e.preventDefault();
}
switch(e.key) {
case 'ArrowLeft':
game.move('left');
break;
case 'ArrowRight':
game.move('right');
break;
case 'ArrowUp':
game.move('up');
break;
case 'ArrowDown':
game.move('down');
break;
}
});
// === ОБРАБОТКА МЫШИ ===
let isMouseDown = false;
let mouseStartX = 0;
let mouseStartY = 0;
let mouseStartTime = 0;
// Функция для определения направления движения мыши
function getMouseDirection(startX, startY, endX, endY) {
const dx = endX - startX;
const dy = endY - startY;
const minDistance = 30; // Минимальное расстояние для срабатывания
if (Math.abs(dx) < minDistance && Math.abs(dy) < minDistance) {
return null; // Слишком маленькое движение
}
if (Math.abs(dx) > Math.abs(dy)) {
// Горизонтальное движение
return dx > 0 ? 'right' : 'left';
} else {
// Вертикальное движение
return dy > 0 ? 'down' : 'up';
}
}
// Обработка нажатия кнопки мыши
document.addEventListener('mousedown', (e) => {
// Не реагируем на правую кнопку мыши
if (e.button !== 0) return;
isMouseDown = true;
mouseStartX = e.clientX;
mouseStartY = e.clientY;
mouseStartTime = Date.now();
// Меняем курсор
document.body.style.cursor = 'grabbing';
// Предотвращаем выделение текста
e.preventDefault();
});
// Обработка движения мыши с зажатой кнопкой
document.addEventListener('mousemove', (e) => {
if (!isMouseDown || !game) return;
// Можно добавить визуальную индикацию направления
const dx = e.clientX - mouseStartX;
const dy = e.clientY - mouseStartY;
// Показываем направление (опционально)
if (Math.abs(dx) > 20 || Math.abs(dy) > 20) {
if (Math.abs(dx) > Math.abs(dy)) {
document.body.style.cursor = dx > 0 ? 'e-resize' : 'w-resize';
} else {
document.body.style.cursor = dy > 0 ? 's-resize' : 'n-resize';
}
}
});
// Обработка отпускания кнопки мыши
document.addEventListener('mouseup', (e) => {
if (!isMouseDown || !game) {
document.body.style.cursor = 'default';
return;
}
const mouseEndX = e.clientX;
const mouseEndY = e.clientY;
const mouseEndTime = Date.now();
// Сбрасываем состояние мыши
isMouseDown = false;
document.body.style.cursor = 'default';
// Проверяем, что движение было достаточно быстрым (не больше 500мс)
// и достаточно длинным
const duration = mouseEndTime - mouseStartTime;
if (duration > 500) return; // Слишком медленно
const direction = getMouseDirection(mouseStartX, mouseStartY, mouseEndX, mouseEndY);
if (direction) {
game.move(direction);
}
});
// Отмена при выходе мыши за пределы окна
document.addEventListener('mouseleave', () => {
if (isMouseDown) {
isMouseDown = false;
document.body.style.cursor = 'default';
}
});
// Отмена при нажатии правой кнопки
document.addEventListener('contextmenu', (e) => {
if (isMouseDown) {
isMouseDown = false;
document.body.style.cursor = 'default';
}
});
// ❌ ФУНКЦИЯ createControlButtons УДАЛЕНА ❌
// === ОБРАБОТКА СВАЙПОВ ДЛЯ МОБИЛЬНЫХ ===
let touchStartX = 0;
let touchStartY = 0;
let touchStartTime = 0;
document.addEventListener('touchstart', (e) => {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
touchStartTime = Date.now();
});
document.addEventListener('touchend', (e) => {
if (!game) return;
const touchEndX = e.changedTouches[0].clientX;
const touchEndY = e.changedTouches[0].clientY;
const touchEndTime = Date.now();
const dx = touchEndX - touchStartX;
const dy = touchEndY - touchStartY;
const duration = touchEndTime - touchStartTime;
// Минимальное расстояние для свайпа
const minSwipeDistance = 30;
// Максимальное время для свайпа
const maxSwipeDuration = 500;
if (duration > maxSwipeDuration) return; // Слишком медленно
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > minSwipeDistance) {
if (dx > 0) {
game.move('right');
} else {
game.move('left');
}
} else if (Math.abs(dy) > minSwipeDistance) {
if (dy > 0) {
game.move('down');
} else {
game.move('up');
}
}
});
// Запуск игры при загрузке страницы
window.onload = () => {
initGame();
// ❌ createControlButtons() УДАЛЕН - кнопки не создаются ❌
};
// Функция перезапуска для кнопки
window.restartGame = function() {
game.init();
};