class_name Grid
## Central grid constants and utilities for the dungeon crawler.
## Cell size: 64x64x128 TrenchBroom units = 2x2x4 Godot units.

## Size of one grid cell in world units (64 TrenchBroom units).
const CELL_SIZE: float = 2.0
## Half-cell offset. Cell centers sit halfway between wall planes, so with TB
## walls on the 64-unit grid (even Godot units) cell centers land on odd units.
const HALF_CELL: float = CELL_SIZE * 0.5


## Snap a world position to the nearest cell center (Y axis unchanged).
static func snap(pos: Vector3) -> Vector3:
	return Vector3(
		floorf(pos.x / CELL_SIZE) * CELL_SIZE + HALF_CELL,
		pos.y,
		floorf(pos.z / CELL_SIZE) * CELL_SIZE + HALF_CELL,
	)


## Convert a world position to integer cell coordinates.
static func world_to_cell(pos: Vector3) -> Vector2i:
	return Vector2i(
		int(floorf(pos.x / CELL_SIZE)),
		int(floorf(pos.z / CELL_SIZE)),
	)
