Merge branch 'main' into tooltip

This commit is contained in:
2026-07-02 22:56:34 +02:00
21 changed files with 265 additions and 59 deletions

View File

@@ -2,6 +2,7 @@ extends Path3D
const STEAM_DISTANCE_STAT: String = "stat_distance_km"
const DISTANCE_KM_PER_UNIT: float = 0.01
const STATION_STOP_GROUP: StringName = &"railway_station"
@export_group("Train")
##train speed
@@ -48,6 +49,10 @@ const DISTANCE_KM_PER_UNIT: float = 0.01
@export_group("Train Stops")
##if true the train stop the ran on stops
@export var enable_stops: bool = true
##if true the train only stops on markers with a generated station nearby
@export var require_station_for_stop: bool = true
##max horizontal distance between a stop marker and a station chunk
@export var station_stop_detection_radius: float = 35.0
##time of stop
@export var stop_time: float = 4.0
##distance when the train start to brake
@@ -129,15 +134,22 @@ func _apply_initial_train_start() -> void:
if total_length <= 0.0:
return
#start from a random position
if environment_config != null and environment_config.train_start_from_random_position:
train_progress = randf() * total_length
_update_next_stop_index_from_progress()
elif environment_config != null and not stop_offset.is_empty():
#start from a specific stop
var stop_index: int = clampi(environment_config.train_start_stop_index, 0, stop_offset.size() - 1)
train_progress = stop_offset[stop_index]
_set_next_stop_index_from_current(stop_index)
if environment_config != null:
match environment_config.train_start_from_random_position:
EnvironmentConfig.TrainStartMode.RANDOM_POSITION:
train_progress = randf() * total_length
_update_next_stop_index_from_progress()
EnvironmentConfig.TrainStartMode.SPECIFIED_STATION:
if stop_offset.is_empty():
train_progress = wrapf(train_progress, 0.0, total_length)
_update_next_stop_index_from_progress()
else:
var stop_index: int = clampi(environment_config.train_start_stop_index, 0, stop_offset.size() - 1)
train_progress = stop_offset[stop_index]
_set_next_stop_index_from_current(stop_index)
EnvironmentConfig.TrainStartMode.SPECIFIED_POSITION:
train_progress = wrapf(environment_config.train_start_position, 0.0, total_length)
_update_next_stop_index_from_progress()
else:
train_progress = wrapf(train_progress, 0.0, total_length)
_update_next_stop_index_from_progress()
@@ -148,16 +160,22 @@ func _apply_train_start_delay() -> void:
if not _initial_is_inmotion:
return
var should_start_after_delay: bool = true
var start_delay_seconds: float = 0.0
if environment_config != null:
should_start_after_delay = environment_config.train_start_after_delay
start_delay_seconds = maxf(environment_config.train_start_delay_seconds, 0.0)
if start_delay_seconds <= 0.0:
if not should_start_after_delay:
is_inmotion = false
return
is_inmotion = false
await get_tree().create_timer(start_delay_seconds).timeout
if not is_inside_tree():
return
if not should_start_after_delay:
return
is_inmotion = _initial_is_inmotion
@@ -274,6 +292,9 @@ func build_rails() -> void:
rail_piece.add_child(rail_dx)
func _plan_stops() -> void:
stop_offset.clear()
stops_position.clear()
var current_stops = []
for child in get_children():
if child is Marker3D:
@@ -287,8 +308,58 @@ func _plan_stops() -> void:
stop_offset.append(data["offset"])
stops_position.append(data["position"])
func _advance_next_stop_index(go_forward: bool) -> void:
if stop_offset.is_empty():
next_stop_index = 0
return
if go_forward:
next_stop_index += 1
if next_stop_index >= stop_offset.size():
next_stop_index = 0
else:
next_stop_index -= 1
if next_stop_index < 0:
next_stop_index = stop_offset.size() - 1
func _find_next_valid_stop(go_forward: bool) -> bool:
if stop_offset.is_empty():
return false
for i in range(stop_offset.size()):
if _is_stop_valid(next_stop_index):
return true
_advance_next_stop_index(go_forward)
return false
func _is_stop_valid(stop_index: int) -> bool:
if not require_station_for_stop:
return true
if stop_index < 0 or stop_index >= stops_position.size():
return false
return _has_station_near_position(stops_position[stop_index])
func _has_station_near_position(stop_position: Vector3) -> bool:
var radius_sq := station_stop_detection_radius * station_stop_detection_radius
for station in get_tree().get_nodes_in_group(STATION_STOP_GROUP):
var station_node := station as Node3D
if station_node == null or not is_instance_valid(station_node):
continue
var delta := station_node.global_position - stop_position
var horizontal_dist_sq := delta.x * delta.x + delta.z * delta.z
if horizontal_dist_sq <= radius_sq:
return true
return false
func _input(event: InputEvent) -> void:
if _is_photo_mode_active: return
if EnvironmentManagerRoot.is_keyboard_input_blocked(get_tree()):
return
if event is InputEventKey and event.pressed and not event.echo:
if event.keycode == KEY_T:
_goto_next_stop()
@@ -319,6 +390,9 @@ func _test_manual_fireworks() -> void:
func _goto_next_stop() -> void:
if stop_offset.size() == 0 or stop_ongoing or is_restarting:
return
if not _find_next_valid_stop(train_speed >= 0.0):
return
var target_offset = stop_offset[next_stop_index]
train_progress = target_offset
_execute_stop(train_speed >= 0)
@@ -328,6 +402,7 @@ func _physics_process(delta: float) -> void:
train_move(delta)
func input_controls_management(delta: float) -> void:
if EnvironmentManagerRoot.is_keyboard_input_blocked(get_tree()): return
if not is_inmotion or _is_photo_mode_active: return
if Input.is_action_pressed("train_speed_up"):
train_speed += manual_acceleration * delta
@@ -344,15 +419,20 @@ func train_move(delta: float) -> void:
if not stop_ongoing:
var last_progress = train_progress
var has_valid_stop := false
if enable_stops and stop_offset.size() > 0 and not is_restarting:
var target_offset = stop_offset[next_stop_index]
var dist = target_offset - train_progress
dist = wrapf(dist + total_length / 2.0, 0.0, total_length) - total_length / 2.0
if abs(dist) < brake_distance and sign(dist) == sign(train_speed):
var t = abs(dist) / brake_distance
stop_multiply = max(smoothstep(0.0, 1.0, t), 0.05)
has_valid_stop = _find_next_valid_stop(train_speed >= 0.0)
if has_valid_stop:
var target_offset = stop_offset[next_stop_index]
var dist = target_offset - train_progress
dist = wrapf(dist + total_length / 2.0, 0.0, total_length) - total_length / 2.0
if abs(dist) < brake_distance and sign(dist) == sign(train_speed):
var t = abs(dist) / brake_distance
stop_multiply = max(smoothstep(0.0, 1.0, t), 0.05)
else:
stop_multiply = 1.0
else:
stop_multiply = 1.0
@@ -360,7 +440,7 @@ func train_move(delta: float) -> void:
train_progress += current_speed * delta
_track_steam_distance(abs(current_speed) * delta)
if enable_stops and stop_offset.size() > 0:
if enable_stops and stop_offset.size() > 0 and has_valid_stop:
var target_offset = stop_offset[next_stop_index]
var exceeded_forward = (current_speed > 0 and last_progress < target_offset and train_progress >= target_offset)
var exceeded_back = (current_speed < 0 and last_progress > target_offset and train_progress <= target_offset)
@@ -447,19 +527,16 @@ func _check_distance_achievements(total_distance_km: int) -> void:
AchievementManager.unlock("ACH_DISTANCE_200")
func _execute_stop(go_forward: bool = true) -> void:
if not _find_next_valid_stop(go_forward):
stop_multiply = 1.0
return
stop_ongoing = true
stop_multiply = 0.0
var current_stop_index = next_stop_index
if go_forward:
next_stop_index += 1
if next_stop_index >= stop_offset.size():
next_stop_index = 0
else:
next_stop_index -= 1
if next_stop_index < 0:
next_stop_index = stop_offset.size() - 1
_advance_next_stop_index(go_forward)
if fireworks_scene != null:
var marker_position = stops_position[current_stop_index]