extends RigidBody2D

var line: Line2D
var elapsed_time: float = 0.0
var pop_chance: float = 10
var pop_timer := Timer.new()
var multiplier: int = 1
var holding = false
var bubble_name: String = 'Dotcom'
var multiplier_color = {
	1: "white",
	2: "pink",
	3: "deeppink",
	4: "Fuchsia",
	5: "Turquoise ",
	6: "Yellow ",
	7: "red",
	8: "Chartreuse",
	9: "orange",
	10: "purple",
}
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	linear_velocity = Vector2(200, 0)
	gravity_scale = randf_range(-1, 1)
	lock_rotation = true
	
	add_child(pop_timer)
	

	$RichTextLabel.text = "[center][color="+multiplier_color[multiplier]+"]"+str(multiplier)+"[/color][/center]"
	pop_timer.wait_time = pop_chance
	pop_timer.one_shot = true
	pop_timer.start()
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if not freeze:
		gravity_scale = randf_range(-1, 1)
		linear_velocity = Vector2(200, 0)
	if holding:
		$"..".total_score += multiplier
	if pop_timer.is_stopped():
		$AnimatedSprite2D.play("pop")
		$Pop.play()
	pass

func pop():	
	queue_free()

func _on_body_entered(body: Node) -> void:
	if body.name == "Player":
		$"..".current_investments.append("[color="+multiplier_color[multiplier]+"]"+bubble_name+" x"+str(multiplier)+"[/color]")
		$AnimatedSprite2D.play("static")
		$"../CanvasLayer/Score/AnimationPlayer".play("zoom")
		$"../CanvasLayer/Score/AudioStreamPlayer2D".play()
		holding = true
	if body.name.contains("RigidBody2D"):
		$"..".current_investments.erase("[color="+multiplier_color[multiplier]+"]"+bubble_name+" x"+str(multiplier)+"[/color]")
		$AnimatedSprite2D.play("pop")
		$Pop.play()
		
	linear_velocity = Vector2(0, 0)
	gravity_scale = 0
	freeze = true
	freeze_mode = FREEZE_MODE_STATIC
	
	pass # Replace with function body.



func _on_animated_sprite_2d_animation_finished() -> void:
	if $AnimatedSprite2D.animation == "pop":
		pop()
	pass # Replace with function body.
