mirror of
https://github.com/tiennm99/try-claudekit.git
synced 2026-04-17 13:21:43 +00:00
Browser-based physics puzzle game where players drop fruits that merge into larger fruits on collision, using Matter.js for 2D physics and Canvas2D for rendering. Includes 11-fruit progression chain, scoring, game-over detection, mouse/touch input, and Vitest test suite.
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
import { FRUITS } from './fruits.js';
|
|
import { CONTAINER_X, CONTAINER_WIDTH } from './constants.js';
|
|
|
|
export function clampX(x, fruitTier) {
|
|
const radius = FRUITS[fruitTier].radius;
|
|
const minX = CONTAINER_X + radius;
|
|
const maxX = CONTAINER_X + CONTAINER_WIDTH - radius;
|
|
return Math.max(minX, Math.min(maxX, x));
|
|
}
|
|
|
|
export function setupInput(canvas, game) {
|
|
function getCanvasX(clientX) {
|
|
const rect = canvas.getBoundingClientRect();
|
|
const scaleX = canvas.width / rect.width;
|
|
return (clientX - rect.left) * scaleX;
|
|
}
|
|
|
|
canvas.addEventListener('mousemove', (e) => {
|
|
game.setCursorX(getCanvasX(e.clientX));
|
|
});
|
|
|
|
canvas.addEventListener('click', (e) => {
|
|
if (game.state.isGameOver) {
|
|
game.restart();
|
|
} else {
|
|
game.drop();
|
|
}
|
|
});
|
|
|
|
canvas.addEventListener('touchmove', (e) => {
|
|
e.preventDefault();
|
|
const touch = e.touches[0];
|
|
game.setCursorX(getCanvasX(touch.clientX));
|
|
}, { passive: false });
|
|
|
|
canvas.addEventListener('touchend', (e) => {
|
|
e.preventDefault();
|
|
if (game.state.isGameOver) {
|
|
game.restart();
|
|
} else {
|
|
game.drop();
|
|
}
|
|
}, { passive: false });
|
|
}
|