extends Spatial

var player:Player
onready var camera:InterpolatedCamera = get_node("camera")
onready var ingame_menu:Spatial = find_node("ingame_menu")


func _process(delta):
	if is_network_master() && player != null:
		if Input.is_action_just_pressed("controls_menu"):
			ingame_menu.set_visible(not ingame_menu.is_visible_in_tree())
		if player.is_out():
			camera.set_speed(0.1)
			if player.timer.get_time_left() > 0:
				get_node("hud/reset").set_text(str(int(player.timer.get_time_left()) + 1))
			else:
				get_node("hud/reset").set_text(tr("RESET"))
			if Input.is_action_pressed("controls_reset"):
				player.rpc("reset")
		elif not ingame_menu.is_visible_in_tree():
			camera.set_speed(10)
			get_node("hud/reset").set_text("")
			if Input.is_action_pressed("controls_thrust"):
				player.rpc_unreliable("set_thrust",1)
			elif Input.is_action_pressed("controls_break"):
				player.rpc_unreliable("set_thrust",-1)
			else:
				player.rpc_unreliable("set_thrust",0)

		get_node("hud/speed_slider").set_value(player.current_speed * 1000)

		if Input.is_action_pressed("debug_camera_1"):
			set_debug_camera(0)
		elif Input.is_action_pressed("debug_camera_2"):
			set_debug_camera(1)
		elif Input.is_action_pressed("debug_camera_3"):
			set_debug_camera(2)
		elif Input.is_action_pressed("debug_camera_4"):
			set_debug_camera(3)


func set_player(path:String):
	player = get_node(path)
	get_node("inventory").set_player(player)
	camera.set_target_path(player.find_node("CameraTarget").get_path())
	camera.set_interpolation_enabled(true)
	camera.set_speed(10)


func set_debug_camera(idx):
	if idx < gamestate.game.get_node("players").get_child_count():
		camera.set_target_path(gamestate.game.get_node("players").get_child(idx).find_node("CameraTarget").get_path())
