labyrinth-frontend/src/Cell.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

import React from 'react';
2023-04-14 02:01:55 +02:00
function isMarked(x, y, marked) {
return !!marked.find(e => e.x === x && e.y === y);
}
export default function Cell({x, y, state, dispatch}) {
const cell = state.maze.grid[y][x];
let classes = "cell r" + y + " c" + x;
if (cell.top) classes += " top";
if (cell.right) classes += " right";
if (cell.bottom) classes += " bottom";
if (cell.left) classes += " left";
if (cell.solution && state.showSolution) classes += " solution";
const marked = isMarked(x, y, state.userPath);
if (marked) classes += " user";
2023-04-14 02:01:55 +02:00
return (
<div className={classes}
onMouseEnter={(e) => {
const leftPressed = e.buttons & 0x1;
if (leftPressed) {
dispatch({
type: 'clicked_cell',
x,
y,
e
});
}
}}
onMouseLeave={(e) => {
const leftPressed = e.buttons & 0x1;
if (leftPressed) {
}
}}
onClick={(e) => {
dispatch({
type: 'clicked_cell',
x,
y,
e
});
}}>
</div>
2023-04-14 02:01:55 +02:00
);
}