122 lines
3.3 KiB
GDScript
122 lines
3.3 KiB
GDScript
extends Node3D
|
|
|
|
@onready var track_generator: TrackGenerator = $TrackGenerator
|
|
@onready var train: Train = $Train
|
|
@onready var camera: TrainCamera = $TrainCamera
|
|
|
|
@onready var speed_label : Label = $HUD/speed_label
|
|
@onready var camera_label : Label = $HUD/camera_label
|
|
@onready var distance_label : Label = $HUD/tripdistance_label
|
|
@onready var total_distance_label : Label = $HUD/totaldistance_label
|
|
@onready var auto_save_timer = $AutoSaveTimer
|
|
|
|
var savegame_path: String = "user://savegame.save"
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
# caricamento stato gioco
|
|
##load_game()
|
|
# avvia autosaver
|
|
auto_save_timer.start()
|
|
|
|
train.track_generator = track_generator
|
|
camera.train = train
|
|
|
|
# Terreno
|
|
var ground = MeshInstance3D.new()
|
|
ground.name = "Ground"
|
|
var plane = PlaneMesh.new()
|
|
plane.size = Vector2(2000, 2000)
|
|
ground.mesh = plane
|
|
|
|
var ground_mat = StandardMaterial3D.new()
|
|
ground_mat.albedo_color = Color(0.35, 0.48, 0.28)
|
|
ground_mat.roughness = 1.0
|
|
ground.material_override = ground_mat
|
|
ground.position.y = -0.15
|
|
add_child(ground)
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
_update_ui()
|
|
_update_ground()
|
|
|
|
if Input.is_action_just_pressed("ui_cancel"):
|
|
get_tree().quit()
|
|
|
|
|
|
func _update_ui() -> void:
|
|
if speed_label and train:
|
|
var kmh = train.get_speed() * 3.6
|
|
speed_label.text = "Velocità: %.0f km/h" % kmh
|
|
|
|
if distance_label and train:
|
|
var km = train.distance_traveled / 1000.0
|
|
distance_label.text = "Distanza viaggio: %.2f km" % km
|
|
|
|
if total_distance_label and train and Gamestate:
|
|
Gamestate.AddToTotalDistance(train.distance_traveled)
|
|
var km = (Gamestate.getTotalDistance() / 1000)
|
|
total_distance_label.text = "Distanza totale: %.2f km" % km
|
|
|
|
if camera_label and camera:
|
|
camera_label.text = "Camera: %s" % camera.get_mode_name()
|
|
|
|
|
|
func _update_ground() -> void:
|
|
var ground = get_node_or_null("Ground")
|
|
if ground and train:
|
|
var pos = train.get_locomotive_position()
|
|
ground.position.x = pos.x
|
|
ground.position.z = pos.z
|
|
|
|
|
|
func save_game():
|
|
var save_file = FileAccess.open(savegame_path, FileAccess.WRITE)
|
|
var save_nodes = get_tree().get_nodes_in_group("state")
|
|
|
|
for node in save_nodes:
|
|
if !node.has_method("save"):
|
|
continue
|
|
var node_data = node.call("save") # ogni nodo implementa il suo save()
|
|
save_file.store_line(JSON.stringify(node_data))
|
|
|
|
|
|
func load_game():
|
|
if not FileAccess.file_exists(savegame_path):
|
|
return # nessun salvataggio trovato
|
|
|
|
var existing_nodes = get_tree().get_nodes_in_group("state")
|
|
for node in existing_nodes:
|
|
node.queue_free()
|
|
|
|
var save_file = FileAccess.open(savegame_path, FileAccess.READ)
|
|
while save_file.get_position() < save_file.get_length():
|
|
var json_string = save_file.get_line()
|
|
var node_data = JSON.parse_string(json_string)
|
|
|
|
if node_data == null:
|
|
continue
|
|
|
|
# Ricrea il nodo dalla scena salvata
|
|
var new_node = load(node_data["filename"]).instantiate()
|
|
|
|
# Aggiungi al parent originale
|
|
get_node(node_data["parent"]).add_child(new_node)
|
|
|
|
# Ripristina le proprietà
|
|
new_node.SetInitialValue(node_data["total_distance"])
|
|
##new_node.total_distance = new_node.initial_total_distance
|
|
|
|
# Ripristina proprietà custom
|
|
for key in node_data.keys():
|
|
if key in ["filename", "parent", "total_distance"]:
|
|
continue
|
|
if key in new_node:
|
|
new_node.set(key, node_data[key])
|
|
|
|
|
|
func _on_timer_timeout() -> void:
|
|
save_game()
|