private SnapGroup CreateSnapGroup()
    {
        GameObject snapGroupGameObject = Instantiate(snapGroupPrefab);
        SnapGroup  snapGroup           = snapGroupGameObject.GetComponent <SnapGroup>();

        snapGroup.transform.parent = transform;
        snapGroupList.Add(snapGroup);
        return(snapGroup);
    }
 private void DestroySnapGroup(SnapGroup snapGroup)
 {
     if (!snapGroup)
     {
         return;
     }
     snapGroupList.Remove(snapGroup);
     GameObject.Destroy(snapGroup.gameObject);
 }
    private void MergeGroupsOfPieces(Piece a, Piece b)
    {
        //create the new group
        SnapGroup snapGroup = CreateSnapGroup();

        //lets make a list of pieces and it's related (connected) pieces;
        List <Piece> pieces = new List <Piece>();

        pieces.AddRange(GetPiecesInSameGroup(a));
        pieces.AddRange(GetPiecesInSameGroup(b));

        //move pieces into the new and shiny group :O
        foreach (Piece piece in pieces)
        {
            piece.transform.parent = snapGroup.transform;
        }
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        SnapGroup group = GetComponentInParent <SnapGroup>();

        if (group != null)
        {
            //EngineBlock myEngine = null;
            float totalPowerFromEngines = 0f;
            foreach (EngineBlock engine in group.GetComponentsInChildren <EngineBlock>())
            {
                totalPowerFromEngines += engine.currentPowerSentToEachComponent;
                break;
            }

            if (totalPowerFromEngines > 0f)
            {
                powerReceived = totalPowerFromEngines;

                //Debug.Log(" Have engine, driving! Engine gives torque = "+myEngine.currentPowerSentToEachComponent );
                WheelCollider wc = GetComponentInChildren <WheelCollider>();

                if (wc != null)
                {
                    wc.motorTorque = powerReceived;

                    float dir = Vector3.Angle(transform.forward, group.transform.forward) < 20f ? 1f : -1f;

                    childWheelToSpinFast.transform.localRotation *= Quaternion.AngleAxis(360f * (dir * Time.deltaTime * wc.rpm / 60f), new Vector3(1f, 0, 0));
                }
            }
            else
            {
                powerReceived = 0f;
                Debug.Log(" :( :( no engine :( :(");
            }
        }
    }
        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;
        }