Exemplo n.º 1
0
    public void bondWithAtom(GameObject obj, int bondOrder)
    {
        AtomScript collidedAtomScript = obj.GetComponent <AtomScript>();

        if (nBondConnections < allowedBonds && collidedAtomScript.nBondConnections < collidedAtomScript.allowedBonds)
        {
            //Debug.Log(obj.tag.Contains("Bullet"));
            //Debug.Log(obj.tag);

            if (obj.tag.Contains("Bullet"))
            {
                Slug slugScript = obj.GetComponent <Slug>();
                slugScript.resetToAtom();
            }
            else if (tag.Contains("Bullet"))
            {
                Slug slugScript = GetComponent <Slug>();
                slugScript.resetToAtom();
            }


            if (bondedAtoms.Contains(obj) == false)        //Ignore collision with atom I am already bonded to



            {
                GameObject startAtom       = gameObject;
                AtomScript startAtomScript = this;
                GameObject endAtom         = obj;
                AtomScript endAtomScript   = collidedAtomScript;

                //Has no bonds
                bool startAtomIsSingletonAtom = startAtomScript.getBondedAtoms().Count == 0;
                bool endAtomIsSingletonAtom   = endAtomScript.getBondedAtoms().Count == 0;



                Vector3    startAtomOldPosition = startAtom.transform.position;
                Quaternion startAtomOldRotation = startAtom.transform.rotation;

                Vector3    endAtomOldPosition = endAtom.transform.position;
                Quaternion endAtomOldRotation = endAtom.transform.rotation;


                GameObject endAtomStub   = closestStubBond(endAtomScript, startAtomScript); //endAtomScript.getStubBondsList()[closestStubBondOnEndAtomIndex];
                GameObject startAtomStub = closestStubBond(startAtomScript, endAtomScript);



                Vector3 endAtomStubDirection = (endAtomStub.transform.position - endAtom.transform.position).normalized;

                Vector3 startAtomStubDirection = (startAtomStub.transform.position - startAtom.transform.position).normalized;

                /*
                 * //Checks to see if a double bond should be formed
                 * int possibleBondsOfThisAtom = allowedBonds - nBondConnections;
                 * int possibleBondsOfCollidedAtom = collidedAtomScript.allowedBonds - collidedAtomScript.nBondConnections;
                 *
                 * //selects the lowest number of open bonds
                 * int newBondOrder = Mathf.Min(possibleBondsOfThisAtom, possibleBondsOfCollidedAtom);
                 * //If the lowest number of open bonds is greater than 2, then it will default to 2, since triple bonds haven't been added yet
                 * if (newBondOrder > 2)
                 *  newBondOrder = 2;*/

                //bondPrefab is the placeholder for either a single or double bond
                GameObject bondPrefab;

                int newBondOrder = bondOrder;
                if (newBondOrder == 1)
                {
                    bondPrefab = bondGameObject;
                }
                else if (newBondOrder == 2)
                {
                    bondPrefab = doubleBondGameObject;
                    GameObject endAtomSecondClosestStub   = closestStubBond(endAtomScript, startAtomScript, endAtomStub);
                    GameObject startAtomSecondClosestStub = closestStubBond(startAtomScript, endAtomScript, startAtomStub);

                    endAtomStubDirection = ((endAtomStub.transform.position + endAtomSecondClosestStub.transform.position) / 2 - endAtom.transform.position).normalized;

                    startAtomStubDirection = ((startAtomStub.transform.position + startAtomSecondClosestStub.transform.position) / 2 - startAtom.transform.position).normalized;
                }
                else
                {
                    bondPrefab = bondGameObject;
                }


                if (startAtomIsSingletonAtom)
                {
                    Vector3 directionFromStartToEndAtom = endAtom.transform.position - startAtom.transform.position;
                    //Vector3 oldStartAtomStubDirection = (startAtomStub.transform.position - startAtom.transform.position).normalized;

                    startAtom.transform.rotation = Quaternion.FromToRotation(startAtomStubDirection, directionFromStartToEndAtom) * startAtomOldRotation;
                }

                if (endAtomIsSingletonAtom)
                {
                    Vector3 directionFromEndToStartAtom = startAtom.transform.position - endAtom.transform.position;
                    // Vector3 oldEndAtomStubDirection = (endAtomStub.transform.position - endAtom.transform.position).normalized;

                    endAtom.transform.rotation = Quaternion.FromToRotation(endAtomStubDirection, directionFromEndToStartAtom) * endAtomOldRotation;
                }



                //reset the bond directions

                if (newBondOrder == 2)
                {
                    GameObject endAtomSecondClosestStub   = closestStubBond(endAtomScript, startAtomScript, endAtomStub);
                    GameObject startAtomSecondClosestStub = closestStubBond(startAtomScript, endAtomScript, startAtomStub);
                    endAtomStubDirection = ((endAtomStub.transform.position + endAtomSecondClosestStub.transform.position) / 2 - endAtom.transform.position).normalized;

                    startAtomStubDirection = ((startAtomStub.transform.position + startAtomSecondClosestStub.transform.position) / 2 - startAtom.transform.position).normalized;
                }
                else if (newBondOrder == 1)
                {
                    endAtomStubDirection   = (endAtomStub.transform.position - endAtom.transform.position).normalized;
                    startAtomStubDirection = (startAtomStub.transform.position - startAtom.transform.position).normalized;
                }



                //Set temporary position of atoms. This will be their real position once the joints drag them into position
                float bondDistanceModifier = (endAtom.transform.localScale.x + startAtom.transform.localScale.x) * (.65f);
                startAtom.transform.position = endAtom.transform.position + bondDistanceModifier * endAtomStubDirection;
                startAtom.transform.rotation = Quaternion.FromToRotation(startAtomStubDirection, -endAtomStubDirection) * startAtomOldRotation;



                //The new bond is instantiated, and then formBond is called on the two connected atoms (along with bondOrder, which says whether the bond is a single, double, or triple bond)
                GameObject newBond = Instantiate(bondPrefab, gameObject.transform.position, Quaternion.identity);
                newBond.GetComponent <Bond> ().formBond(startAtom, endAtom, newBondOrder);

                startAtom.transform.position = startAtomOldPosition;

                startAtom.transform.rotation = startAtomOldRotation;
            }
        }
    }