frame-of-mind/src/base-environments/transition/code/fahrplan.gd

101 lines
3.5 KiB
GDScript3
Raw Normal View History

2026-01-20 22:19:20 +00:00
class_name Fahrplan extends Node3D
2026-01-20 21:47:44 +00:00
## easy graph: <station> : [Track1Train1 (next halt), Track1Train2], [Track2Train1(next halt), ...]
## CAVEAT: These must be direct children of the fahrplan!
@onready var fahrplan : Dictionary[Station, Array] = {
$hirschfeld: [["u2", $uni_mensa, "u1", $parity_square],[]],
$uni_mensa : [["u2", $uni_main],[]],
$uni_main: [["u2", $ministry],["u8", $rosenthal, "u2", $uni_mensa, "u1", $saint_exupery]],
$parity_square : [["u4", $saint_exupery, "u1", $saint_exupery], []],
$saint_exupery : [["u4", $rosenthal], ["u1", $uni_main]],
$ministry : [null, null], # Endstation
$rosenthal : [null, null], # Endstation
2026-01-20 21:47:44 +00:00
}
## List of all registered station nodes, used to unparent them
@onready var stations : Array[Station] = fahrplan.keys()
2026-01-20 21:47:44 +00:00
@onready var tracks : Array[Dolly] = [%Track0Dolly, %Track1Dolly]
@export var empty_train_random_delay : Vector2 = Vector2(5.0, 20.0)
2026-01-20 21:47:44 +00:00
func _set_signage_texts(group: StringName, message: String) -> void:
get_tree().call_group(group, "set_text", "")
for i in range(len(message)):
get_tree().call_group(group, "set_text", message.substr(0, i+1))
await get_tree().create_timer(0.05).timeout
func _ready() -> void:
2026-01-20 22:19:20 +00:00
await get_tree().process_frame
tracks[0].train_left.connect(_player_train_left)
tracks[1].train_left.connect(_player_train_left)
_unparent_all_stations_except($hirschfeld)
2026-01-24 12:59:14 +00:00
enter_station(current)
2026-01-20 21:47:44 +00:00
2026-01-24 12:59:14 +00:00
## The current station
var current : Station
2026-01-20 21:47:44 +00:00
var stop := false
var destinations : Array[Station] = [null, null]
2026-01-20 21:47:44 +00:00
## Begins the scheduled traffic (looping through the list of destinations) on a given track
func _begin_traffic_loop(track: Dolly, cancel: Array) -> void:
2026-01-24 12:59:14 +00:00
if not track.player_on_board and fahrplan[current][track.index] == null: # empty trains dont arrive at endstation
2026-01-21 00:54:01 +00:00
track.arrive(true)
_set_signage_texts(track.signage_group, "Endstation")
return
2026-01-24 12:59:14 +00:00
var routes : Array = fahrplan[current][track.index]
2026-01-20 21:47:44 +00:00
if routes.is_empty():
_set_signage_texts(track.signage_group, "Verkehr z. Zt.\nunregelmäßig")
return
while true:
2026-01-21 00:54:01 +00:00
if cancel.is_empty(): return # abort the loop
2026-01-24 12:59:14 +00:00
var line : StringName = routes.pop_front()
var next : Station = routes.pop_front()
routes.append(line) # It's a ring buffer
2026-01-20 21:47:44 +00:00
routes.append(next) # It's a ring buffer
destinations[track.index] = next
2026-01-24 12:59:14 +00:00
track.set_line(line)
_set_signage_texts(track.signage_group, current.get_label(line, next)) # TODO: load nice string
2026-01-20 21:47:44 +00:00
# Vary our schedule a little by making empty trains wait a random amount of time
if not track.player_on_board:
await get_tree().create_timer(randf_range(empty_train_random_delay.x, empty_train_random_delay.y)).timeout
2026-01-21 00:54:01 +00:00
2026-01-20 21:47:44 +00:00
if cancel.is_empty(): return # abort the loop
2026-01-21 00:54:01 +00:00
await track.cycle() # arrive and depart
2026-01-20 21:47:44 +00:00
var cancellation_token : Array
2026-01-24 12:59:14 +00:00
func enter_station(station: Station):
2026-01-20 21:47:44 +00:00
prints("------------", "ENTER STATION", station, station.name, "------------")
2026-01-24 12:59:14 +00:00
remove_child(current)
current = station
add_child(station)
2026-01-20 21:47:44 +00:00
cancellation_token = ["go"] # Allocate a new stopping token
_begin_traffic_loop(tracks[0], cancellation_token)
_begin_traffic_loop(tracks[1], cancellation_token)
2026-01-20 21:47:44 +00:00
func _player_train_left(track: Dolly) -> void:
2026-01-20 21:47:44 +00:00
cancellation_token.clear()
enter_station(destinations[track.index])
func _unparent_all_stations_except(except : Node3D):
for station in stations:
2026-01-24 12:59:14 +00:00
assert(station.get_parent() == self, "A station that isn't a child of Fahrplan is in the Fahrplan: %s" % station.name)
station.visible = true # Make visible by default, parenting handles visibility and collision
if station == except:
2026-01-24 12:59:14 +00:00
current = station
continue
remove_child(station)