177 lines
5.9 KiB
JavaScript
177 lines
5.9 KiB
JavaScript
// © Головин Г.Г., Обработка действий пользователя, 2023
|
|
'use strict';
|
|
// коды кнопок на клавиатуре
|
|
const KEY = {PAUSE:19,SPACE:32,LEFT:37,UP:38,RIGHT:39,DOWN:40};
|
|
// типы объёмного изображения
|
|
const VOLUME = {FLAT:0,PARALLEL:1,PERSPECTIVE:2};
|
|
// текущий тип изображения
|
|
let vol = VOLUME.FLAT;
|
|
// кнопка нажата и удерживается
|
|
function keyPressed(caller) {
|
|
if (status == GAME.OVER) return;
|
|
switch (caller.keyCode) {
|
|
case KEY.PAUSE: {
|
|
status = GAME.PAUSE;
|
|
statusView.refresh();
|
|
return;
|
|
}
|
|
case KEY.LEFT: {
|
|
caller.preventDefault();
|
|
moveFigureLeft();
|
|
break;
|
|
}
|
|
case KEY.RIGHT: {
|
|
caller.preventDefault();
|
|
moveFigureRight();
|
|
break;
|
|
}
|
|
case KEY.SPACE:
|
|
case KEY.UP: {
|
|
caller.preventDefault();
|
|
rotateFigure();
|
|
break;
|
|
}
|
|
case KEY.DOWN: {
|
|
caller.preventDefault();
|
|
rapidFall = true;
|
|
if (sleepTimeout != undefined) {
|
|
clearTimeout(sleepTimeout);
|
|
stepDown();
|
|
}
|
|
break;
|
|
}
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
if (status == GAME.LEVEL || status == GAME.PAUSE) {
|
|
status = GAME.RUN;
|
|
setTimeout(figureFall, 60);
|
|
}
|
|
}
|
|
// кнопка отпущена
|
|
function keyReleased(caller) {
|
|
if (caller.keyCode == KEY.DOWN) {
|
|
rapidFall = false;
|
|
}
|
|
}
|
|
// изменить количество строк
|
|
function changeRows(caller) {
|
|
rows = caller.target.valueAsNumber;
|
|
container.changeSize();
|
|
t0.reCalc();
|
|
tv2.reCalc();
|
|
refreshParams();
|
|
prepareNewGame();
|
|
}
|
|
// изменить количество колонок
|
|
function changeColumns(caller) {
|
|
columns = caller.target.valueAsNumber;
|
|
container.changeSize();
|
|
t0.reCalc();
|
|
tv2.reCalc();
|
|
refreshParams();
|
|
prepareNewGame();
|
|
}
|
|
// переключить режим улитки
|
|
function changeSnailMode(caller) {
|
|
if (caller.target.checked)
|
|
reduction=REDUCTION_SNAIL;
|
|
else
|
|
reduction=REDUCTION_STEP;
|
|
}
|
|
// изменить тип объёмного изображения
|
|
function changeVolume(caller) {
|
|
const value = caller.target.value;
|
|
const old = vol;
|
|
if (value=="off") vol=VOLUME.FLAT;
|
|
if (value=="parallel") vol=VOLUME.PARALLEL;
|
|
if (value=="perspective") vol=VOLUME.PERSPECTIVE;
|
|
refreshDisabled();
|
|
repaint(false);
|
|
}
|
|
// изменяем прозрачность фигур
|
|
function changeAlpha(caller) {
|
|
colors.alpha=caller.target.valueAsNumber;
|
|
}
|
|
// поворачиваем кубики, изменяем текущий угол
|
|
function rotate(axis, caller) {
|
|
deg[axis] = caller.target.valueAsNumber;
|
|
prepare3D();
|
|
}
|
|
// вертикальная корректировка
|
|
function changeTv1(caller) {
|
|
tv1.y = caller.target.valueAsNumber;
|
|
}
|
|
// центральная точка на экране наблюдателя
|
|
function changeTv2(axis, caller) {
|
|
tv2[axis] = caller.target.valueAsNumber;
|
|
}
|
|
// удалённость источника проекции
|
|
function changeDistance2(caller) {
|
|
d2=caller.target.valueAsNumber;
|
|
}
|
|
// двигать центральную точку
|
|
function showCenter(caller) {
|
|
tv2.show=!tv2.show;
|
|
}
|
|
// обновить игру и все настройки
|
|
function reload() {
|
|
vol=VOLUME.FLAT;
|
|
field3D=[];
|
|
colors.setDefault();
|
|
deg.setDefault();
|
|
tv1.reCalc();
|
|
tv2.reCalc();
|
|
reduction=REDUCTION_STEP;
|
|
prepareNewGame();
|
|
refreshParams();
|
|
}
|
|
// обновить отображение параметров
|
|
function refreshParams() {
|
|
document.getElementById('speedometer').value=0;
|
|
document.getElementById('snail').checked=(reduction==REDUCTION_SNAIL);
|
|
document.getElementById('alpha').value=colors.alpha;
|
|
document.getElementById('oAlpha').value=colors.alpha + '%';
|
|
document.getElementById('off').checked=(vol==VOLUME.FLAT);
|
|
document.getElementById('parallel').checked=(vol==VOLUME.PARALLEL);
|
|
document.getElementById('perspective').checked=(vol==VOLUME.PERSPECTIVE);
|
|
document.getElementById('rotateX').value = deg.x;
|
|
document.getElementById('rotateXo').value = deg.x + '°';
|
|
document.getElementById('rotateY').value = deg.y;
|
|
document.getElementById('rotateYo').value = deg.y + '°';
|
|
document.getElementById('rotateZ').value = deg.z;
|
|
document.getElementById('rotateZo').value = deg.z + '°';
|
|
document.getElementById('tv1Y').value = tv1.y;
|
|
document.getElementById('tv1Yo').value = tv1.y;
|
|
document.getElementById('center').checked = tv2.show;
|
|
document.getElementById('tv2X').max = container.canvas.width;
|
|
document.getElementById('tv2X').value = tv2.x;
|
|
document.getElementById('tv2Xo').value = tv2.x;
|
|
document.getElementById('tv2Y').max = container.canvas.height;
|
|
document.getElementById('tv2Y').value = tv2.y;
|
|
document.getElementById('tv2Yo').value = tv2.y;
|
|
document.getElementById('tv2Z').max = d2/2;
|
|
document.getElementById('tv2Z').value = tv2.z;
|
|
document.getElementById('tv2Zo').value = tv2.z;
|
|
document.getElementById('dist').max = d2max;
|
|
document.getElementById('dist').value = d2;
|
|
document.getElementById('oDist').value = d2;
|
|
refreshDisabled();
|
|
}
|
|
// обновить доступность блоков
|
|
function refreshDisabled() {
|
|
document.getElementById('rotateX').disabled=(vol==VOLUME.FLAT);
|
|
document.getElementById('rotateY').disabled=(vol==VOLUME.FLAT);
|
|
document.getElementById('rotateZ').disabled=(vol==VOLUME.FLAT);
|
|
document.getElementById('tv1Y').disabled=(vol!=VOLUME.PARALLEL);
|
|
document.getElementById('center').disabled=(vol!=VOLUME.PERSPECTIVE);
|
|
document.getElementById('tv2X').disabled=(vol!=VOLUME.PERSPECTIVE);
|
|
document.getElementById('tv2Y').disabled=(vol!=VOLUME.PERSPECTIVE);
|
|
document.getElementById('tv2Z').disabled=(vol!=VOLUME.PERSPECTIVE);
|
|
document.getElementById('dist').disabled=(vol!=VOLUME.PERSPECTIVE);
|
|
}
|
|
// после загрузки всех частей страницы
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
refreshParams();
|
|
});
|