extends Node2D
## Простой перетаскиваемый узел для демо-уровня.

@export var drag_enabled: bool = true
@export var snap_grid: int = 32

var dragging: bool = false
var drag_offset: Vector2 = Vector2.ZERO


func _input(event: InputEvent) -> void:
	if not drag_enabled:
		return
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed and _is_over(event.position):
			dragging = true
			drag_offset = global_position - event.position
			z_index = 10
		elif not event.pressed and dragging:
			dragging = false
			z_index = 0
			position = Vector2(
				round(position.x / snap_grid) * snap_grid,
				round(position.y / snap_grid) * snap_grid
			)
	if event is InputEventMouseMotion and dragging:
		global_position = event.position + drag_offset


func _is_over(pos: Vector2) -> bool:
	return global_position.distance_to(pos) < 40.0
