extends Spatial

class_name Route

signal road_added(road)
signal road_removed(road)


func get_last_road():
	if get_child_count() > 0:
		return get_child(get_child_count() - 1)
	else:
		return null


func get_road(road_index):
	return get_child(road_index)


remotesync func add_road(type, creator):
	var transform = get_global_transform()
	var last_road = get_last_road()
	if last_road != null:
		transform = last_road.get_global_transform();
		var curve = last_road.get_curve()
		transform = transform.translated(curve.get_point_position(curve.get_point_count() - 1))
		if last_road.get_end_rotation().length() != 0:
			transform.basis = transform.basis.rotated(last_road.get_end_rotation().normalized(), last_road.get_end_rotation().length())
	
	var new_road = roads_factory.get_road_instance(type)
	new_road.set_global_transform(transform)
	new_road.set_preview(false)
	new_road.set_creator(creator)
	
	if creator > 0:
		var player = gamestate.game.get_node(str(creator))
		if player != null:
			new_road.set_color(player.get_player_color())
	
	add_child(new_road)
		
	emit_signal("road_added", new_road)


remotesync func remove_road(road_index):
	var road = get_child(road_index)
	remove_child(road)
	emit_signal("road_removed", road)