extends Node2D

#@onready var player = $Player
@onready var main_camera = $MainCamera
@onready var hud = $UI/HUD
@onready var player: Player = $Player
@onready var win_sound = $WinSound

var stars_remaining: int = 3  # total at start




func _ready() -> void:
	if player:
		var hit_points: int = player.get_hit_points()
		var bullets: int = player.get_bullets()
		var kills: int = 0
		hud.update_values(hit_points, bullets)
		hud.update_stars_left(stars_remaining) 
		player.died.connect(_on_player_died)
		player.camera_remote_transform.remote_path = main_camera.get_path()

	
func _process(_delta):
	if player:
		hud.update_values(player.get_hit_points(), player.get_bullets())

func star_collected():
	stars_remaining -= 1
	hud.update_stars_left(stars_remaining)
	
	if stars_remaining == 0:
		win_sound.play()
		hud.show_you_won()
		get_tree().paused = true


func _on_player_died():
	print("game over")
	hud.update_values(player.get_hit_points(), player.get_bullets())
	hud.show_game_over()
	get_tree().create_timer(3).timeout.connect(get_tree().reload_current_scene)
