Пример #1
0
    // recalculates the clusters for each input and output in the cluster, based on all the connections in the cluster
    public static void RecalculateCluster(WireCluster cluster)
    {
        if (cluster == null)
        {
            return;
        }

        RecalculateClustersFromInputs(cluster.GetConnectedInputs());

        Object.Destroy(cluster.gameObject);
    }
    // merge two clusters
    public static void JoinClusters(WireCluster cluster1, WireCluster cluster2)
    {
        // stick inputs & outputs of cluster2 into cluster1
        foreach (CircuitInput ConnectedInput in cluster2.GetConnectedInputs())
        {
            cluster1.ConnectInput(ConnectedInput);
        }

        foreach (CircuitOutput ConnectedOutput in cluster2.GetConnectedOutputs())
        {
            cluster1.ConnectOutput(ConnectedOutput);
        }

        Object.Destroy(cluster2.gameObject);
    }
    // todo: rethink this...
    public static Transform ProperClusterParent(WireCluster cluster)
    {
        int       ShallowestDepthInHeirarchy = 1000000000;
        Transform ProperParent = cluster.transform;

        foreach (CircuitInput input in cluster.GetConnectedInputs())
        {
            int ThisDepthInHeirarchy = DepthInHeirarchy(input.transform);
            if (ShallowestDepthInHeirarchy > ThisDepthInHeirarchy)
            {
                ProperParent = input.transform.parent;
                ShallowestDepthInHeirarchy = ThisDepthInHeirarchy;
            }
        }

        return(ProperParent);
    }
    // Triggers WireCluster.ConnectInput in the appropriate cases for a connection. Covers possible cases.
    public static void LinkInputs(InputInputConnection connection)
    {
        CircuitInput input1 = connection.Input1;
        CircuitInput input2 = connection.Input2;

        if (input1 == null || input2 == null)
        {
            Debug.LogError("This wire doesn't have its inputs set!"); return;
        }

        if (input1.Cluster == null && input2.Cluster == null) // if neither input has a cluster, create a new one and add them to it
        {
            WireCluster NewCluster = Object.Instantiate(Prefabs.Cluster).GetComponent <WireCluster>();
            NewCluster.ConnectInput(input1);
            NewCluster.ConnectInput(input2);

            NewCluster.transform.parent = ProperClusterParent(NewCluster); // this is also done the next frame when the cluster recalculates its mesh. The reason it is being done here is because when a board is cloned, it gets new clusters, and those need to be children of the board during the same frame they start existing so that autocombineonstable can be set to false for them by StuffPlacer.NewThingBeingPlaced. My hope is that it doesn't affect performance, but I'm pretty nervous about it...
        }

        else if (input1.Cluster == input2.Cluster) // if they're both part of the same cluster already, mesh recalculation needs to happen to incorporate the new wire
        {
            input1.Cluster.QueueMeshRecalculation();
        }

        else if (input1.Cluster != null && input2.Cluster != null && input1.Cluster != input2.Cluster) // if both inputs are part of a cluster, merge those clusters
        {
            JoinClusters(input1.Cluster, input2.Cluster);
        }

        else if (input1.Cluster != null && input2.Cluster == null) // if one input is part of a cluster, connect the other to that cluster
        {
            input1.Cluster.ConnectInput(input2);
        }

        else if (input1.Cluster == null && input2.Cluster != null) // ditto
        {
            input2.Cluster.ConnectInput(input1);
        }

        else
        {
            Debug.Log("no input connection case found");
        }
    }
    // joins an output with an input by sticking the output in the cluster of the input
    public static void LinkInputOutput(InputOutputConnection connection)
    {
        CircuitInput  input  = connection.Input;
        CircuitOutput output = connection.Output;

        if (input == null || output == null)
        {
            Debug.LogError("this wire didn't have its input/output set!"); return;
        }

        if (input.Cluster != null)
        {
            input.Cluster.ConnectOutput(output);
        }

        else
        {
            WireCluster NewCluster = Object.Instantiate(Prefabs.Cluster).GetComponent <WireCluster>();
            NewCluster.ConnectInput(input);
            NewCluster.ConnectOutput(output);
            NewCluster.transform.parent = ProperClusterParent(NewCluster); // this is also done the next frame when the cluster recalculates its mesh. The reason it is being done here is because when a board is cloned, it gets new clusters, and those need to be children of the board during the same frame they start existing so that autocombineonstable can be set to false for them by StuffPlacer.NewThingBeingPlaced. My hope is that it doesn't affect performance, but I'm pretty nervous about it...
        }
    }