extends Node2D

## Particle Demo: Playground for testing GPUParticles2D and ParticleProcessMaterial

var spawn_pos = Vector2(200, 300)
var current_particles: GPUParticles2D

func _ready():
	$BtnFountain.pressed.connect(_on_fountain)
	$BtnBurst.pressed.connect(_on_burst)
	$BtnZeroGravity.pressed.connect(_on_zero_gravity)
	$BtnFinalFruit.pressed.connect(_on_final_fruit)

func clear_particles():
	if is_instance_valid(current_particles):
		current_particles.queue_free()

func create_base_particles() -> GPUParticles2D:
	clear_particles()
	var p = GPUParticles2D.new()
	p.texture = preload("res://assets/images/star_01.png")
	p.position = spawn_pos
	add_child(p)
	current_particles = p
	return p

func _on_fountain():
	var p = create_base_particles()
	var mat = ParticleProcessMaterial.new()
	
	# Narrow spread, shooting upwards
	mat.direction = Vector3(0, -1, 0)
	mat.spread = 15.0
	mat.particle_flag_disable_z = true
	
	mat.initial_velocity_min = 200.0
	mat.initial_velocity_max = 300.0
	mat.gravity = Vector3(0, 400, 0) # Strong gravity pulling them back down
	
	# Scale over time
	mat.scale_min = 0.5
	mat.scale_max = 1.0
	
	p.process_material = mat
	p.lifetime = 1.5
	p.amount = 50
	
	# Continuous emission!
	p.one_shot = false
	p.explosiveness = 0.0

func _on_burst():
	var p = create_base_particles()
	var mat = ParticleProcessMaterial.new()
	
	# 360 degree spread
	mat.direction = Vector3(1, 0, 0)
	mat.spread = 180.0
	mat.particle_flag_disable_z = true
	
	mat.initial_velocity_min = 150.0
	mat.initial_velocity_max = 250.0
	mat.gravity = Vector3(0, 300, 0)
	
	p.process_material = mat
	p.lifetime = 1.0
	p.amount = 40
	
	# Explode all at once!
	p.one_shot = true
	p.explosiveness = 1.0

func _on_zero_gravity():
	var p = create_base_particles()
	var mat = ParticleProcessMaterial.new()
	
	mat.direction = Vector3(1, 0, 0)
	mat.spread = 180.0
	mat.particle_flag_disable_z = true
	
	mat.initial_velocity_min = 50.0
	mat.initial_velocity_max = 150.0
	# ZERO GRAVITY - they just drift outward
	mat.gravity = Vector3(0, 0, 0)
	
	p.process_material = mat
	p.lifetime = 2.0
	p.amount = 30
	p.one_shot = true
	p.explosiveness = 1.0

func _on_final_fruit():
	clear_particles()
	# Call the actual static function they are about to write!
	var ParticleBurst = preload("res://scripts/particle_burst.gd")
	ParticleBurst.create_fruit_burst(self, spawn_pos)
