コード例 #1
0
    void Awake()
    {
        if (transform.parent != null && transform.parent.gameObject.layer == 5)
        {
            return;
        }                                                                                   // don't do this in the UI cuz it won't be visible

        ChildInputs = GetComponentsInChildren <CircuitInput>();

        if (GetComponentInChildren <Wire>())
        {
            return;
        }                                               // if it's on a cloned board it'll already have a wire

        InputInputConnection connection = Instantiate(References.Prefabs.Wire, transform).AddComponent <InputInputConnection>();

        connection.Input1 = ChildInputs[0];
        connection.Input2 = ChildInputs[1];

        connection.DrawWire();
        connection.unbreakable = true;
        StuffConnector.LinkConnection(connection);

        DestroyImmediate(connection.GetComponent <BoxCollider>());
    }
コード例 #2
0
    private static void ConnectionFinal()
    {
        if (CurrentWirePlacementIsValid())
        {
            StuffPlacer.RemoveOutlineFromObject(WireBeingPlaced);
            WireBeingPlaced.GetComponent <Wire>().SetPegsBasedOnPoints();
            StuffConnector.LinkConnection(WireBeingPlaced);
            StuffConnector.SetAppropriateConnectionParent(WireBeingPlaced);

            WireBeingPlaced.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;
            WireBeingPlaced.GetComponent <BoxCollider>().enabled      = true;

            SoundPlayer.PlaySoundAt(Sounds.ConnectionFinal, WireBeingPlaced);
            WireBeingPlaced = null;
        }
        else if (SelectedPeg != null)
        {
            SoundPlayer.PlaySoundAt(Sounds.FailDoSomething, SelectedPeg); // I'm using SelectedPeg instead of WireBeingPlaced here because I'm lazy; WireBeingPlaced might not exist
        }

        if (AutoHidePlacingGhostWhileConnecting)
        {
            ComponentPlacer.ShowPlacingGhost = PlacingGhostWasHiddenBeforeConnecting;
        }

        DoneConnecting();
    }
コード例 #3
0
    public SnappingPeg GetPegToSnapTo()
    {
        Vector3    origin = Wire.GetWireReference(gameObject).position;
        RaycastHit hit;

        if (Physics.Raycast(origin, -Wire.GetWireReference(gameObject).forward, out hit, 0.20f, Wire.IgnoreWiresLayermask)) // snapped connections will be about 18cm long; we cast for 20, just to be safe
        {
            if (hit.collider.tag == "Input")
            {
                SnappingPeg OtherSnappyPeg = hit.collider.GetComponent <SnappingPeg>();
                if (OtherSnappyPeg != null)
                {
                    if (StuffConnector.CanConnect(gameObject, OtherSnappyPeg.gameObject) && !WirePlacer.ConnectionExists(gameObject, OtherSnappyPeg.gameObject) &&
                        hit.transform.InverseTransformPoint(hit.point).z < -0.49f // make sure it hits the right face of the other peg
                        &&

                        // make sure it's rotated approximately correctly
                        // use the wire reference instead of the peg itself so the same code works for vertical and horizontal pegs
                        ((Wire.GetWireReference(hit).eulerAngles.y + 180 > Wire.GetWireReference(transform).eulerAngles.y - 2 &&
                          Wire.GetWireReference(hit).eulerAngles.y + 180 < Wire.GetWireReference(transform).eulerAngles.y + 2)

                         || (Wire.GetWireReference(hit).eulerAngles.y - 180 > Wire.GetWireReference(transform).eulerAngles.y - 2 &&
                             Wire.GetWireReference(hit).eulerAngles.y - 180 < Wire.GetWireReference(transform).eulerAngles.y + 2)))
                    {
                        return(OtherSnappyPeg);
                    }
                }
            }
        }

        return(null);
    }
