示例#1
0
        public CableSegment(Cable parent, int index)
        {
            Parent = parent;
            Index  = index;

            hitbox = new LineHitbox(this);

            Add();
        }
示例#2
0
        public static ContextGate DeserializeTopLayer(SerializedGate storageObject, List <Cable> cables)
        {
            if (storageObject.Type != "ContextGate")
            {
                throw new Exception("Object does not store an ContextGate");
            }
            ContextGate contextGate = new ContextGate();

            contextGate.Name = storageObject.Name;
            contextGate.Tag  = storageObject.Tag;
            contextGate.Size = storageObject.Size;
            var idToNode      = new Dictionary <int, ConnectionNode>();
            var cableEpToNode = new Dictionary <int, ConnectionNode>();
            var uncabledNodes = new LinkedList <ConnectionNode>();

            foreach (SerializedGate innerStore in storageObject.Context)
            {
                Gate innerGate = Deserialize(innerStore);
                if (!(innerGate is ContextGate))
                {
                    if (innerStore.InputLabels != null)
                    {
                        for (int i = 0; i < innerGate.Input.Count; i++)
                        {
                            innerGate.Input[i].Name = innerStore.InputLabels[i];
                        }
                    }
                    if (innerStore.OutputLabels != null)
                    {
                        for (int i = 0; i < innerGate.Output.Count; i++)
                        {
                            innerGate.Output[i].Name = innerStore.OutputLabels[i];
                        }
                    }
                }
                contextGate.Context.Add(innerGate);
                for (int i = 0; i < innerStore.OutputConnections.Count(); i++)
                {
                    int id = innerStore.OutputConnections[i];
                    if (id != 0)
                    {
                        idToNode[id] = innerGate.Output[i];
                    }
                }
            }

            for (int i = 0; i < storageObject.Context.Count; i++)
            {
                SerializedGate innerStore = storageObject.Context[i];
                Gate           innerGate  = contextGate.Context[i];
                for (int j = 0; j < innerStore.InputConnections.Count(); j++)
                {
                    int id = innerStore.InputConnections[j];
                    if (id != 0)
                    {
                        if (!idToNode.ContainsKey(id))
                        {
                            throw new InvalidOperationException("Invalid connection");
                        }
                        ConnectionNode thisNode  = innerGate.Input[j];
                        ConnectionNode otherNode = idToNode[id];
                        otherNode.NextConnectedTo.Add(thisNode);
                        thisNode.BackConnectedTo = otherNode;
                        otherNode.IsEmpty        = false;
                        thisNode.IsEmpty         = false;
                        thisNode.State           = thisNode.IsInverted ? !otherNode.State : otherNode.State;
                        if (innerStore.CableEndPoints != null)
                        {
                            int endpoint = innerStore.CableEndPoints[j];
                            cableEpToNode[endpoint] = thisNode;
                        }
                        uncabledNodes.AddLast(thisNode);
                    }
                }
            }

            if (storageObject.Cables == null)
            {
                return(contextGate);
            }

            foreach (SerializedGate.Cable cablestore in storageObject.Cables)
            {
                if (!idToNode.ContainsKey(cablestore.OutputConnection))
                {
                    continue;
                }
                ConnectionNode outputNode = idToNode[cablestore.OutputConnection];
                ConnectionNode inputNode  = cableEpToNode[cablestore.EndPoint];
                Cable          cable      = new Cable(outputNode);
                foreach (Point point in cablestore.Points)
                {
                    cable.AddSegment(point);
                }
                cable.ConnectTo(inputNode, false);
                cables.Add(cable);
                uncabledNodes.Remove(inputNode);
            }

            foreach (ConnectionNode node in uncabledNodes)
            {
                Cable cable = new Cable(node.BackConnectedTo);
                cable.ConnectTo(node, false);
                cables.Add(cable);
            }

            return(contextGate);
        }