extends Spatial

var road_ray:RayCast
var collider
var road:Road
var road_index:int = 0

puppet var slave_transform:Transform

signal road_entered(road)


func _ready():
	road_ray = get_node("ray")


func _physics_process(delta):
	if is_network_master():
		if road_ray.is_colliding():
			if road_ray.get_collider() != collider:
				var parent = road_ray.get_collider().get_parent()
				if parent is Road && (road == null && road_index == parent.get_index() || road.get_index() == parent.get_index() - 1) && !parent.is_preview():
					collider = road_ray.get_collider()
					road = parent
					road_index = road.get_index()
					emit_signal("road_entered",road)
		rset_unreliable("slave_transform", get_global_transform())
	else:
		set_global_transform(slave_transform)