コード例 #4
0
    public static void RecalculateClustersFromInputs(CircuitInput[] inputs)
    {
        HashSet <InputInputConnection>  IIConnections = new HashSet <InputInputConnection>();
        HashSet <InputOutputConnection> IOConnections = new HashSet <InputOutputConnection>();

        foreach (CircuitInput input in inputs) // the connections of the inputs of the cluster are the same as all the connections in the cluster
        {
            if (input.Cluster != null)
            {
                input.Cluster.RemoveInput(input);
            }                                                                // a lot of important stuff happens here!

            foreach (InputInputConnection connection in input.IIConnections)
            {
                connection.Renderer.enabled = true;

                IIConnections.Add(connection);
            }
            foreach (InputOutputConnection connection in input.IOConnections)
            {
                IOConnections.Add(connection);
            }
        }

        foreach (InputInputConnection connection in IIConnections)
        {
            StuffConnector.LinkInputs(connection); // LinkInputs is used because we already have the physical object, we just need the connection codeside
        }
        foreach (InputOutputConnection connection in IOConnections)
        {
            StuffConnector.LinkInputOutput(connection);
        }
    }
コード例 #5
0
ファイル: Editor.cs プロジェクト: pipe01/WireEdit
        protected GameObject CreateWire(Transform a, Transform b, bool ghost = true)
        {
            if (WirePlacer.ConnectionExists(a.parent.gameObject, b.parent.gameObject))
            {
                return(null);
            }

            GameObject obj = GameObject.Instantiate(Prefabs.Wire);
            Wire       w;

            if (a.parent.tag == "Input" && b.parent.tag == "Input")
            {
                w        = obj.AddComponent <InputInputConnection>();
                w.Point1 = a;
                w.Point2 = b;
            }
            else
            {
                w = obj.AddComponent <InputOutputConnection>();

                if (a.parent.tag == "Input")
                {
                    w.Point1 = a;
                    w.Point2 = b;
                }
                else
                {
                    w.Point1 = b;
                    w.Point2 = a;
                }
            }

            if (!WirePlacer.CanConnect(w))
            {
                GameObject.Destroy(obj);
                return(null);
            }

            w.DrawWire();

            if (ghost)
            {
                obj.GetComponent <BoxCollider>().enabled = false;
                StuffPlacer.OutlineObject(obj, OutlineColor.blue);
            }
            else
            {
                w.SetPegsBasedOnPoints();
                StuffConnector.LinkConnection(w);
                StuffConnector.SetAppropriateConnectionParent(w);
                obj.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;
                obj.GetComponent <BoxCollider>().enabled      = true;
            }

            return(obj);
        }
コード例 #6
0
    public void Initialize()
    {
        transform.parent = StuffConnector.AppropriateConnectionParent(this);

        MegaMeshComponent MMC = gameObject.AddComponent <MegaMeshComponent>();

        MMC.MaterialType = MaterialType.SnappingPeg;
        MegaMeshManager.AddComponent(MMC);

        SetThisAsSnappedConnectionOfPegs();
    }
コード例 #7
0
    // Recalculate combined mesh
    // mesh is of all the inputs and all the IIConnections in the cluster, since that's all that can be guaranteed to change color together
    public void RecalculateCombinedMesh()
    {
        MegaMeshManager.RemoveComponentImmediately(MegaMeshComponent);

        List <MeshFilter> MeshFilters = new List <MeshFilter>();

        foreach (CircuitInput input in ConnectedInputs)
        {
            if (!input.IsSnappingPeg)
            {
                MeshFilters.Add(input.MeshFilter);
                input.Renderer.enabled = false;

                foreach (InputInputConnection IIConnection in input.IIConnections)
                {
                    if (!MeshFilters.Contains(IIConnection.MeshFilter))
                    {
                        MeshFilters.Add(IIConnection.MeshFilter);
                    }                                                                                                 // check so we don't get duplicates
                    IIConnection.Renderer.enabled = false;
                }
            }
            else
            {
                foreach (InputInputConnection MaybeNotSnappy in input.IIConnections)
                {
                    if (MaybeNotSnappy.GetType() != typeof(SnappedConnection) && !MeshFilters.Contains(MaybeNotSnappy.MeshFilter))
                    {
                        MeshFilters.Add(MaybeNotSnappy.MeshFilter);
                        MaybeNotSnappy.Renderer.enabled = false;
                    }
                }
            }
        }

        transform.parent = null;
        transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);

        MeshFilter.mesh = new Mesh();
        MeshFilter.mesh.CombineMeshes(MegaMeshManager.GetScaledCombineInstances(MeshFilters, 1.002f)); // the minimum viable value for extremely large clusters
        transform.parent = StuffConnector.ProperClusterParent(this);

        VisualsChanged();
        QueueVisualUpdate();

        MegaMeshComponent.Mesh = new Mesh();
        MegaMeshComponent.Mesh.CombineMeshes(MegaMeshManager.GetCombineInstances(MeshFilters));

        MeshRecalculationQueued = false;
    }
