extends Node2D
class_name SuperCamera

@onready var area_2d: Area2D = $Area2D
@onready var camera: Camera2D = $Camera2D

@export var should_follow_player = false
@export var minimum_location = 0
@export var maximum_location = 640

signal became_active(SuperCamera)

var is_player_in_range = false
var player: Node2D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if not should_follow_player or not is_player_in_range:
		return
	
	if player.global_position.x < minimum_location or player.global_position.x > maximum_location:
		return
		
	global_position.x = player.global_position.x


func _on_body_entered(body: Node2D) -> void:
	if body.name == "Player":
		became_active.emit(self)
		camera.make_current()
		player = body
		is_player_in_range = true


func _on_body_exited(body: Node2D) -> void:
	if body.name == "Player":
		is_player_in_range = false
