diff --git a/.godot/editor/rail_switch.gd-folding-8e4deca5ebc583cb0317bfa5f2843afb.cfg b/.godot/editor/rail_switch.gd-folding-8e4deca5ebc583cb0317bfa5f2843afb.cfg new file mode 100644 index 0000000..e860dd4 --- /dev/null +++ b/.godot/editor/rail_switch.gd-folding-8e4deca5ebc583cb0317bfa5f2843afb.cfg @@ -0,0 +1,3 @@ +[folding] + +sections_unfolded=PackedStringArray() diff --git a/.godot/editor/rail_swtich.gd-folding-66615c4df519abfd9e79d3c9f0c4dc07.cfg b/.godot/editor/rail_swtich.gd-folding-66615c4df519abfd9e79d3c9f0c4dc07.cfg new file mode 100644 index 0000000..e860dd4 --- /dev/null +++ b/.godot/editor/rail_swtich.gd-folding-66615c4df519abfd9e79d3c9f0c4dc07.cfg @@ -0,0 +1,3 @@ +[folding] + +sections_unfolded=PackedStringArray() diff --git a/scenes/train_control.tscn b/scenes/train_control.tscn index e4d6b1e..5558f9e 100644 --- a/scenes/train_control.tscn +++ b/scenes/train_control.tscn @@ -7,6 +7,7 @@ [ext_resource type="ArrayMesh" uid="uid://cm8un80nubjbi" path="res://resources/train kit/train-diesel-a.obj" id="3_jy5c4"] [ext_resource type="Script" uid="uid://b65vwus8xxmun" path="res://scripts/traincontrol_camera.gd" id="4_jy5c4"] [ext_resource type="Script" uid="uid://df2oi5vyeak81" path="res://scripts/train_movement.gd" id="6_1lqs8"] +[ext_resource type="Script" uid="uid://b5x80gpkn1swt" path="res://scripts/rail_swtich.gd" id="7_cenvf"] [sub_resource type="PlaneMesh" id="PlaneMesh_23vqf"] size = Vector2(100, 100) @@ -17,10 +18,10 @@ albedo_color = Color(2.153851e-06, 0.25305444, 0.03645257, 1) [sub_resource type="Curve3D" id="Curve3D_0b1nv"] closed = true _data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, -41.962486, 0.03566742, -44.78871, 0, 0, 0, 0, 0, 0, -7.760188, 0.036315918, -45.60305, 0, 0, 0, 0, 0, 0, 21.603975, 0.03604126, -45.26774, 0, 0, 0, 0, 0, 0, 41.148148, 0.03127289, -39.27994, 0, 0, 0, 0, 0, 0, 44.2618, 0.02154541, -27.064838, 0, 0, 0, 0, 0, 0, 44.118095, 0.0033874512, -4.2633114, 0, 0, 0, 0, 0, 0, 26.96904, 0.003112793, -3.9279928, 0, 0, 0, 0, 0, 0, 3.4968712, 0.0079574585, -10.011598, 0, 0, 0, 0, 0, 0, -3.8321874, 0.024543762, -30.849133, 0, 0, 0, 0, 0, 0, -25.627771, 0.026069641, -32.765224, 0, 0, 0, 0, 0, 0, -31.28027, 0.015075684, -18.969343, 0, 0, 0, 0, 0, 0, -23.472174, 0.003479004, -4.4070177, 0, 0, 0, 0, 0, 0, 12.167218, -0.011207581, 14.035401, 0, 0, 0, 0, 0, 0, 34.681347, -0.013763428, 17.244862, 0, 0, 0, 0, 0, 0, 46.99225, -0.019981384, 25.052942, 0, 0, 0, 0, 0, 0, 42.72894, -0.03504944, 43.97439, 0, 0, 0, 0, 0, 0, 14.897652, -0.036231995, 45.459354, 0, 0, 0, 0, 0, 0, -23.088955, -0.034858704, 43.73488, 0, 0, 0, 0, 0, 0, -44.357616, -0.034965515, 43.878586, 0, 0, 0, 0, 0, 0, -45.603073, -0.010246277, 12.837832, 0, 0, 0, 0, 0, 0, -34.585533, 0.0079422, -10.0116005, 0, 0, 0, 0, 0, 0, -38.321926, 0.022087097, -27.78338), -"tilts": PackedFloat32Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) +"points": PackedVector3Array(-2.1678085, -0.043792725, 1.658886, 2.1678085, 0.043792725, -1.658886, -41.962486, 0.03566742, -44.78871, 0, 0, 0, 0, 0, 0, -7.760188, 0.036315918, -45.60305, -6.559948, 0.007423401, -0.16356277, 6.559948, -0.007423401, 0.16356277, 21.603975, 0.03604126, -45.26774, -3.4513588, 0.124176025, -4.544277, 3.4513588, -0.124176025, 4.544277, 40.931072, 0.032211304, -40.35701, 0.5563507, 0.09750366, -3.6268482, -0.5563507, -0.09750366, 3.6268482, 45.43544, 0.021453857, -26.947992, 3.438774, 0.09018707, -3.405755, -3.438774, -0.09018707, 3.405755, 38.412643, 0.47789, -9.150471, 6.6514378, -0.022911072, 0.7331085, -6.6514378, 0.022911072, -0.7331085, 25.856459, 0.10243797, -3.7440395, 5.315757, -0.10071564, 3.6390858, -5.315757, 0.10071564, -3.6390858, 3.4968712, 0.0079574585, -10.011598, 4.9409637, -0.0754776, 2.709568, -4.9409637, 0.0754776, -2.709568, -3.8321874, 0.024543762, -30.849133, 6.360344, 0.0488739, -1.9223251, -6.360344, -0.0488739, 1.9223251, -22.311077, 0.005493164, -31.887383, -0.9378357, 0.12989807, -4.797656, 0.9378357, -0.12989807, 4.797656, -31.28027, 0.015075684, -18.969343, -7.12364, 0.096969604, -3.4738579, 7.12364, -0.096969604, 3.4738579, -23.472174, 0.003479004, -4.4070177, -9.224047, 0.08418274, -2.961853, 9.224047, -0.08418274, 2.961853, 12.167218, -0.011207581, 14.035401, -8.825874, 0.03265381, -1.0564928, 8.825874, -0.03265381, 1.0564928, 32.632145, -0.011894226, 14.993831, 0, 0, 0, 0, 0, 0, 39.98447, -0.019104004, 24.146927, 4.9879036, 0.122177124, -4.6163826, -4.9879036, -0.122177124, 4.6163826, 35.724655, 0.047676086, 33.444454, 8.1661625, 0.014076233, -0.6628723, -8.1661625, -0.014076233, 0.6628723, 11.495622, 0.0951767, 40.647045, 4.9570847, -0.058029175, 2.0650558, -4.9570847, 0.058029175, -2.0650558, -22.318954, 0.051948547, 40.504288, 4.9869003, -0.18069458, 6.610588, -4.9869003, 0.18069458, -6.610588, -37.494194, 0.080329895, 26.992659, -1.5943985, -0.19984436, 7.435757, 1.5943985, 0.19984436, -7.435757, -46.58226, 0.10346222, 8.63745, -0.4270401, -0.16287994, 6.045038, 0.4270401, 0.16287994, -6.045038, -39.930115, 0.050865173, -11.514841, 4.8086166, -0.20992279, 7.687052, -4.8086166, 0.20992279, -7.687052, -35.56163, 0.15759277, -32.8561, -0.73628235, -0.15232849, 5.656826, 0.73628235, 0.15232849, -5.656826, -45.566254, 0.602231, -39.59991), +"tilts": PackedFloat32Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) } -point_count = 22 +point_count = 23 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ke0jj"] albedo_texture = ExtResource("1_ihlg3") @@ -31,6 +32,14 @@ uv1_offset = Vector3(0, -0.61, 0) metallic = 1.0 roughness = 0.25 +[sub_resource type="Curve3D" id="Curve3D_1lqs8"] +closed = true +_data = { +"points": PackedVector3Array(-6.2964935, 0.06638336, -2.3570595, 6.2964935, -0.06638336, 2.3570595, 24, 0, -36, 0.40251923, 0.1432724, -5.316846, -0.40251923, -0.1432724, 5.316846, 30.535202, 0.016418457, -19.69652, -3.0211945, 0.21754456, -8.010609, 3.0211945, -0.21754456, 8.010609, 37.792625, 0.0013275146, -0.5123644, 7.037945, 0.16593933, -6.2738285, -7.037945, -0.16593933, 6.2738285, 33.177887, 0.0013427734, 21.921793, 12.049584, -0.0362854, 1.1334801, -12.049584, 0.0362854, -1.1334801, 7.185522, 0.0013427734, 33.84686, 3.006013, -0.23966217, 8.824167, -3.006013, 0.23966217, -8.824167, -5.633688, 0.0127334595, 16.86435, 0.09129143, -0.14064789, 5.2124147, -0.09129143, 0.14064789, -5.2124147, -20.520624, 0.0013427734, -3.9991686, -6.1595325, -0.18273163, 6.8687496, 6.1595325, 0.18273163, -6.8687496, -10.095118, 0.0013427734, -27.90497), +"tilts": PackedFloat32Array(0, 0, 0, 0, 0, 0, 0, 0) +} +point_count = 8 + [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_jgp6u"] sky_top_color = Color(0.6262097, 0.76737565, 0.9309246, 1) sky_horizon_color = Color(0.82665706, 0.8808251, 0.95468724, 1) @@ -63,10 +72,10 @@ main_scene = ExtResource("2_0b1nv") mesh = SubResource("PlaneMesh_23vqf") surface_material_override/0 = SubResource("StandardMaterial3D_23vqf") -[node name="Rails" type="Path3D" parent="Ground" unique_id=1160818899] +[node name="Rails1" type="Path3D" parent="Ground" unique_id=1160818899] curve = SubResource("Curve3D_0b1nv") -[node name="Ballast" type="CSGPolygon3D" parent="Ground/Rails" unique_id=259312931] +[node name="Ballast" type="CSGPolygon3D" parent="Ground/Rails1" unique_id=259312931] polygon = PackedVector2Array(-1, 0, -1, 0.1, 1, 0.1, 1, 0) mode = 2 path_node = NodePath("..") @@ -81,7 +90,7 @@ path_u_distance = 1.0 path_joined = true material = SubResource("StandardMaterial3D_ke0jj") -[node name="LRail" type="CSGPolygon3D" parent="Ground/Rails" unique_id=477776682] +[node name="LRail" type="CSGPolygon3D" parent="Ground/Rails1" unique_id=477776682] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.24889, -4.7985353, 2.2341003) polygon = PackedVector2Array(-0.5, 0, -0.5, 0.2, -0.4, 0.2, -0.4, 0) mode = 2 @@ -97,7 +106,7 @@ path_u_distance = 1.0 path_joined = true material = SubResource("StandardMaterial3D_ihlg3") -[node name="RRail" type="CSGPolygon3D" parent="Ground/Rails" unique_id=965437995] +[node name="RRail" type="CSGPolygon3D" parent="Ground/Rails1" unique_id=965437995] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.24889, -4.7985353, 2.2341003) polygon = PackedVector2Array(0.5, 0, 0.5, 0.2, 0.4, 0.2, 0.5, 0) mode = 2 @@ -113,15 +122,78 @@ path_u_distance = 1.0 path_joined = true material = SubResource("StandardMaterial3D_ihlg3") -[node name="TrainEngine" type="PathFollow3D" parent="Ground/Rails" unique_id=973990755] -transform = Transform3D(0.026897233, -2.1416474e-05, -1.1296921, 0, 1.130161, -2.141972e-05, 1.1296905, 5.0991304e-07, 0.02689727, -41.962486, 0.03566742, -44.78871) +[node name="TrainEngine" type="PathFollow3D" parent="Ground/Rails1" unique_id=973990755] +transform = Transform3D(0.6867339, -0.014397825, -0.8973005, 0, 1.130071, -0.018126711, 0.8974141, 0.01101774, 0.68664694, -41.962486, 0.03566742, -44.78871) rotation_mode = 4 script = ExtResource("2_jgp6u") -[node name="Train" type="MeshInstance3D" parent="Ground/Rails/TrainEngine" unique_id=65189673] +[node name="Rails2" type="Path3D" parent="Ground" unique_id=1426938039] +curve = SubResource("Curve3D_1lqs8") + +[node name="Ballast" type="CSGPolygon3D" parent="Ground/Rails2" unique_id=608449521] +polygon = PackedVector2Array(-1, 0, -1, 0.1, 1, 0.1, 1, 0) +mode = 2 +path_node = NodePath("..") +path_interval_type = 0 +path_interval = 1.0 +path_simplify_angle = 0.0 +path_rotation = 2 +path_rotation_accurate = false +path_local = false +path_continuous_u = true +path_u_distance = 1.0 +path_joined = true +material = SubResource("StandardMaterial3D_ke0jj") + +[node name="RRail" type="CSGPolygon3D" parent="Ground/Rails2" unique_id=469196308] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.24889, -4.7985353, 2.2341003) +polygon = PackedVector2Array(0.5, 0, 0.5, 0.2, 0.4, 0.2, 0.5, 0) +mode = 2 +path_node = NodePath("..") +path_interval_type = 0 +path_interval = 1.0 +path_simplify_angle = 0.0 +path_rotation = 2 +path_rotation_accurate = false +path_local = false +path_continuous_u = true +path_u_distance = 1.0 +path_joined = true +material = SubResource("StandardMaterial3D_ihlg3") + +[node name="LRail" type="CSGPolygon3D" parent="Ground/Rails2" unique_id=1887208549] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.24889, -4.7985353, 2.2341003) +polygon = PackedVector2Array(-0.5, 0, -0.5, 0.2, -0.4, 0.2, -0.4, 0) +mode = 2 +path_node = NodePath("..") +path_interval_type = 0 +path_interval = 1.0 +path_simplify_angle = 0.0 +path_rotation = 2 +path_rotation_accurate = false +path_local = false +path_continuous_u = true +path_u_distance = 1.0 +path_joined = true +material = SubResource("StandardMaterial3D_ihlg3") + +[node name="TrainEngine" type="PathFollow3D" parent="Ground/Rails2" unique_id=1618605131] +transform = Transform3D(-0.39616987, 0.010450672, -1.0582525, 0, 1.1301593, 0.01115706, 1.0583019, 0.003912155, -0.3961513, 24, 0, -36) +rotation_mode = 4 +script = ExtResource("2_jgp6u") + +[node name="Train" type="MeshInstance3D" parent="Ground" unique_id=65189673] +transform = Transform3D(0.6867333, -0.014397784, -0.89729905, 0, 1.1300678, -0.018126683, 0.8974134, 0.011017708, 0.68664587, -41.962486, 0.03566742, -44.78871) mesh = ExtResource("3_jy5c4") +skeleton = NodePath("../Rails1/TrainEngine") script = ExtResource("6_1lqs8") +[node name="RailSwtich" type="Area3D" parent="Ground" unique_id=1897237029 node_paths=PackedStringArray("rail_a", "rail_b", "switch_to_rail")] +script = ExtResource("7_cenvf") +rail_a = NodePath("../Rails1") +rail_b = NodePath("../Rails2") +switch_to_rail = NodePath("../Rails1") + [node name="DirectionalLight3D" type="DirectionalLight3D" parent="." unique_id=1729725358] shadow_enabled = true shadow_bias = 0.04 @@ -135,7 +207,8 @@ current = true fov = 11.9 near = 0.001 script = ExtResource("4_jy5c4") -train = NodePath("../Ground/Rails/TrainEngine/Train") +train = NodePath("../Ground/Train") +follow_distance = 30.0 [node name="HUD" type="Control" parent="." unique_id=563257159] layout_mode = 3 @@ -155,9 +228,9 @@ label_settings = SubResource("LabelSettings_1lqs8") [node name="camera_label" type="Label" parent="HUD" unique_id=1719494020] layout_mode = 0 -offset_left = 20.0 +offset_left = 19.0 offset_top = 43.0 -offset_right = 1280.0 +offset_right = 1279.0 offset_bottom = 66.0 size_flags_vertical = 0 text = "Camera: " @@ -165,15 +238,25 @@ label_settings = SubResource("LabelSettings_1lqs8") [node name="controls_label" type="Label" parent="HUD" unique_id=1262113688] layout_mode = 0 -offset_left = 20.0 -offset_top = 64.0 -offset_right = 1280.0 -offset_bottom = 217.0 +offset_left = 19.0 +offset_top = 83.0 +offset_right = 1279.0 +offset_bottom = 236.0 size_flags_vertical = 0 text = "Comandi: \"W / S - Accelera / Rallenta\", \"A / D - Ruota camera\", \"SPACE - Cambia camera\", -\"TAB - Cambia treno\", +\"TAB\" - Cambia binario, \"ESC - Esci\"" label_settings = SubResource("LabelSettings_1lqs8") + +[node name="switch_label" type="Label" parent="HUD" unique_id=462391845] +layout_mode = 0 +offset_left = 19.0 +offset_top = 64.0 +offset_right = 1279.0 +offset_bottom = 87.0 +size_flags_vertical = 0 +text = "Binario: " +label_settings = SubResource("LabelSettings_1lqs8") diff --git a/scripts/engine.gd b/scripts/engine.gd index 555a833..a655832 100644 --- a/scripts/engine.gd +++ b/scripts/engine.gd @@ -1,9 +1,9 @@ extends PathFollow3D -class_name TrainEngine - -@onready var train: MeshInstance3D = $Train - - -func _physics_process(delta: float) -> void: - if train: - progress += train.current_speed * delta +#class_name TrainEngine +# +#@onready var train: MeshInstance3D = $"../../Train" +# +# +#func _physics_process(delta: float) -> void: + #if train: + #progress += train.current_speed * delta diff --git a/scripts/rail_switch.gd b/scripts/rail_switch.gd new file mode 100644 index 0000000..d07a5de --- /dev/null +++ b/scripts/rail_switch.gd @@ -0,0 +1,17 @@ +extends Area3D +class_name RailSwitch + +@export var rail_a: Path3D +@export var rail_b: Path3D +@export var switch_to_rail: Path3D # Il binario verso cui deviare + +# Cambia direzione dello scambio +func toggle_switch() -> void: + if switch_to_rail == rail_a: + switch_to_rail = rail_b + else: + switch_to_rail = rail_a + +func _on_body_entered(body: Node3D) -> void: + if body is Train: + body.switch_to_rail(switch_to_rail) diff --git a/scripts/rail_switch.gd.uid b/scripts/rail_switch.gd.uid new file mode 100644 index 0000000..d424258 --- /dev/null +++ b/scripts/rail_switch.gd.uid @@ -0,0 +1 @@ +uid://bhan6tvt4sow diff --git a/scripts/rail_swtich.gd b/scripts/rail_swtich.gd new file mode 100644 index 0000000..68738c7 --- /dev/null +++ b/scripts/rail_swtich.gd @@ -0,0 +1,17 @@ +extends Area3D + +@export var rail_a: Path3D +@export var rail_b: Path3D +@export var switch_to_rail: Path3D # Il binario verso cui deviare + + +# Cambia direzione dello scambio +func toggle_switch() -> void: + if switch_to_rail == rail_a: + switch_to_rail = rail_b + else: + switch_to_rail = rail_a + + +#func get_state_name() -> String: + #return "BIANRIO 1" if current_state == SwitchState.LEFT else "BINARIO 2" diff --git a/scripts/rail_swtich.gd.uid b/scripts/rail_swtich.gd.uid new file mode 100644 index 0000000..f47aa79 --- /dev/null +++ b/scripts/rail_swtich.gd.uid @@ -0,0 +1 @@ +uid://b5x80gpkn1swt diff --git a/scripts/train_movement.gd b/scripts/train_movement.gd index 2f595bf..745bb36 100644 --- a/scripts/train_movement.gd +++ b/scripts/train_movement.gd @@ -1,21 +1,30 @@ extends MeshInstance3D +class_name TrainBody signal speed_changed(new_speed: float) +signal rail_switched(old_rail: Path3D, new_rail: Path3D) @export_group("Train") @export var base_speed: float = 15.0 @export var max_speed: float = 50.0 @export var min_speed: float = 3.0 @export var acceleration: float = 12.0 +@onready var rail_a: Path3D = $"../Rails1" +@onready var rail_b: Path3D = $"../Rails2" +var current_follower: PathFollow3D +var current_rail: Path3D var current_speed: float = 15.0 func _ready() -> void: current_speed = base_speed + switch_to_rail(rail_a) func _process(delta: float) -> void: _handle_input(delta) + current_follower.progress += current_speed * delta + global_transform = current_follower.global_transform func _handle_input(delta: float) -> void: @@ -29,3 +38,35 @@ func _handle_input(delta: float) -> void: func get_speed() -> float: return current_speed + + +func switch_to_rail(new_rail: Path3D) -> void: + if new_rail == current_rail: + return + + current_rail = new_rail + current_follower = new_rail.get_node("TrainEngine") as PathFollow3D + + var closest_offset = find_closest_point_on_path(new_rail, global_position) + current_follower.progress = closest_offset + + +func find_closest_point_on_path(path: Path3D, point: Vector3) -> float: + var curve = path.curve + var closest_offset = 0.0 + var min_distance = INF + + var length = curve.get_baked_length() + var steps = 100 # Più alto = più preciso ma più lento + + for i in range(steps + 1): + var offset = (float(i) / steps) * length + # Trasforma il punto locale della curva in coordinate globali + var path_point = path.global_transform * curve.sample_baked(offset) + var dist = point.distance_to(path_point) + + if dist < min_distance: + min_distance = dist + closest_offset = offset + + return closest_offset diff --git a/scripts/traincontrol.gd b/scripts/traincontrol.gd index 26fce01..6b4faa9 100644 --- a/scripts/traincontrol.gd +++ b/scripts/traincontrol.gd @@ -3,8 +3,10 @@ extends Node3D @onready var speed_label : Label = $HUD/speed_label @onready var camera_label : Label = $HUD/camera_label -@onready var train: MeshInstance3D = $Ground/Rails/TrainEngine/Train +@onready var train: MeshInstance3D = $Ground/Train @onready var camera: TrainControlCamera = $TrainCamera +@onready var rail_switch: Area3D = $Ground/RailSwtich +@onready var switch_label : Label = $HUD/switch_label func _process(_delta: float) -> void: @@ -13,8 +15,10 @@ func _process(_delta: float) -> void: if Input.is_action_just_pressed("ui_cancel"): SceneSwitcher.switch_scene(main_scene.resource_path) - - + if Input.is_action_just_pressed("change_train"): + _toggle_rail() + + func _update_ui() -> void: if speed_label and train: var kmh = train.get_speed() * 3.6 @@ -22,3 +26,22 @@ func _update_ui() -> void: if camera_label and camera: camera_label.text = "Camera: %s" % camera.get_mode_name() + + +func _toggle_rail() -> void: + if train and rail_switch: + rail_switch.toggle_switch() + train.switch_to_rail(rail_switch.switch_to_rail) + + +func _update_switch_status(): + if not switch_label: + return + + #if rail_switch: + #var state_name = rail_switch.get_state_name() + #var color = Color.YELLOW if rail_switch.current_state == rail_switch.SwitchState.LEFT else Color.CYAN + #switch_label.text = "Scambio: %s" % state_name + #switch_label.add_theme_color_override("font_color", color) + #else: + #switch_label.text = "Scambio: N/A"