コード例 #8
0
ファイル: Editor.cs プロジェクト: pipe01/WireEdit
        protected virtual void ApplyInner()
        {
            foreach (var wire in Created)
            {
                StuffPlacer.RemoveOutlineFromObject(wire, false);
                wire.GetComponent <Wire>().SetPegsBasedOnPoints();
                StuffConnector.LinkConnection(wire);
                StuffConnector.SetAppropriateConnectionParent(wire);
                wire.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;
                wire.GetComponent <BoxCollider>().enabled      = true;
            }

            SoundPlayer.PlaySoundGlobal(Sounds.ConnectionFinal);
        }
コード例 #9
0
    // finds all connections connected to an input or output which is a child of BoardBeingPlaced. If that connection does not have BoardBeingPlaced above it in the heirarchy, destroy it
    public static void DestroyAllWiresConnectedToBoardButNotPartOfIt(GameObject board)
    {
        CircuitInput[]  Inputs  = board.GetComponentsInChildren <CircuitInput>();
        CircuitOutput[] Outputs = board.GetComponentsInChildren <CircuitOutput>();

        // annoying InvalidOperationException protection
        List <InputInputConnection>  IIConnectionsToDestroy = new List <InputInputConnection>();
        List <InputOutputConnection> IOConnectionsToDestroy = new List <InputOutputConnection>();

        foreach (CircuitInput input in Inputs)
        {
            foreach (InputInputConnection connection in input.IIConnections)
            {
                if (!StuffConnector.IsChildOf(connection.transform, board.transform))
                {
                    IIConnectionsToDestroy.Add(connection);
                }
            }

            foreach (InputOutputConnection connection in input.IOConnections)
            {
                if (!StuffConnector.IsChildOf(connection.transform, board.transform))
                {
                    IOConnectionsToDestroy.Add(connection);
                }
            }
        }

        foreach (CircuitOutput output in Outputs)
        {
            foreach (InputOutputConnection connection in output.GetIOConnections())
            {
                if (!StuffConnector.IsChildOf(connection.transform, board.transform))
                {
                    IOConnectionsToDestroy.Add(connection);
                }
            }
        }

        foreach (InputInputConnection connection in IIConnectionsToDestroy)
        {
            StuffDeleter.DestroyIIConnection(connection);
        }
        foreach (InputOutputConnection connection in IOConnectionsToDestroy)
        {
            StuffDeleter.DestroyIOConnection(connection);
        }
    }
コード例 #10
0
        public override void Do()
        {
            var netObj1 = NetObject.GetByNetId(Packet.NetObj1Id);
            var netObj2 = NetObject.GetByNetId(Packet.NetObj2Id);

            if (netObj1 == null || netObj2 == null)
            {
                return;
            }

            var io1 = netObj1.IO[Packet.Point1Id];
            var io2 = netObj2.IO[Packet.Point2Id];

            if (io1 == null || io2 == null)
            {
                return;
            }

            var  wireObj = GameObject.Instantiate(Prefabs.Wire);
            Wire wire;

            if (io1.tag == "Input" && io2.tag == "Input")
            {
                wire = wireObj.AddComponent <InputInputConnection>();
            }
            else
            {
                wire = wireObj.AddComponent <InputOutputConnection>();
            }

            wire.Point1 = Wire.GetWireReference(io1);
            wire.Point2 = Wire.GetWireReference(io2);

            wire.DrawWire();

            wire.SetPegsBasedOnPoints();
            StuffConnector.LinkConnection(wire);
            StuffConnector.SetAppropriateConnectionParent(wire);

            SoundPlayer.PlaySoundAt(Sounds.ConnectionFinal, wireObj);

            wireObj.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;
            wireObj.AddComponent <NetObject>().NetID          = Packet.NetID;
            wireObj.GetComponent <Collider>().enabled         = true;
        }
