새요소 추가 시에도 위로 올리기 체크 구현

This commit is contained in:
dohyeons 2025-10-27 12:02:15 +09:00
parent 3b5f0b638f
commit 189f0e03a0
1 changed files with 32 additions and 3 deletions

View File

@ -129,10 +129,39 @@ export default function YardEditor({ layout, onBack }: YardEditorProps) {
return { x: 0, z: 0 }; return { x: 0, z: 0 };
}; };
// 특정 XZ 위치에 배치할 때 적절한 Y 위치 계산 (마인크래프트 쌓기)
const calculateYPosition = (x: number, z: number, existingPlacements: YardPlacement[]) => {
const gridSize = 5;
const halfSize = gridSize / 2;
let maxY = halfSize; // 기본 바닥 높이 (2.5)
for (const p of existingPlacements) {
// XZ가 겹치는지 확인
const isXZOverlapping = Math.abs(x - p.position_x) < gridSize && Math.abs(z - p.position_z) < gridSize;
if (isXZOverlapping) {
// 이 요소의 윗면 높이
const topY = p.position_y + (p.size_y || gridSize) / 2;
// 새 요소의 Y 위치 (윗면 + 새 요소 높이/2)
const newY = topY + gridSize / 2;
if (newY > maxY) {
maxY = newY;
}
}
}
return maxY;
};
// 빈 요소 추가 (로컬 상태에만 추가, 저장 시 서버에 반영) // 빈 요소 추가 (로컬 상태에만 추가, 저장 시 서버에 반영)
const handleAddElement = () => { const handleAddElement = () => {
const gridSize = 5; const gridSize = 5;
const emptyPos = findEmptyGridPosition(gridSize); const emptyPos = findEmptyGridPosition(gridSize);
const centerX = emptyPos.x + gridSize / 2;
const centerZ = emptyPos.z + gridSize / 2;
// 해당 위치에 적절한 Y 계산 (쌓기)
const appropriateY = calculateYPosition(centerX, centerZ, placements);
const newPlacement: YardPlacement = { const newPlacement: YardPlacement = {
id: nextPlacementId, // 임시 음수 ID id: nextPlacementId, // 임시 음수 ID
@ -142,9 +171,9 @@ export default function YardEditor({ layout, onBack }: YardEditorProps) {
quantity: null, quantity: null,
unit: null, unit: null,
// 그리드 칸의 중심에 배치 (Three.js Box position은 중심점) // 그리드 칸의 중심에 배치 (Three.js Box position은 중심점)
position_x: emptyPos.x + gridSize / 2, // 칸 중심: 0→2.5, 5→7.5, 10→12.5... position_x: centerX, // 칸 중심: 0→2.5, 5→7.5, 10→12.5...
position_y: gridSize / 2, // 요소 높이의 절반 (바닥에서 시작) position_y: appropriateY, // 쌓기 고려한 Y 위치
position_z: emptyPos.z + gridSize / 2, // 칸 중심: 0→2.5, 5→7.5, 10→12.5... position_z: centerZ, // 칸 중심: 0→2.5, 5→7.5, 10→12.5...
size_x: gridSize, size_x: gridSize,
size_y: gridSize, size_y: gridSize,
size_z: gridSize, size_z: gridSize,