class_name GroundButton
extends Area2D

@onready var sprite: Sprite2D = $Sprite2D
@onready var audio_player: AudioStreamPlayer2D = $AudioStreamPlayer2D

@export var nodes: Array[Node2D]
@export var is_active = false
var default_state = false

func _ready():
	default_state = is_active
	_flip()

func _on_body_entered(body):
	if body is Player:
		is_active = not is_active
		_flip()
		audio_player.play()

func _flip():
	$Sprite2D.frame = 1 if is_active else 0
	for node in nodes:
		if node is TileMapLayer:
			var layer = node as TileMapLayer
			layer.enabled = !layer.enabled
		else:
			node.visible = false if is_active else true
			node.process_mode = PROCESS_MODE_DISABLED if is_active else PROCESS_MODE_INHERIT
			node.set_physics_process(!is_active)

func reset():
	is_active = default_state
	_flip()
