extends Sprite

onready var light: Light2D = $"Light"
onready var timer: Timer = $"Timer"
onready var sound = $"AudioStreamPlayer2D"
var expanding: bool = false
var landed: bool = false

func _ready():
	set_visible(false)
	light.set_visible(false)

func strike(target: Vector2):
	set_position(target)
	set_visible(true)
	expanding = true
	var region = get_region_rect()
	region.size.y = 0
	sound.play()
	set_region_rect(region)
	
func play():
	sound.play()
	

func _process(_delta):
	if expanding:
		var region = get_region_rect()
		region.size.y += 18
		if region.size.y > 256:
			region.size.y = 256
			expanding = false
			landed = true
			light.set_visible(true)
			timer.start()
		set_region_rect(region)
	
	if landed:
		var intensity = min(timer.get_time_left(), 1.0)
		set_modulate(Color(1, 1, 1, intensity))
		light.set_energy(intensity * 10.0)
		if timer.is_stopped():
			landed = false
			set_visible(false)
			light.set_visible(false)
