Трубы не состыкованы между собой, вода также должна течь в вертикальных участках.
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const pipes = [];
const valves = [];
let liquidPosition = 50; // Начальная позиция жидкости
// Инициализация игры
function initGame() {
// Настройка трубы (используем координаты сегментов для образцовой изогнутой трубы)
pipes.push({ x: 50, y: 250, length: 150, direction: 'horizontal' }); // горизонтально
pipes.push({ x: 200, y: 250, length: 100, direction: 'vertical' }); // вверх
pipes.push({ x: 200, y: 150, length: 150, direction: 'horizontal' }); // горизонтально
pipes.push({ x: 350, y: 150, length: 100, direction: 'vertical' }); // вниз
pipes.push({ x: 350, y: 250, length: 150, direction: 'horizontal' }); // горизонтально
// Добавление вентилей на определенные сегменты трубы
valves.push({ x: 200, y: 250, width: 20, height: 20, closed: true });
valves.push({ x: 350, y: 150, width: 20, height: 20, closed: false });
}
// Отрисовка изогнутой трубы
function drawPipes() {
ctx.fillStyle = 'grey';
pipes.forEach(pipe => {
if (pipe.direction === 'horizontal') {
ctx.fillRect(pipe.x, pipe.y, pipe.length, 20);
} else if (pipe.direction === 'vertical') {
ctx.fillRect(pipe.x, pipe.y, 20, pipe.length);
}
});
}
// Рисование вентилей
function drawValves() {
valves.forEach(valve => {
ctx.fillStyle = valve.closed ? 'red' : 'green';
ctx.fillRect(valve.x, valve.y, valve.width, valve.height);
});
}
// Определение движения жидкости по трубам
function drawLiquid() {
ctx.fillStyle = 'blue';
// Безопасное положение для жидкости
const nextStopPosition = getNextStopPosition(liquidPosition);
// Проверка и движение жидкости
if (liquidPosition < nextStopPosition) {
liquidPosition += 3; // Скорость наполнения
}
// Зарисовываем жидкость в трубах - только горизонтальных для примера
ctx.fillRect(50, 260, Math.min(liquidPosition - 50, 150), 10); // первая часть
if (liquidPosition > 200) {
ctx.fillRect(200, 150, Math.min(liquidPosition - 200, 150), 10); // вторая часть
}
if (liquidPosition > 350) {
ctx.fillRect(350, 260, Math.min(liquidPosition - 350, 150), 10); // последняя часть
}
}
// Получение безопасной позиции для жидкости
function getNextStopPosition(currentPos) {
let stopPosition = pipes.reduce((max, pipe) => pipe.x + (pipe.direction === 'horizontal' ? pipe.length : 0), 0);
valves.forEach(valve => {
if (valve.closed && valve.x < liquidPosition + 20) {
stopPosition = Math.min(stopPosition, valve.x);
}
});
return stopPosition;
}
// Обработка кликов вентилей
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
valves.forEach(valve => {
if (mouseX > valve.x && mouseX < valve.x + valve.width &&
mouseY > valve.y && mouseY < valve.y + valve.height) {
valve.closed = !valve.closed;
}
});
});
// Запуск игры
function updateGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPipes();
drawValves();
drawLiquid();
requestAnimationFrame(updateGame);
}
initGame();
requestAnimationFrame(updateGame);