switch
This commit is contained in:
@@ -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
|
||||
|
||||
17
scripts/rail_switch.gd
Normal file
17
scripts/rail_switch.gd
Normal file
@@ -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)
|
||||
1
scripts/rail_switch.gd.uid
Normal file
1
scripts/rail_switch.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bhan6tvt4sow
|
||||
14
scripts/rail_swtich.gd
Normal file
14
scripts/rail_swtich.gd
Normal file
@@ -0,0 +1,14 @@
|
||||
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
|
||||
|
||||
1
scripts/rail_swtich.gd.uid
Normal file
1
scripts/rail_swtich.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b5x80gpkn1swt
|
||||
@@ -1,21 +1,29 @@
|
||||
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
|
||||
@export var rail_a: Path3D
|
||||
@export var rail_b: Path3D
|
||||
|
||||
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
|
||||
|
||||
|
||||
func _handle_input(delta: float) -> void:
|
||||
@@ -29,3 +37,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
|
||||
|
||||
@@ -3,9 +3,9 @@ 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
|
||||
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
_update_ui()
|
||||
@@ -13,6 +13,8 @@ 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"):
|
||||
SceneSwitcher.switch_scene(main_scene.resource_path)
|
||||
|
||||
|
||||
func _update_ui() -> void:
|
||||
|
||||
Reference in New Issue
Block a user