SandGame/index.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2026-10-22 14:05:51 +00:00
import { canvas, resize } from './state.js';
import { setupUI, handleInput } from './ui.js';
import { update } from './physics.js';
import { draw } from './renderer.js';
import { loadSettings, config } from './config.js';
function init() {
loadSettings();
resize();
window.addEventListener('resize', resize);
setupUI();
// Input Handling
let isMouseDown = false;
canvas.addEventListener('mousedown', (e) => {
isMouseDown = true;
handleInput(e);
});
canvas.addEventListener('mouseup', () => isMouseDown = false);
canvas.addEventListener('mousemove', (e) => {
if (isMouseDown) handleInput(e);
});
canvas.addEventListener('touchstart', (e) => {
isMouseDown = true;
handleInput(e);
e.preventDefault();
}, { passive: false });
canvas.addEventListener('touchend', (e) => {
if (e.touches.length === 0) isMouseDown = false;
});
canvas.addEventListener('touchmove', (e) => {
if (isMouseDown) handleInput(e);
e.preventDefault();
}, { passive: false });
gameLoop();
}
let updateAccumulator = 0;
function gameLoop() {
if (!config.time.paused) {
updateAccumulator += config.time.speed;
while (updateAccumulator >= 1) {
update();
updateAccumulator -= 1;
if (updateAccumulator > 50) {
updateAccumulator = 0;
break;
}
}
}
draw();
requestAnimationFrame(gameLoop);
}
init();