Пример #1
0
    private void OnCollisionEnter(Collision collision)
    {
        if (ifActive)
        {
            foreach (ContactPoint contact in collision.contacts)
            {
                if (contact.thisCollider.name == "Head")
                {
                    //Debug
                    Debug.Log("Structure Group Target Collider: " + contact.otherCollider.name);
                    Debug.Log("Structure Group This Collider: " + contact.thisCollider.name);
                    //Debug

                    if (contact.otherCollider.tag == "Structure")
                    {
                        GameObject thisNail       = contact.thisCollider.transform.parent.gameObject;
                        Nail       thisScript     = thisNail.GetComponent <Nail>();
                        GameObject otherStructure = contact.otherCollider.gameObject;
                        Structure  otherScript    = otherStructure.GetComponent <Structure>();

                        //Check if target structure is single or not
                        if (!otherScript.trackingManager)
                        {
                            //If target structure is single, add into structure group
                            //Debug
                            Debug.Log("Structure Group add new structure: " + otherStructure);
                            //Debug

                            //Try to remove rigid body
                            if (otherStructure.GetComponent <Rigidbody>())
                            {
                                Destroy(otherStructure.GetComponent <Rigidbody>());
                            }

                            //Try to remove the grabbale
                            if (otherStructure.GetComponent <OVRGrabbable>())
                            {
                                Destroy(otherStructure.GetComponent <OVRGrabbable>());
                            }

                            //Add as child and start tracking
                            otherStructure.transform.parent = transform;
                            //AddChild(otherStructure.transform);

                            //Update the target structure
                            otherScript.trackingManager = gameObject;

                            //Connect nail and structure
                            thisNail.GetComponent <Nail>().addToConnect(otherStructure);
                            otherScript.AddConnect(thisNail);
                        }
                        else
                        {
                            //Get the target manager
                            GameObject targetManager = otherScript.trackingManager;

                            //Disable the target tracking manager
                            targetManager.GetComponent <StructureGroup>().ifActive = false;

                            //Debug
                            Debug.Log("SG Trans: from " + targetManager.name + " to " + gameObject.name);
                            Debug.Log("SG Trans: target child num is: " + targetManager.transform.childCount);
                            //Debug

                            //Transfer all the child to this manager
                            //Use the while loop here in order to correct tracking the child number
                            //The old version with for loop will make the counter change as child moved
                            while (targetManager.transform.childCount > 0)
                            {
                                //Get child
                                GameObject currentObject = targetManager.transform.GetChild(0).gameObject;

                                //Debug
                                //targetList.Add(currentObject);
                                //continue;
                                //Debug.Log("SG Trans: Total child: " + targetManager.transform.childCount);
                                //Debug.Log("SG Trans: target " + i + " child is");
                                //Debug.Log(currentObject.name);
                                //Debug

                                //Start to tracking the child
                                //AddChild(currentObject.transform);

                                //Debug
                                //currentObject.GetComponent<Renderer>().material.color = new Color(255, 0, 0);
                                //Debug

                                //Check if stud or nail
                                if (currentObject.tag == "Tools")
                                {
                                    //Debug
                                    Debug.Log("SG Trans: Find Tool '" + currentObject.name + "' in " + targetManager.name);
                                    //Debug

                                    //Set manager
                                    Nail currentScript = currentObject.GetComponent <Nail>();
                                    currentScript.structureGroup = this.gameObject;
                                }
                                else if (currentObject.tag == "Structure")
                                {
                                    //Set manager
                                    Structure currentScript = currentObject.GetComponent <Structure>();
                                    currentScript.trackingManager = this.gameObject;
                                }

                                //Transfer child
                                currentObject.transform.parent = transform;
                            }

                            //Debug
                            //thisNail.GetComponent<Renderer>().material.color = new Color(255, 0, 0);
                            //otherStructure.GetComponent<Renderer>().material.color = new Color(255, 0, 0);
                            //Debug

                            //Connect nail and structure
                            thisScript.addToConnect(otherStructure);
                            otherScript.AddConnect(thisNail);

                            //Debug.log
                            Debug.Log("SG Trans finish.");
                            //Debug

                            //Destroy target manager
                            Destroy(targetManager);
                        }

                        //Debug
                        //targetList = IterateChild();
                        //Debug
                    }
                }
            }
        }
    }