This commit is contained in:
2025-10-20 22:31:31 +05:00
parent a9af432e27
commit 2c42ab5bdb

View File

@@ -17,6 +17,83 @@ let isPanning = false;
let startPanX = 0;
let startPanY = 0;
// Функция обработки ошибок во время воспроизведения
function handlePlaybackError(message) {
alert(`❌ Ошибка воспроизведения: ${message}`);
pauseVisualization();
// Сбрасываем счёт, но не очищаем визуализацию
if (trajectory && trajectory.length > 0) {
trajectory.forEach(step => {
step.score = 0;
step.passedCheckpoints = 0;
});
updateStepInfo();
drawMap();
}
console.error('Ошибка воспроизведения:', message);
}
// Функция проверки корректности ускорения
function validateAcceleration(ax, ay, x, y, vx, vy) {
// Проверка выезда за границы карты
if (x < 0 || x >= width || y < 0 || y >= height) {
return `Выезд за границы карты на позиции (${x.toFixed(1)}, ${y.toFixed(1)})`;
}
// Получаем тип текущей ячейки
const cellX = Math.floor(x);
const cellY = Math.floor(y);
const cellType = map[cellY] ? map[cellY][cellX] : -1;
// Проверка правил для разных типов ячеек
switch (cellType) {
case 0: // Обычная дорога
if (ax < -2 || ax > 2 || ay < -2 || ay > 2) {
return `На дороге ускорение должно быть от -2 до +2, получено (${ax}, ${ay})`;
}
break;
case 1: // Камень
// Можно проезжать через камни, но нельзя на них останавливаться
if (vx === 0 && vy === 0 && ax === 0 && ay === 0) {
return `Нельзя останавливаться на камне на позиции (${cellX}, ${cellY})`;
}
if (ax < -2 || ax > 2 || ay < -2 || ay > 2) {
return `На камне ускорение должно быть от -2 до +2, получено (${ax}, ${ay})`;
}
break;
case 2: // Снег
if (ax < -1 || ax > 1 || ay < -1 || ay > 1) {
return `На снегу ускорение должно быть от -1 до +1, получено (${ax}, ${ay})`;
}
break;
case 3: // Лёд
if (ax !== 0 || ay !== 0) {
return `На льду нельзя менять ускорение, получено (${ax}, ${ay})`;
}
break;
case 4: // Чекпоинт
if (ax < -2 || ax > 2 || ay < -2 || ay > 2) {
return `На чекпоинте ускорение должно быть от -2 до +2, получено (${ax}, ${ay})`;
}
break;
case 5: // Старт
if (ax < -2 || ax > 2 || ay < -2 || ay > 2) {
return `На старте ускорение должно быть от -2 до +2, получено (${ax}, ${ay})`;
}
break;
default:
return `Неизвестный тип ячейки ${cellType} на позиции (${cellX}, ${cellY})`;
}
return null; // Нет ошибок
}
// Canvas элементы
const canvas = document.getElementById('mapCanvas');
const ctx = canvas.getContext('2d');
@@ -317,7 +394,25 @@ function playVisualization() {
playbackInterval = setInterval(() => {
if (currentStep < trajectory.length - 1) {
currentStep++;
const nextStep = currentStep + 1;
const current = trajectory[currentStep];
const next = trajectory[nextStep];
// Проверяем корректность ускорения для следующего шага
const error = validateAcceleration(next.ax, next.ay, current.x, current.y, current.vx, current.vy);
if (error) {
handlePlaybackError(`Шаг ${nextStep + 1}: ${error}`);
return;
}
// Проверяем корректность новой позиции
const positionError = validateAcceleration(0, 0, next.x, next.y, next.vx, next.vy);
if (positionError && positionError.includes('Выезд за границы')) {
handlePlaybackError(`Шаг ${nextStep + 1}: ${positionError}`);
return;
}
currentStep = nextStep;
updateStepInfo();
drawMap();
} else {
@@ -350,7 +445,25 @@ function stepForward() {
if (!trajectory || trajectory.length === 0) return;
if (currentStep < trajectory.length - 1) {
currentStep++;
const nextStep = currentStep + 1;
const current = trajectory[currentStep];
const next = trajectory[nextStep];
// Проверяем корректность ускорения для следующего шага
const error = validateAcceleration(next.ax, next.ay, current.x, current.y, current.vx, current.vy);
if (error) {
handlePlaybackError(`Шаг ${nextStep + 1}: ${error}`);
return;
}
// Проверяем корректность новой позиции
const positionError = validateAcceleration(0, 0, next.x, next.y, next.vx, next.vy);
if (positionError && positionError.includes('Выезд за границы')) {
handlePlaybackError(`Шаг ${nextStep + 1}: ${positionError}`);
return;
}
currentStep = nextStep;
updateStepInfo();
drawMap();
}