Exemple #1
0
    // called from snap socket
    public void SetSocket(SnapSocket socket)
    {
        // Debug.Log(string.Format("Connecting plug {0} to socket {1}", this, socket));

        this.InSocket = socket;
        PlugginAction();
    }
Exemple #2
0
    private void OnTriggerEnter(Collider other)
    {
        SnapSocket socket = other.gameObject.GetComponent <SnapSocket>();

        if (socket == null)
        {
            return;
        }

        nearSocket = socket;
    }
    private bool IsInConversation(SnapSocket socket)
    {
        foreach (var m in missions)
        {
            // a person calling, cant call again
            if (m.conversation.FromCaller == socket)
            {
                return(true);
            }

            if (m.conversation.ToCaller == socket &&
                m.IsInConversation)
            {
                return(true);
            }
        }

        return(false);
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 9)
        {
            return;                              //terrain
        }
        //Debug.Log("On collision, got " + snapPiece.CountUnconnectedSockets() + " unconnected Sockets");

        Ray ray = new Ray(transform.position, other.transform.position);

        Debug.DrawLine(transform.position, other.transform.position);
        List <RaycastHit> hits;
        SnapPiece         first = snapPiece.FirstSnapPieceAlongRayWithAvailableSockets(ray, out hits);

        if (hits.Count > 0)
        {
            SnapSocket eSocket = first.GetFirstSocketNamed("E Socket");

            SnapSocket wSocket = snapPiece.GetFirstSocketNamed("W Socket");

            eSocket.SnapTo(wSocket);



            /*
             * Debug.Log(gameObject.name + " hit something");
             * Debug.Log(first + " was the first snap piece along the collision ray");
             * Debug.Log(hits.Count + " hits total");
             * foreach(RaycastHit hit in hits)
             * {
             *  Debug.Log(hit.collider.gameObject.name + " was hit");
             * }
             */
        }

        //snapPiece.GetAllUnconnectedSockets();
    }
        void Update()
        {
            Vector3 mWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * _currentDistanceToPositionAt);

            if (_currentPiece != null)
            {
                /** Decide the new position to place in world, from which we'll calculate a POTENTIAL snap position */
                switch (_snapMode)
                {
                case PointerSnapMode.ABSOLUTE_PROJECTION:
                    _currentPiece.transform.position = mWorld;
                    break;

                case PointerSnapMode.RELATIVE_TO_LAST_SNAP:
                    /** See how far the mouse has moved since the snap position */
                    Ray     currentMouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
                    Vector3 currentMousePointCloseToSocket = currentMouseRay.GetPoint(_snapModeOrignalJoinDistanceFromCamera);

                    Vector3 mouseOffsetCloseToSocket = currentMousePointCloseToSocket - _snapModeOriginalMousePointNearSocket;

                    _currentPiece.transform.position = _snapModeOriginalPosition + mouseOffsetCloseToSocket;
                    break;
                }



                /** Now that we've repositioned in world, calculate the rays for each socket.
                 *
                 * On next frame-update, this will be used to try and match a socket to a nearby other-piece
                 */
                if (_lastRayForEachSocket == null)
                {
                    _lastRayForEachSocket = new Dictionary <SnapSocket, Ray>();
                }

                foreach (SnapSocket localSocket in _currentPiece.GetAllUnconnectedSockets())
                {
                    _lastRayForEachSocket[localSocket] = new Ray(Camera.main.transform.position, localSocket.transform.position - Camera.main.transform.position);
                }


                /***************************************************************//***************************************************************/
                // MAJOR bug in the Mono compiler that Unity is shipping: these variabels ARE USED below!
                                                        #pragma warning disable 219
                Vector3    nearestRoomHitPoint         = Vector3.zero;
                float      nearestSocketSocketDistance = 999999f;            // MUST calculate this in screen space, not world space!
                SnapPiece  nearestRoom              = null;
                SnapSocket nearestSocket            = null;
                SnapSocket localSocketForConnection = null;
                                                        #pragma warning restore 219

                /**
                 * 1. Check each unconnected socket
                 * 2. For each, find the piece it is nearest to
                 * 3. On that piece, for ALL hits on the ray (front and back) find the unconnected socket that is nearest to the hit
                 * 4. Stick with whichever socket was nearest
                 */
                foreach (SnapSocket localSocket in _currentPiece.GetAllUnconnectedSockets())
                {
                    Ray rayForSocket = _lastRayForEachSocket[localSocket];

                    List <RaycastHit> hitsOnSnapPiece;
                    SnapPiece         hitRoom = _currentPiece.FirstSnapPieceAlongRayWithAvailableSockets(rayForSocket, out hitsOnSnapPiece, localSocket);

                    if (hitRoom != null)
                    {
                        foreach (RaycastHit hit in hitsOnSnapPiece)
                        {
                            SnapSocket hitSocket = hitRoom.NearestUnconnectedSocketScreenSpace(Camera.main.WorldToScreenPoint(hit.point), Camera.main, 0f, localSocket, SnapAndPlugScene.sceneData.showDebugMessages);

                            if (hitSocket != null)
                            {
                                Vector3 screenForHitSocket   = Camera.main.WorldToScreenPoint(hitSocket.attachPoint);
                                Vector3 screenForLocalSocket = Camera.main.WorldToScreenPoint(localSocket.attachPoint);

                                float socketDistance = (screenForHitSocket - screenForLocalSocket).magnitude;
                                if (socketDistance < nearestSocketSocketDistance)
                                {
                                    nearestSocketSocketDistance = socketDistance;
                                    nearestRoom         = hitRoom;
                                    nearestSocket       = hitSocket;
                                    nearestRoomHitPoint = hit.point;

                                    localSocketForConnection = localSocket;
                                }

                                if (SnapAndPlugScene.sceneData.showDebugMessages)
                                {
                                    Debug.Log("Found " + hitsOnSnapPiece.Count + " hits on target piece; this one has nearest socket: " + hitSocket + " near hit: " + hit.point + " (" + socketDistance + " away)");
                                }
                            }
                            else
                            if (SnapAndPlugScene.sceneData.showDebugMessages)
                            {
                                Debug.Log("HIT on the room, but no unconnected sockets within range of point: " + hit.point);
                            }
                        }
                    }
                }

                /********* DEBUG HELPER * /
                 * if( nearestRoom != null )
                 * {
                 *      foreach( SnapSocket s in _currentPiece.GetAllUnconnectedSockets() )
                 *      {
                 *              foreach( SnapSocket t in nearestRoom.GetAllUnconnectedSockets() )
                 *              {
                 *                      Color c = Color.gray;
                 *
                 *                      if( s.Equals( localSocketForConnection ) )
                 *                              c = Color.yellow;
                 *
                 *                      if( s.Equals( localSocketForConnection ) && t.Equals( nearestSocket ) )
                 *                              c = Color.green;
                 *
                 *                      Debug.DrawLine( s.transform.position, t.transform.position, c );
                 *              }
                 *      }
                 * }*/

                switch (_snapMode)
                {
                case PointerSnapMode.ABSOLUTE_PROJECTION:
                    if (nearestSocket != null)
                    {
                        _snapModeOriginalJoinPosition          = nearestSocket.transform.position;
                        _snapModeOriginalPosition              = _currentPiece.transform.position;
                        _snapModeOrignalJoinDistanceFromCamera = Vector3.Distance(Camera.main.transform.position, _snapModeOriginalJoinPosition);
                        _snapModeOriginalMousePointNearSocket  = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(_snapModeOrignalJoinDistanceFromCamera);

                        _snapMode = PointerSnapMode.RELATIVE_TO_LAST_SNAP;
                        //Debug.Log("Switching to RELATIVE SNAP positioning mode" );
                    }
                    break;

                case PointerSnapMode.RELATIVE_TO_LAST_SNAP:
                    if (nearestSocket == null)
                    {
                        // Change the default projected distance for absolute mode so it matches the new local depth from last round of snapping
                        _currentDistanceToPositionAt = _snapModeOrignalJoinDistanceFromCamera;

                        _snapMode = PointerSnapMode.ABSOLUTE_PROJECTION;
                        //Debug.Log("Switching to ABSOLUTE snap positioning mode" );
                    }
                    break;
                }


                if (nearestSocket != null)
                {
                    /*if( ! nearestSocket.Equals( _debug_currentSnapSocket ) )
                     *      Debug.Log("-- changing to new snap socket (old: "+_debug_currentSnapSocket+"), new = "+nearestSocket );*/

                    /////////////////
                    localSocketForConnection.SnapTo(nearestSocket, false);

                    foreach (SnapGhostable ghostable in _currentPiece.GetComponentsInChildren <SnapGhostable>())
                    {
                        ghostable.SetSnapped(true);
                    }

                    if (!nearestSocket.Equals(_snapModeLastSnappedToSocket))
                    {
                        _snapModeOriginalJoinPosition          = nearestSocket.transform.position;
                        _snapModeOriginalPosition              = _currentPiece.transform.position;
                        _snapModeOrignalJoinDistanceFromCamera = Vector3.Distance(Camera.main.transform.position, _snapModeOriginalJoinPosition);
                        _snapModeOriginalMousePointNearSocket  = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(_snapModeOrignalJoinDistanceFromCamera);
                    }
                    _snapModeLastSnappedToSocket = nearestSocket;

                    /** Allow user to change the chosen socket, moving the piece to FORCE it into being "nearest" */
                    float scrollInput = Input.GetAxis("Mouse ScrollWheel");
                    if (scrollInput != 0)
                    {
                        int deltaIndex   = (scrollInput > 0) ? 1 : -1;
                        int indexCurrent = _currentPiece.GetAllUnconnectedSockets().IndexOf(localSocketForConnection);
                        int indexNew     = indexCurrent + deltaIndex;
                        Debug.Log("SCROLL: " + scrollInput + " -- changing index of local socket from " + indexCurrent + " to " + indexNew + " (total socks on this object = " + _currentPiece.GetAllUnconnectedSockets().Count);
                        if (indexNew >= _currentPiece.GetAllUnconnectedSockets().Count)
                        {
                            indexNew = 0;
                        }
                        if (indexNew < 0)
                        {
                            indexNew = _currentPiece.GetAllUnconnectedSockets().Count - 1;
                        }


                        _currentPiece.GetAllUnconnectedSockets()[indexNew].SnapTo(nearestSocket, false);

                        // redo it
                        _snapModeOriginalJoinPosition          = nearestSocket.transform.position;
                        _snapModeOriginalPosition              = _currentPiece.transform.position;
                        _snapModeOrignalJoinDistanceFromCamera = Vector3.Distance(Camera.main.transform.position, _snapModeOriginalJoinPosition);
                        _snapModeOriginalMousePointNearSocket  = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(_snapModeOrignalJoinDistanceFromCamera);
                    }
                }
                else
                {
                    foreach (SnapGhostable ghostable in _currentPiece.GetComponentsInChildren <SnapGhostable>())
                    {
                        ghostable.SetSnapped(false);
                    }
                }

                if (Input.GetMouseButtonDown(0))
                {
                    //Debug.Log(" mouse clicked " );

                    // Clear the socket/ray dictionry since we'll have a new piece next
                    if (_lastRayForEachSocket != null)
                    {
                        _lastRayForEachSocket.Clear();
                    }

                    // Finalise the connection, if it is snappably-close (if not, leave it where it is)
                    if (nearestSocket != null)
                    {
                        //Debug.Log(" ... snapping: "+localSocketForConnection+" to: "+nearestSocket );
                        localSocketForConnection.SnapTo(nearestSocket, true);

                        if (removeWCRigidBodiesToo)
                        {
                            if (showDebugMessages)
                            {
                                Debug.Log("Removing Unity fake RBs");
                            }

                            foreach (WheelCollider wc in nearestSocket.parentPiece.GetComponentsInChildren <WheelCollider>())
                            {
                                if (showDebugMessages)
                                {
                                    Debug.Log("Removing fake RB from WC = " + wc.gameObject.name);
                                }
                                Rigidbody rbwc = wc.GetComponent <Rigidbody>();

                                if (rbwc != null)
                                {
                                    if (showDebugMessages)
                                    {
                                        Debug.Log("Destroying!");
                                    }
                                    GameObject.Destroy(rbwc);
                                }
                            }
                        }
                    }

                    // If it was ghosted, reset the object materials back to what they should be
                    foreach (SnapGhostable ghostable in _currentPiece.GetComponentsInChildren <SnapGhostable>())
                    {
                        ghostable.UnGhost();
                    }

                    _currentPiece = null;
                }
            }
        }
