extends Node2D

## Dynamic .new() Demo: Create nodes from code instead of scenes

func _ready():
	$Label.text = "Click anywhere to spawn a random circle"

func _input(event: InputEvent):
	if event is InputEventMouseButton and event.pressed:
		spawn_circle(event.position)

func spawn_circle(position: Vector2):
	var sprite = Sprite2D.new()  # ← Create from code, not from scene
	sprite.texture = create_circle_texture(32, Color(randf(), randf(), randf()))
	sprite.position = position
	add_child(sprite)
	
	# Animate it
	var tween = sprite.create_tween()
	tween.tween_property(sprite, "scale", Vector2(0.5, 0.5), 0.5)
	await tween.finished
	sprite.queue_free()

func create_circle_texture(radius: int, color: Color) -> ImageTexture:
	var image = Image.create(radius * 2, radius * 2, false, Image.FORMAT_RGBA8)
	for y in range(radius * 2):
		for x in range(radius * 2):
			var dx = x - radius
			var dy = y - radius
			var dist = sqrt(dx * dx + dy * dy)
			if dist <= radius:
				image.set_pixel(x, y, color)
	return ImageTexture.create_from_image(image)
