extends KinematicBody2D

#------------------Easy

#var wheel_base = 70  # Distance from front to rear wheel
#var steering_angle = 10  # Amount that front wheel turns, in degrees

#var velocity = Vector2.ZERO
#var steer_angle

#var friction = -0.9
#var drag = -0.0015

#var braking = -450
#var max_speed_reverse = 150

#var engine_power = 375  # Forward acceleration force.

#var acceleration = Vector2.ZERO

#var slip_speed = 300  # Speed where traction is reduced
#var traction_fast = 0.1  # High-speed traction
#var traction_slow = 0.9  # Low-speed traction

# ------------------Medium

var wheel_base = 70  # Distance from front to rear wheel
var steering_angle = 10  # Amount that front wheel turns, in degrees

var velocity = Vector2.ZERO
var steer_angle

var friction = -1
var drag = -0.0015

var braking = -450
var max_speed_reverse = 150

var engine_power = 425  # Forward acceleration force.

var acceleration = Vector2.ZERO

var slip_speed = 300  # Speed where traction is reduced
var traction_fast = 0.1  # High-speed traction
var traction_slow = 0.9  # Low-speed traction

var coinscollected: float = 0

func calculate_steering(delta):
	var rear_wheel = position - transform.x * wheel_base / 2.0
	var front_wheel = position + transform.x * wheel_base / 2.0
	rear_wheel += velocity * delta
	front_wheel += velocity.rotated(steer_angle) * delta
	var new_heading = (front_wheel - rear_wheel).normalized()
	var traction = traction_slow
	if velocity.length() > slip_speed:
		traction = traction_fast
	var d = new_heading.dot(velocity.normalized())
	if d > 0:
		velocity = velocity.linear_interpolate(new_heading * velocity.length(), traction)
	if d < 0:
		velocity = -new_heading * min(velocity.length(), max_speed_reverse)
	rotation = new_heading.angle()


func get_input():
	var turn = 0
	if Input.is_action_pressed("player1_steer_right"):
		turn -= 1
#		$player_controller.func_and_sync("steer", -1)
	if Input.is_action_pressed("player1_steer_left"):
		turn += 1
#		$player_controller.func_and_sync("steer", 1)
	steer_angle = turn * steering_angle
	if Input.is_action_pressed("player1_accelerate"):
		#var player = AudioStreamPlayer.new()
		#self.add_child(player)
		#player.stream = load("res://assets/sounds/golfcart/accelerate.wav")
		#player.play()

		acceleration = transform.x * engine_power
	if Input.is_action_pressed("player1_horn"):
		Input.start_joy_vibration(0, 1, 0, 0.05)
		Input.vibrate_handheld(100)
		var player = AudioStreamPlayer.new()
		self.add_child(player)
		player.stream = load("res://assets/sounds/horn3.wav")
		player.play()
	pass
		
func _physics_process(delta):
	acceleration = Vector2.ZERO
	get_input()
	apply_friction()
	calculate_steering(delta)
	velocity += acceleration * delta
	velocity = move_and_slide(velocity)

func apply_friction():
	if velocity.length() < 5:
		velocity = Vector2.ZERO
	var friction_force = velocity * friction
	var drag_force = velocity * velocity.length() * drag
	if velocity.length() < 100:
		friction_force *= 3
	acceleration += drag_force + friction_force

	if Input.is_action_pressed("player1_decelerate"):
		acceleration = transform.x * braking


func _on_Area2D_body_entered(body):
	pass



func _on_Area2D_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	var completedtimetrial = true
	get_tree().change_scene("res://RaceCompletion.tscn")
	pass

func _on_Coin1_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	coinscollected = coinscollected+1
	$"../Coin1".visible = false
	$"../Coin1/CollisionShape2D".disabled = true
	$"../Coin1".set_deferred("disabled", true)
	$"../Coin1".queue_free()

	var player = AudioStreamPlayer.new()
	self.add_child(player)
	player.stream = load("res://assets/sounds/coinpickup.wav")
	player.play()
	$"../HUD/HUD/RightUI/Barlarge/ItemsCollectedText".text = str(coinscollected)

func _on_Coin2_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	coinscollected = coinscollected+1
	$"../Coin2".visible = false
	$"../Coin2/CollisionShape2D".disabled = true

	var player = AudioStreamPlayer.new()
	self.add_child(player)
	player.stream = load("res://assets/sounds/coinpickup.wav")
	player.play()
	$"../HUD/HUD/RightUI/Barlarge/ItemsCollectedText".text = str(coinscollected)
	$"../Coin2".set_deferred("disabled", true)
	$"../Coin2".queue_free()



func _on_Coin3_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	coinscollected = coinscollected+1
	$"../Coin3".visible = false
	$"../Coin3/CollisionShape2D".disabled = true
	var player = AudioStreamPlayer.new()
	self.add_child(player)
	player.stream = load("res://assets/sounds/coinpickup.wav")
	player.play()
	$"../HUD/HUD/RightUI/Barlarge/ItemsCollectedText".text = str(coinscollected)
	$"../Coin3".set_deferred("disabled", true)
	$"../Coin3".queue_free()

	
func _notification(what):
	if (what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST):
		$"../HUD".visible = true
		$"../SwamplandsHUD".visible = true
		$"../RaceHUD".visible = true