コード例 #11
0
    public static void RecalculateAllClustersEverywhere()
    {
        // we used to destroy all existing clusters, but I took this out to make through pegs work in 0.2. I sure hope this wasn't necessary!
        //WireCluster[] existingclusters = UnityEngine.Object.FindObjectsOfType<WireCluster>();
        //foreach (WireCluster oldcluster in existingclusters) { UnityEngine.Object.Destroy(oldcluster.gameObject); }

        Wire[] wires = UnityEngine.Object.FindObjectsOfType <Wire>();

        foreach (Wire wire in wires)
        {
            wire.FindPoints();
            StuffConnector.LinkConnection(wire);
        }

        SnappingPeg.SnapEverywhere();
        MegaMeshManager.AddComponentsEverywhere();

        // and FINALLY, allow circuitry updates again
        BehaviorManager.AllowedToUpdate = true;
    }
コード例 #12
0
ファイル: Mover.cs プロジェクト: pipe01/WireEdit
        public static void EndMove(GameObject newObj)
        {
            var inputs  = newObj.GetComponentsInChildren <CircuitInput>();
            var outputs = newObj.GetComponentsInChildren <CircuitOutput>();

            foreach (var item in Connections)
            {
                GameObject obj = GameObject.Instantiate(Prefabs.Wire);

                var wire = item.InputInput ? (Wire)obj.AddComponent <InputInputConnection>() : obj.AddComponent <InputOutputConnection>();

                var a = (item.OutputIsMoved ? outputs[item.Index].transform : inputs[item.Index].transform).Find("WireReference");
                var b = item.Point;

                if (a.parent.tag == "Input")
                {
                    wire.Point1 = a;
                    wire.Point2 = b;
                }
                else
                {
                    wire.Point1 = b;
                    wire.Point2 = a;
                }

                if (!WirePlacer.CanConnect(wire))
                {
                    GameObject.Destroy(obj);
                    continue;
                }

                wire.DrawWire();
                wire.SetPegsBasedOnPoints();
                StuffConnector.LinkConnection(wire);
                StuffConnector.SetAppropriateConnectionParent(wire);
                obj.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;
                obj.GetComponent <BoxCollider>().enabled      = true;
            }

            IsMoving = false;
        }
コード例 #13
0
    public void TryToConnect()
    {
        DestroySnappedConnection();
        SnappingPeg OtherSnappyPeg = GetPegToSnapTo();                         // I quite like the prefix "snappy"

        if (OtherSnappyPeg != null && OtherSnappyPeg.GetPegToSnapTo() == this) // make sure the snapping can occur both ways
        {
            SnappedConnection SnappyConnection = Instantiate(References.Prefabs.Wire).AddComponent <SnappedConnection>();
            SnappyConnection.Input1 = this;
            SnappyConnection.Input2 = OtherSnappyPeg;
            SnappyConnection.DrawWire();
            SnappyConnection.Initialize();

            StuffConnector.LinkInputs(SnappyConnection);

            if (BehaviorManager.AllowedToUpdate)
            {
                SoundPlayer.PlaySoundAt(References.Sounds.ConnectionFinal, transform);
            }                                                                                                               // the check is so it doesn't play on loaded pegs
        }
    }
コード例 #14
0
        protected override void CircuitLogicUpdate()
        {
            var ChildInputs = GetComponentsInChildren <CircuitInput>();

            if (!this.Inputs[2].On && IIC != null)
            {
                StuffDeleter.DestroyIIConnection(IIC);
                IIC = null;
            }

            if (GetComponentsInChildren <Wire>().Length < 1 && this.Inputs[2].On)
            {
                InputInputConnection inputInputConnection = Instantiate(Prefabs.Wire, base.transform).AddComponent <InputInputConnection>();
                inputInputConnection.Input1 = ChildInputs[0];
                inputInputConnection.Input2 = ChildInputs[1];
                inputInputConnection.DrawWire();
                inputInputConnection.unbreakable = true;
                StuffConnector.LinkConnection(inputInputConnection);
                IIC = inputInputConnection;
            }
        }