Exemple #6
0
 void Toggle(SnapSocket socket, SnapPlug plug)
 {
     // Debug.Log("Toggle");
     socket.ConnectedPlug = socket.ConnectedPlug == null ? plug : null;
 }
        private void OnMouseUp()
        {
            /***************************************************************//***************************************************************/
            // MAJOR bug in the Mono compiler that Unity is shipping: these variabels ARE USED below!
            // The variable 'variable' is assigned but its value is never used
#pragma warning disable 219



            Vector3    nearestRoomHitPoint         = Vector3.zero;
            float      nearestSocketSocketDistance = 999999f; // MUST calculate this in screen space, not world space!
            SnapPiece  nearestRoom              = null;
            SnapSocket nearestSocket            = null;
            SnapSocket localSocketForConnection = null;
#pragma warning restore 219

            /**
             * 1. Check each unconnected socket
             * 2. For each, find the piece it is nearest to
             * 3. On that piece, for ALL hits on the ray (front and back) find the unconnected socket that is nearest to the hit
             * 4. Stick with whichever socket was nearest
             */
            foreach (SnapSocket localSocket in _currentPiece.GetAllUnconnectedSockets())
            {
                Ray rayForSocket = _lastRayForEachSocket[localSocket];

                List <RaycastHit> hitsOnSnapPiece;
                SnapPiece         hitRoom = _currentPiece.FirstSnapPieceAlongRayWithAvailableSockets(rayForSocket, out hitsOnSnapPiece, localSocket);

                if (hitRoom != null)
                {
                    foreach (RaycastHit hit in hitsOnSnapPiece)
                    {
                        SnapSocket hitSocket = hitRoom.NearestUnconnectedSocketScreenSpace(Camera.main.WorldToScreenPoint(hit.point), Camera.main, 0f, localSocket, SnapAndPlugScene.sceneData.showDebugMessages);

                        if (hitSocket != null)
                        {
                            Vector3 screenForHitSocket   = Camera.main.WorldToScreenPoint(hitSocket.attachPoint);
                            Vector3 screenForLocalSocket = Camera.main.WorldToScreenPoint(localSocket.attachPoint);

                            float socketDistance = (screenForHitSocket - screenForLocalSocket).magnitude;
                            if (socketDistance < nearestSocketSocketDistance)
                            {
                                nearestSocketSocketDistance = socketDistance;
                                nearestRoom         = hitRoom;
                                nearestSocket       = hitSocket;
                                nearestRoomHitPoint = hit.point;

                                localSocketForConnection = localSocket;
                            }

                            if (SnapAndPlugScene.sceneData.showDebugMessages)
                            {
                                Debug.Log("Found " + hitsOnSnapPiece.Count + " hits on target piece; this one has nearest socket: " + hitSocket + " near hit: " + hit.point + " (" + socketDistance + " away)");
                            }
                        }
                        else
                        if (SnapAndPlugScene.sceneData.showDebugMessages)
                        {
                            Debug.Log("HIT on the room, but no unconnected sockets within range of point: " + hit.point);
                        }
                    }
                }
            }

            //Debug.Log(" mouse clicked " );

            // Clear the socket/ray dictionry since we'll have a new piece next
            if (_lastRayForEachSocket != null)
            {
                _lastRayForEachSocket.Clear();
            }

            // Finalise the connection, if it is snappably-close (if not, leave it where it is)
            if (nearestSocket != null)
            {
                //Debug.Log(" ... snapping: "+localSocketForConnection+" to: "+nearestSocket );
                localSocketForConnection.SnapTo(nearestSocket, true);

                if (removeWCRigidBodiesToo)
                {
                    if (showDebugMessages)
                    {
                        Debug.Log("Removing Unity fake RBs");
                    }

                    foreach (WheelCollider wc in nearestSocket.parentPiece.GetComponentsInChildren <WheelCollider>())
                    {
                        if (showDebugMessages)
                        {
                            Debug.Log("Removing fake RB from WC = " + wc.gameObject.name);
                        }
                        Rigidbody rbwc = wc.GetComponent <Rigidbody>();

                        if (rbwc != null)
                        {
                            if (showDebugMessages)
                            {
                                Debug.Log("Destroying!");
                            }
                            GameObject.Destroy(rbwc);
                        }
                    }
                }
            }

            // If it was ghosted, reset the object materials back to what they should be
            foreach (SnapGhostable ghostable in _currentPiece.GetComponentsInChildren <SnapGhostable>())
            {
                ghostable.UnGhost();
            }


            SnapGroup sG = _currentPiece.GetGroup();
            if (sG)
            {
                foreach (Rigidbody rB in _currentPiece.GetComponentsInChildren <Rigidbody>())
                {
                    //rB.isKinematic = true;
                    GameObject.Destroy(rB);
                }
                foreach (MySnappableSpawner sS in _currentPiece.GetComponentsInChildren <MySnappableSpawner>())
                {
                    //sS.enabled = false;
                    //rB.isKinematic = true;
                    GameObject.Destroy(sS);
                }


                sG.gameObject.AddComponent <Rigidbody>();
                //BoxCollider bC = sG.GetComponent<BoxCollider>();
                sG.gameObject.AddComponent <SnapPiece>();
                sG.gameObject.AddComponent <MySnappableSpawner>();
            }


            _currentPiece = null;
        }