class_name Game
extends Node

const PLAYER: PackedScene = preload(GameGlobals.PLAYER_PATH)

@export_group("Debug", "debug_")
@export_custom(PROPERTY_HINT_GROUP_ENABLE, "checkbox_only") var debug_enabled: bool = false
@export var debug_spawn_point: StringName = &""
@export_range(-1, 0, 1, "or_greater") var debug_chapter_idx: int = -1

var player_character: PlayerCharacter


func _init() -> void:
	GameGlobals.game = self


func _enter_tree() -> void:
	if OS.has_feature("editor") and debug_enabled:
		if not debug_spawn_point.is_empty():
			GameGlobals.set_spawn_id(debug_spawn_point)

		if debug_chapter_idx >= 0:
			GameGlobals.set_chapter_index(debug_chapter_idx)


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	spawn_player()

	AudioManager.play_audio(preload("uid://cyxvsy5wjxhl7"), AudioManager.AMBIENCE)


func spawn_player() -> void:
	player_character = PLAYER.instantiate()
	add_child(player_character)

	var spawnpoint := PlayerSpawnPoint.get_spawn_point_by_identifier(GameGlobals.spawn_id)

	if is_instance_valid(spawnpoint):
		player_character.global_position = spawnpoint.global_position
		player_character.head.global_rotation = spawnpoint.global_rotation
