extends Area2D

@onready var collectArea = $CollectArea
@onready var animatedSprite = $AnimatedSprite2D

func _ready() -> void:
	animatedSprite.play("default")

func _physics_process(delta: float) -> void:
	for i in get_overlapping_bodies():
		if i.is_in_group("Player"):
			if G.isStormThreshold():
				moveTowardsCenter(i)
	for i in get_overlapping_areas():
		if i.is_in_group("Object"):
			if not i.isCarryied():
				moveTowardsCenter(i)
			else:
				scale = Vector2(1,1)
	#cashInObject()

func cashInObject():
	for i in collectArea.get_overlapping_areas():
		if not i.isCarryied():
			G.addMoney(i.getValue())
			i.queue_free()


func moveTowardsCenter(object : Node2D):
	object.global_position.y = move_toward(object.global_position.y, global_position.y, 2)

	object.global_position.x = move_toward(object.global_position.x, global_position.x, 2)

	object.scale.y -= 0.01
	object.scale.x -= 0.01

func extractPlayer():
	G.extractPlayer()


func _on_collect_area_area_entered(area: Area2D) -> void:
	if area.is_in_group("Object"):
		cashInObject()


func _on_collect_area_body_entered(body: Node2D) -> void:

	if G.isStormThreshold():
		if body.is_in_group("Player"):
			extractPlayer()
