public void FreePort(RoadConnectorSide side_from, RoadConnectorSide side_to)
 {
     foreach (var item in connectedPorts.Where(tf => tf.Key == side_from.GetPath() && tf.Value == side_to.GetPath()))
     {
         connectedPorts.Remove(item);
     }
 }
 public void ClosePort(RoadConnectorSide side_from, RoadConnectorSide side_to)
 {
     if (!connectedPorts.ContainsKey(side_from.GetPath()))
     {
         connectedPorts.Add(side_from.GetPath(), side_to.GetPath());
     }
 }
    public List <RoadConnectorSide> findFreeSides(RoadConnectorSide currentSideInUse = null)
    {
        List <RoadConnectorSide> sides = new List <RoadConnectorSide>();

        foreach (var side in GetSides())
        {
            if (connectedPorts == null)
            {
                sides.Add(side);
            }
            else
            {
                var amount = connectedPorts.Count(tf => tf.Value == side.GetPath());

                if (amount < 2)
                {
                    sides.Add(side);
                }
            }
        }
        return(sides);
    }
 public RoadConnectorSide findFreeSide(RoadConnectorSide side = null)
 {
     return(findFreeSides(side).FirstOrDefault());
 }
    public void addNode(RoadConnector connector, int degress = 0, RoadConnectorSide targetSideToBeUse = null)
    {
        var sourceConnector = getLastConnector();
        var targetConnector = createConnector(connector);

        targetConnector.modelName = connector.Name;

        var targetSide = targetConnector.findFreeSide();

        if (targetSideToBeUse != null)
        {
            targetSide = (RoadConnectorSide)targetConnector.FindNode(targetSideToBeUse.Name);
        }
        GD.Print("side: " + targetSide);

        targetConnector.currentSide = targetSide.GetPath();

        var target_top_left     = targetSide.portTopLeft;
        var target_top_right    = targetSide.portTopRight;
        var target_bottom_left  = targetSide.portBottomLeft;
        var target_bottom_right = targetSide.portBottomRight;

        //check radius and set ip up
        var target_radius = calclateRadius(targetConnector, target_top_left, target_top_right, target_bottom_left, target_bottom_right);

        GD.Print("target radius:" + target_radius);

        if (degress > 0)
        {
            GD.Print("swapped or over 90");

            //swap ports
            var temp_top_left     = targetSide.portTopLeft;
            var temp_top_right    = targetSide.portTopRight;
            var temp_bottom_left  = targetSide.portBottomLeft;
            var temp_bottom_right = targetSide.portBottomRight;

            target_top_left     = temp_bottom_left;
            target_top_right    = temp_bottom_right;
            target_bottom_left  = temp_top_left;
            target_bottom_right = temp_top_right;


            targetConnector.RotationDegrees  = new Vector3(0, -180, 0);
            targetConnector.RotationDegrees += new Vector3(0, target_radius, 0);

            targetConnector.Swappded = true;
        }

        if (sourceConnector != null)
        {
            var sourceSide = sourceConnector.GetCurrentSide();

            // var sourceSide = sourceConnector.getLastSide();
            // var sourceSideLast = sourceConnector.getFirstSide();

            var source_top_left     = sourceSide.portTopLeft;
            var source_top_right    = sourceSide.portTopRight;
            var source_bottom_left  = sourceSide.portBottomLeft;
            var source_bottom_right = sourceSide.portBottomRight;

            var source_radius = calclateRadius(sourceConnector, source_top_left, source_top_right, source_bottom_left, source_bottom_right);

            Vector3 xf1_global = Vector3.Zero;
            Vector3 xf2_global = Vector3.Zero;

            if (sourceConnector.Swappded)
            {
                GD.Print("swapped");

                var temp_top_left     = sourceSide.portTopLeft;
                var temp_top_right    = sourceSide.portTopRight;
                var temp_bottom_left  = sourceSide.portBottomLeft;
                var temp_bottom_right = sourceSide.portBottomRight;

                source_top_left     = temp_bottom_left;
                source_top_right    = temp_bottom_right;
                source_bottom_left  = temp_top_left;
                source_bottom_right = temp_top_right;

                //one of this hav eto be disabled
                targetConnector.RotationDegrees += sourceConnector.RotationDegrees;
                targetConnector.RotationDegrees -= new Vector3(0, -180, 0);
                //targetConnector.RotationDegrees += new Vector3(0, source_radius * -1, 0);
            }
            else
            {
                targetConnector.RotationDegrees += sourceConnector.RotationDegrees;
                targetConnector.RotationDegrees += new Vector3(0, source_radius * -1, 0);
            }

            var ports_front_0 = findClosestMesh(targetConnector, target_bottom_left);
            var ports_front_1 = findClosestMesh(targetConnector, target_bottom_right);

            var source_ports_0 = findClosestMesh(sourceConnector, source_top_left);
            var source_ports_1 = findClosestMesh(sourceConnector, source_top_right);

            var v1 = ports_front_1.vec;
            var v2 = ports_front_0.vec;

            var v3 = source_ports_1.vec;
            var v4 = source_ports_0.vec;

            v1.y = 0;
            v2.y = 0;
            v3.y = 0;
            v4.y = 0;

            xf1_global = v1.LinearInterpolate(v2, 0.5f);
            xf2_global = v3.LinearInterpolate(v4, 0.5f);

            var orig_port_front_left = xf2_global - xf1_global;
            //   targetConnector.TranslateObjectLocal(orig_port_front_left);
            var gt2 = targetConnector.Transform;
            gt2.origin = orig_port_front_left;
            targetConnector.Transform = gt2;

            targetConnector.ClosePort(sourceSide, targetSide);
            sourceConnector.ClosePort(targetSide, sourceSide);
        }

        addButtons();
    }