extends Spatial

class_name Preview

var player:Player
var item:Road


func _ready(): 
	gamestate.game.route.connect("road_added",self,"_on_route_road_added")


func _on_route_road_added(road):
	if is_network_master() && item:
		var transform = road.get_global_transform();
		var curve = road.get_curve()
		transform = transform.translated(curve.get_point_position(curve.get_point_count() -1))
		if road.get_end_rotation().length() != 0:
			transform.basis = transform.basis.rotated(road.get_end_rotation().normalized(), road.get_end_rotation().length())
		item.set_global_transform(transform)


func _on_inventory_item_changed(road_identifier, player):
	if is_network_master():
		if item:
			item.free()
		item = roads_factory.get_road_instance(road_identifier)
		item.set_preview(true)
		if player != null:
			item.set_color(player.get_player_color())
		add_child(item)
		_on_route_road_added(gamestate.game.route.get_last_road())