コード例 #15
0
        public override void Undo()
        {
            var  wireObj = GameObject.Instantiate(Prefabs.Wire);
            Wire wire;

            if (Connection.Way == Connection.Ways.InputInput)
            {
                wire = wireObj.AddComponent <InputInputConnection>();
            }
            else
            {
                wire = wireObj.AddComponent <InputOutputConnection>();
            }

            Connection.ObjectA = Connection.ObjectA ?? GetWireReferenceFromPoint(Connection.PositionA);
            Connection.ObjectB = Connection.ObjectB ?? GetWireReferenceFromPoint(Connection.PositionB);

            if (Connection.ObjectA == null || Connection.ObjectB == null)
            {
                SoundPlayer.PlaySoundAt(Sounds.FailDoSomething, wireObj);
                return;
            }

            wire.Point1 = Connection.ObjectA.transform;
            wire.Point2 = Connection.ObjectB.transform;

            wire.SetPegsBasedOnPoints();
            wire.DrawWire();

            StuffConnector.LinkConnection(wire);
            StuffConnector.SetAppropriateConnectionParent(wire);

            wireObj.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;

            SoundPlayer.PlaySoundAt(Sounds.ConnectionFinal, wireObj);
        }
コード例 #16
0
    public static void RotateThing(GameObject RotateThis)
    {
        // determines which direction to rotate
        // TODO: use camera.main.transform to rotate stuff left and right rather than clockwise & ccw

        if (RotateThis.tag == "World")
        {
            SoundPlayer.PlaySoundGlobal(Sounds.FailDoSomething);
            return;
        }

        if (RotateThis.tag == "CircuitBoard" || RotateThis.tag == "PlaceOnlyCircuitBoard")
        {
            SoundPlayer.PlaySoundGlobal(Sounds.FailDoSomething);
            return;
        }

        // rotate the full object, not part of it
        // the last check is so that rotating child boards doesn't rotate their parent
        RotateThis = ComponentPlacer.FullComponent(RotateThis);

        Quaternion BeforeRotation     = Quaternion.identity;
        Vector3    AxisToRotateAround = RotateThis.transform.up;

        // everything but wires should rotate around transform.up
        if (RotateThis.tag == "Wire")
        {
            if (RotateThis.GetComponent <SnappedConnection>()) // you cannot rotate snapped connections
            {
                SoundPlayer.PlaySoundGlobal(Sounds.FailDoSomething);
                return;
            }

            AxisToRotateAround = RotateThis.transform.forward;
            StuffConnector.QueueWireMeshRecalculation(RotateThis);

            SoundPlayer.PlaySoundAt(Sounds.RotateSomething, RotateThis);
        }
        else
        {
            BeforeRotation = RotateThis.transform.rotation; // non-wires might be rotated into invalid positions, in which case they should be reverted to their original rotations
        }

        int direction = 1;

        if (Input.GetAxis("Rotate") < 0)
        {
            direction = -1;
        }
        else
        {
            direction = 1;
        }

        if (Input.GetButton("Mod")) // rotate by 22.5 degrees if the mod key is held down
        {
            RotateThis.transform.RotateAround(RotateThis.transform.position, AxisToRotateAround, direction * 22.5f);
        }
        else // ...but normally rotate by 90 degrees
        {
            RotateThis.transform.RotateAround(RotateThis.transform.position, AxisToRotateAround, direction * 90f);
        }

        // the validity of wire placement is never affected when rotating them, but if it's an object, check if it can actually be placed there. If not, revert to original rotation
        if (RotateThis.tag != "Wire")
        {
            BoxCollider[] colliders = RotateThis.GetComponentsInChildren <BoxCollider>();
            StuffPlacer.SetStateOfBoxColliders(colliders, false);

            if (StuffPlacer.GameObjectIntersectingStuffOrWouldDestroyWires(RotateThis, true, true)) // ignore wires
            {
                RotateThis.transform.rotation = BeforeRotation;
                StuffPlacer.SetStateOfBoxColliders(colliders, true);

                SoundPlayer.PlaySoundGlobal(Sounds.FailDoSomething);
                return;
            }

            SoundPlayer.PlaySoundAt(Sounds.RotateSomething, RotateThis);
            StuffPlacer.SetStateOfBoxColliders(colliders, true);
        }

        FloatingPointRounder.RoundIn(RotateThis);

        RedrawCircuitGeometryOf(RotateThis);
        DestroyIntersectingConnections(RotateThis);

        SnappingPeg.TryToSnapIn(RotateThis);

        MegaMeshManager.RecalculateGroupsOf(RotateThis);
    }