extends Spatial

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


func _physics_process(delta):
	if is_network_master() && player != null:
		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"):
				if player.reset():
					camera.set_speed(10)
					get_node("hud/reset").set_text("")	
		else:
			if Input.is_action_pressed("controls_thrust"):
				player.thrust = 1
			elif Input.is_action_pressed("controls_break"):
				player.thrust = -1
			else:
				player.thrust = 0
				
		get_node("hud/speed").set_text(str(player.current_speed))
		
		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.players.get_child_count():
		camera.set_target_path(gamestate.game.players.get_child(idx).find_node("CameraTarget").get_path())
