Files
ryan ef30931b20 Add sandbox stage builder scripts and functionality
- Introduced StageGizmos for hover highlighting, selection outlines, and rotation handles in the sandbox stage builder.
- Added StageGrid for an optional world-space grid overlay that adjusts with camera panning and zooming.
- Implemented StageSelection for geometric hit-testing and selection management of nodes in the sandbox.
- Created StageSpawner as a registry-driven factory for spawning terrain, props, and stickmen, allowing for dynamic template management.
- Each script includes necessary constants, state management, and public API methods for interaction.
2026-08-28 21:53:55 -04:00

47 lines
1.6 KiB
GDScript

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