extends Area3D


@export var need_to_hold_down = true
@export var linked_animators: Array[Node3D]
var switch_held: bool


func _physics_process(delta: float) -> void:
	# logic for switches that need to be held down
	if (need_to_hold_down):
		var on_switch = false
		var overlapping_bodies = get_overlapping_bodies()
		for body in overlapping_bodies:
			if body.is_in_group("player") or body.is_in_group("canTriggerSwitch"):
				on_switch = true
				break;
				
		if on_switch:
			if !switch_held:
				switch_held = true
				activate_switch()
		else:
			if switch_held:
				switch_held = false
				activate_switch_reverse()
							
	# logic for switches that just need to be touched once
	else:
		var overlapping_bodies = get_overlapping_bodies()
		for body in overlapping_bodies:
			if body.is_in_group("player") or body.is_in_group("canTriggerSwitch"):
				activate_switch()

func activate_switch():
	for linked_animator in linked_animators:
		var animator: AnimationPlayer = linked_animator.anim_player
		if animator:
			animator.speed_scale = 1
			animator.play("anim1/open_animation");
			
func activate_switch_reverse():
	for linked_animator in linked_animators:
		var animator: AnimationPlayer = linked_animator.anim_player
		if animator:
			if !animator.is_playing():
				animator.play_backwards("anim1/open_animation")
			else:
				animator.speed_scale = -1
