ui/scene: dont snap to occupied positions

This commit is contained in:
Johannes Kirschbauer
2025-08-26 11:40:38 +02:00
parent 7b0d10e8c2
commit 360048fd04

View File

@@ -506,6 +506,7 @@ export function CubeScene(props: {
const intersects = raycaster.intersectObject(floor);
if (intersects.length > 0) {
const point = intersects[0].point;
// Snap to grid
const snapped = new THREE.Vector3(
Math.round(point.x / GRID_SIZE) * GRID_SIZE,
@@ -513,6 +514,17 @@ export function CubeScene(props: {
Math.round(point.z / GRID_SIZE) * GRID_SIZE,
);
// Skip snapping if there's already a cube at this position
if (props.sceneStore()) {
const positions = Object.values(props.sceneStore());
const intersects = positions.some(
(p) => p.position[0] === snapped.x && p.position[1] === snapped.z,
);
if (intersects) {
return;
}
}
if (
Math.abs(initBase.position.x - snapped.x) > 0.01 ||
Math.abs(initBase.position.z - snapped.z) > 0.01