extends Node2D

# this scene will be loaded when the player presses the button
export (String) var game_scene_path
var button_fill = 0.0


func _process(delta):
	# if the progress bar is shown - fill it in the span of 3 seconds
	if $Button/ColorRect.visible:
		 button_fill += delta / 3
	
	# make sure it doesnt go past 100%
	button_fill = min(button_fill, 1.0)
	if button_fill == 1.0:
		$upclick.play()
		start_game()
	# make the mains humm louder as the bar fills in a non linear fashion
	$buzz.volume_db = linear2db(pow(button_fill, 2)*0.5)
	# this makes it fill up non linearly, speeding up as it goes
	$Button/ColorRect.rect_scale.x = pow(button_fill, 2)
	pass

func start_game():
	# change scene
	get_tree().change_scene(game_scene_path)
	pass


func _on_Timer_timeout():
	# make the progress bar strobe, change the wait time of Timer to change frequency
	$Button/ColorRect.material.set_shader_param("inverted", !$Button/ColorRect.material.get_shader_param("inverted"))
	pass


func _on_Button_button_down():
	# play click sound, make bar visible and set it to zero
	$downclick.play()
	$Button/ColorRect.visible = true
	button_fill = 0.0
	pass


func _on_Button_button_up():
	# play release sound, hide bar and reset it
	# if the bar is full when button is released - change scene
	$upclick.play()
	if button_fill == 1.0:
		start_game()
	$Button/ColorRect.visible = false
	button_fill = 0.0
	pass
