class_name StageGrid extends Node2D ## StageGrid - Optional world-space grid overlay for the Sandbox Stage Builder. ## ## Draws grid lines that pan/zoom with the camera, with a heavier major line ## every few cells. Pure drawing; no hit-testing. Rendered behind the World by ## keeping this node as the first child of the stage root. var camera: Camera2D = null var grid_size: float = 15.0 var enabled: bool = true const GRID_COLOR := Color(1.0, 1.0, 1.0, 0.08) const MAJOR_COLOR := Color(1.0, 1.0, 1.0, 0.16) const MAJOR_EVERY := 5 func _process(_delta: float) -> void: if enabled: queue_redraw() func _draw() -> void: if not enabled or grid_size <= 0.0: return if camera == null or not is_instance_valid(camera): return var viewport_size := get_viewport_rect().size var zoom := maxf(camera.zoom.x, 0.0001) var half := viewport_size * 0.5 / zoom var center := camera.get_screen_center_position() var rect := Rect2(center - half, half * 2.0) var line_width := 1.0 / zoom var x := floorf(rect.position.x / grid_size) * grid_size while x <= rect.end.x: var grid_index := int(round(x / grid_size)) var color := MAJOR_COLOR if grid_index % MAJOR_EVERY == 0 else GRID_COLOR draw_line(Vector2(x, rect.position.y), Vector2(x, rect.end.y), color, line_width) x += grid_size var y := floorf(rect.position.y / grid_size) * grid_size while y <= rect.end.y: var grid_index := int(round(y / grid_size)) var color := MAJOR_COLOR if grid_index % MAJOR_EVERY == 0 else GRID_COLOR draw_line(Vector2(rect.position.x, y), Vector2(rect.end.x, y), color, line_width) y += grid_size