extends "res://scenes/entities/SimpleMovable.gd"


@onready var audio_player = $AudioStreamPlayer3D

enum PLAYERTOOLS {YELLOW, BLUE, RED}
@export var tool_type: PLAYERTOOLS = PLAYERTOOLS.YELLOW

var is_driven = false



func _ready() -> void:
	super._ready()
	update_rotation_point(DIRECTIONS.UP)

func _process(delta):

	if not is_driven and not is_pushed:
		requested_direction = DIRECTIONS.NONE

func hit():
	if health_node.invulnerable == false:
		audio_player.stream = load("res://sfx/ncl_player_hit.ogg")
		#audio_player.volume_db = -1
		audio_player.pitch_scale = randf_range(0.86, 0.93)
		audio_player.play()
	health_node.hurt()
	#SceneManager.game_scene.update_labels()	

func kill():
	pass
	#health_node.kill()

	

	

func _on_Health_no_health() -> void:
	pass
	#if SceneManager.game_scene.is_finished:
		#no gameover when the level is already finished :-)
	#	return
	#print ("you died")
	
	#Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	#get_tree().change_scene_to_file("res://UI/GameOverScreen.tscn")





func _on_drive_area_body_entered(body: Node3D) -> void:
	if body.is_in_group("player"):
		driver = body
		if body.global_position.y - global_position.y < 2.4:
			return
		if abs(body.global_position.x - global_position.x) > 1.1:
			#print("x dist too large to drive!")
			return
		if abs(body.global_position.z - global_position.z) > 1.1:
			#print("z dist too large to drive!")
			return
		body.driving_object = self
		body.enable_movement = false
		update_rotation_point(body.view_direction)
		#body.collision_shape.set_deferred("disabled", true)
		is_driven = true


func _on_drive_area_body_exited(body: Node3D) -> void:
	if body.is_in_group("player"):
		#print ("a tool was driven (now released) by: " + str(body.name))
		body.driving_object = null
		driver = null
		body.enable_movement = true
		is_blocked = false
		body.requested_direction = DIRECTIONS.NONE
		#body.collision_shape.set_deferred("disabled", false)
		is_driven = false

func wait(time):
	enable_movement = false
	enable_turning = false
	InputManager.player_scene.enable_turning = false
	await get_tree().create_timer(time).timeout
	InputManager.player_scene.enable_turning = true
	enable_movement = true
	enable_turning = true
