extends KinematicBody2D

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 = 350  # Forward acceleration force.

var acceleration = Vector2.ZERO

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

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("player2_steer_right"):
		turn -= 1
	if Input.is_action_pressed("player2_steer_left"):
		turn += 1
	steer_angle = turn * steering_angle
	if Input.is_action_pressed("player2_accelerate"):
		acceleration = transform.x * engine_power
	if Input.is_action_pressed("player2_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("player2_decelerate"):
		acceleration = transform.x * braking
