From 23a2e784a9d0698308ff8e4e65085d974b71c643 Mon Sep 17 00:00:00 2001
From: Fisch <interfisch@gmx.net>
Date: Sat, 22 Mar 2025 15:13:02 +0100
Subject: [PATCH] fix car reset backwards steering

---
 scenes/car.tscn |  4 ++++
 scripts/car.gd  | 36 +++++++++++++++++++++++++++---------
 scripts/game.gd |  5 +++++
 3 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/scenes/car.tscn b/scenes/car.tscn
index 1b84436..f9bc0d8 100644
--- a/scenes/car.tscn
+++ b/scenes/car.tscn
@@ -238,5 +238,9 @@ bus = &"Crash"
 stream = SubResource("AudioStreamRandomizer_evs7n")
 bus = &"Crash"
 
+[node name="NoInputTimer" type="Timer" parent="CharacterBody_Car"]
+wait_time = 10.0
+one_shot = true
+
 [connection signal="timeout" from="CharacterBody_Car/resetTimer" to="CharacterBody_Car" method="_on_reset_timer_timeout"]
 [connection signal="timeout" from="CharacterBody_Car/collisionEnableTimer" to="CharacterBody_Car" method="_on_collision_enable_timer_timeout"]
diff --git a/scripts/car.gd b/scripts/car.gd
index 1a3f087..78cdeca 100644
--- a/scripts/car.gd
+++ b/scripts/car.gd
@@ -58,7 +58,7 @@ var steering_distance_close_fast=64
 var resetcar_stoppedspeed = 50 #activate timer when below this speed
 var resetcar_movingspeed=resetcar_stoppedspeed+10 #stop timer when above this speed
 var resetcar_distance=128 #196 is roughly when car is in the middle of a two wide road
-var resetcar_steerangle=120
+var resetcar_steerangle=120.0/360.0*2*PI
 
 # other car avoidance
 var avoid_car_distance_far=100
@@ -91,6 +91,7 @@ var burnout:float=0 #at 0 is off
 
 
 @onready var reset_timer: Timer = $resetTimer
+@onready var no_input_timer: Timer = $NoInputTimer
 
 @onready var ray_cast_car: RayCast2D = $RayCast_Car #for tracking markers
 
@@ -128,6 +129,7 @@ func _physics_process(delta: float) -> void:
 		$Burnout_Left.emitting=false
 		$Burnout_Right.emitting=false
 	
+	#update_timeout_visibility()
 	move_and_slide()
 	
 	for i in get_slide_collision_count():
@@ -153,8 +155,12 @@ func _physics_process(delta: float) -> void:
 	if velocity.length() < resetcar_stoppedspeed and autosteer_enabled: #moving slow, possibly crash?
 		if reset_timer.is_stopped():
 			reset_timer.start()
+		if no_input_timer.is_stopped():
+			no_input_timer.start()
+		
 	if velocity.length() > resetcar_movingspeed:
 		reset_timer.stop()
+		no_input_timer.stop()
 		
 	if running:
 		enginesound.setCarSpeed(velocity.length())
@@ -168,6 +174,18 @@ func _physics_process(delta: float) -> void:
 func _on_reset_timer_timeout() -> void:
 	autoreset=true
 	
+func update_timeout_visibility():
+	var fadeout=1-constrain((3-no_input_timer.time_left)/3.0,0.0,1.0) #1= full visible, 0=timeouted
+	$sprite_body.modulate.a=fadeout
+	$sprite_features.modulate.a=fadeout
+	$sprite_steeringwheel.modulate.a=fadeout
+	
+func getNoInputTimeout():
+	if no_input_timer.time_left<=0:
+		return true
+	else:
+		return false
+	
 func apply_friction():
 	if velocity.length() < STANDSTILLSPEED: #standstill
 		velocity=Vector2.ZERO
@@ -228,7 +246,6 @@ func get_input(delta:float):
 		steer_direction=1
 	'''
 	
-	steer_direction+=constrain(steer_direction_aim-steer_direction,max_steering_change*delta,-max_steering_change*delta)
 	
 	applied_engine_power=0
 	
@@ -256,18 +273,19 @@ func get_input(delta:float):
 		
 	if autoreset and running:
 		acceleration = transform.x * braking #drive backwards
-		
-		if distance_min>=resetcar_distance: #nothing in front of car
-			steer_direction=resetcar_steerangle #keep steering so turn around if standing in the middle of a track
-		else:
-			steer_direction*=-1 #invert steering
+		steer_direction_aim=resetcar_steerangle #keep steering to turn around if standing in the middle of a track
+		#steer_direction=steer_direction_aim #steer without smoothing
 	else:
-		if steer_direction>1:
+		if steer_direction_aim<0.1:
 			resetcar_steerangle=max(-resetcar_steerangle,+resetcar_steerangle) #calculate steering direction for next autoreset
-		if steer_direction<1:
+		if steer_direction_aim>0.1:
 			resetcar_steerangle=min(-resetcar_steerangle,+resetcar_steerangle) #calculate steering direction for next autoreset
 		
+	
+	steer_direction+=constrain(steer_direction_aim-steer_direction,max_steering_change*delta,-max_steering_change*delta) #smooth steering. towards steer_direction_aim
 	$sprite_steeringwheel.rotation=steer_direction
+	
+	print("steer_direction="+str(steer_direction)+" \t resetcar_steerangle="+str(resetcar_steerangle)+" \t steer_direction_aim="+str(steer_direction_aim))
 
 func calculate_steering(delta:float):
 	var rear_wheel = position - transform.x *wheel_base/2.0
diff --git a/scripts/game.gd b/scripts/game.gd
index fd5ec09..394799c 100644
--- a/scripts/game.gd
+++ b/scripts/game.gd
@@ -125,6 +125,11 @@ func _process(delta: float) -> void:
 		if !anyplayerkeypressed and timer_close.is_stopped():
 			timer_close.start() #start timer when no key is pressed
 			
+func checkCarsTimeout(delta: float):
+	var cars=cars.get_children()
+	for c in cars: #check if any one car is still driving
+		if !c.hasFinished() and c.getNoInputTimeout():
+			print("Removing car")
 
 func updateCameraMovement(delta: float):