ResetCollisionEnhancer() публичный статический Метод

public static ResetCollisionEnhancer ( Part p, bool create_new = true ) : void
p Part
create_new bool
Результат void
Пример #1
0
        private IEnumerator WaitAndAttach(Part partToAttach, Vector3 position, Quaternion rotation, Part toPart = null)
        {
            while (!partToAttach.rigidbody)
            {
                KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Waiting rigidbody to initialize...");
                yield return(new WaitForFixedUpdate());
            }
            KASModuleGrab moduleGrab = partToAttach.GetComponent <KASModuleGrab>();

            if (toPart)
            {
                KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Rigidbody initialized, setting velocity...");
                partToAttach.rigidbody.velocity        = toPart.rigidbody.velocity;
                partToAttach.rigidbody.angularVelocity = toPart.rigidbody.angularVelocity;
                KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Waiting velocity to apply by waiting 0.1 seconds...");
                yield return(new WaitForSeconds(0.1f));

                partToAttach.transform.position = position;
                partToAttach.transform.rotation = rotation;
                partToAttach.Couple(toPart);
                if (moduleGrab)
                {
                    moduleGrab.fxSndAttachPart.audio.Play();
                }
                else
                {
                    KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, cannot fire sound");
                }
            }
            else
            {
                partToAttach.transform.position = position;
                partToAttach.transform.rotation = rotation;
                if (moduleGrab)
                {
                    moduleGrab.AttachStatic();
                    moduleGrab.fxSndAttachStatic.audio.Play();
                }
                else
                {
                    KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, part cannot be attached on static");
                }
            }
            KAS_Shared.ResetCollisionEnhancer(partToAttach);
        }
Пример #2
0
        public void UpdatePointer()
        {
            if (!running)
            {
                if (pointer)
                {
                    UnityEngine.Object.Destroy(pointer);
                }
                return;
            }

            //Cast ray
            Ray        ray = FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (!Physics.Raycast(ray, out hit, 500, 557059))
            {
                if (pointer)
                {
                    UnityEngine.Object.Destroy(pointer);
                }
                return;
            }

            //Create pointer if needed
            if (!pointer)
            {
                GameObject modelGo = partToAttach.FindModelTransform("model").gameObject;
                pointer = Mesh.Instantiate(modelGo) as GameObject;
                foreach (Collider col in pointer.GetComponentsInChildren <Collider>())
                {
                    UnityEngine.Object.Destroy(col);
                }

                allModelMr = new List <MeshRenderer>();
                // Remove attached tube mesh renderer if any
                List <MeshRenderer> tmpAllModelMr = new List <MeshRenderer>(pointer.GetComponentsInChildren <MeshRenderer>() as MeshRenderer[]);
                foreach (MeshRenderer mr in tmpAllModelMr)
                {
                    if (mr.name == "KAStube" || mr.name == "KASsrcSphere" || mr.name == "KASsrcTube" || mr.name == "KAStgtSphere" || mr.name == "KAStgtTube")
                    {
                        Destroy(mr);
                        continue;
                    }
                    allModelMr.Add(mr);
                    mr.material = new Material(Shader.Find("Transparent/Diffuse"));
                }
                pointerNodeTransform               = new GameObject("KASPointerPartNode").transform;
                pointerNodeTransform.parent        = pointer.transform;
                pointerNodeTransform.localPosition = partToAttach.srfAttachNode.position;
                pointerNodeTransform.localRotation = Quaternion.Inverse(Quaternion.LookRotation(partToAttach.srfAttachNode.orientation, Vector3.up));
            }

            //Set default color
            Color color = Color.green;

            // Check if object is valid
            bool      isValidObj = false;
            Part      hitPart    = null;
            KerbalEVA hitEva     = null;

            if (hit.rigidbody)
            {
                hitPart = hit.rigidbody.GetComponent <Part>();
                hitEva  = hit.rigidbody.GetComponent <KerbalEVA>();
                if (hitPart && allowPart && !hitEva & hitPart != partToAttach)
                {
                    isValidObj = true;
                }
                if (hitEva && allowEva)
                {
                    isValidObj = true;
                }
            }
            if (!hitPart && !hitEva && allowStatic)
            {
                isValidObj = true;
            }

            //Check distance
            bool isValidSourceDist = true;

            if (sourceTransform)
            {
                isValidSourceDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, sourceTransform.position) <= maxDist;
            }
            bool isValidTargetDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, hit.point) <= maxDist;

            //Set color
            if (!isValidObj)
            {
                color = Color.red;
            }
            else if (!isValidSourceDist || !isValidTargetDist)
            {
                color = Color.yellow;
            }
            color.a = 0.5f;
            foreach (MeshRenderer mr in allModelMr)
            {
                mr.material.color = color;
            }

            //Rotation keys
            Vector3 delta = new Vector3(0, 0, 1);

            if (Input.GetKey(KeyCode.LeftAlt))
            {
                delta = new Vector3(1, 0, 0);
            }
            else if (Input.GetKey(KeyCode.RightAlt))
            {
                delta = new Vector3(0, -1, 0);
            }

            if (Input.GetKeyDown(KASAddonControlKey.rotateLeftKey.ToLower()))
            {
                customRot -= delta * 15;
            }
            if (Input.GetKeyDown(KASAddonControlKey.rotateRightKey.ToLower()))
            {
                customRot += delta * 15;
            }

            Quaternion rotAdjust = Quaternion.Euler(0, 0, customRot.z) * Quaternion.Euler(customRot.x, customRot.y, 0);

            KAS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hit, rotAdjust);

            //Attach on click
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                KAS_Shared.DebugLog("Attachment started...");
                if (!isValidObj)
                {
                    ScreenMessages.PostScreenMessage("Can't attach, target is not allowed !");
                    audioBipWrong.Play();
                    return;
                }

                if (!isValidSourceDist)
                {
                    ScreenMessages.PostScreenMessage("Can't attach, too far from source !");
                    audioBipWrong.Play();
                    return;
                }

                if (!isValidTargetDist)
                {
                    ScreenMessages.PostScreenMessage("Can't attach, too far from target !");
                    audioBipWrong.Play();
                    return;
                }

                KASModuleGrab modulegrab = partToAttach.GetComponent <KASModuleGrab>();


                //Move and attach mode
                if (pointerMode == PointerMode.MoveAndAttach)
                {
                    // Drop and detach part if needed
                    if (modulegrab)
                    {
                        if (modulegrab.grabbed)
                        {
                            modulegrab.Drop();
                        }
                        modulegrab.Detach();
                    }

                    KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(partToAttach);
                    if (!connectedWinch)
                    {
                        KAS_Shared.DecoupleFromAll(partToAttach);
                    }

                    //Move part
                    partToAttach.transform.position = pointer.transform.position;
                    partToAttach.transform.rotation = pointer.transform.rotation;

                    if (connectedWinch)
                    {
                        //Set cable lenght to real lenght
                        connectedWinch.cableJointLength = connectedWinch.cableRealLenght;
                    }

                    KAS_Shared.ResetCollisionEnhancer(partToAttach);

                    if (msgOnly)
                    {
                        KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach using send message");
                        if (hitPart)
                        {
                            partToAttach.SendMessage("OnAttachPart", hitPart, SendMessageOptions.DontRequireReceiver);
                        }
                        else
                        {
                            partToAttach.SendMessage("OnAttachStatic", SendMessageOptions.DontRequireReceiver);
                        }
                    }
                    else
                    {
                        KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach with couple or static method");
                        if (!hitPart && !hitEva)
                        {
                            if (modulegrab)
                            {
                                modulegrab.AttachStatic();
                                modulegrab.fxSndAttachStatic.audio.Play();
                            }
                            else
                            {
                                KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, part cannot be attached on static");
                            }
                        }
                        else
                        {
                            partToAttach.Couple(hitPart);
                            if (modulegrab)
                            {
                                modulegrab.fxSndAttachPart.audio.Play();
                            }
                            else
                            {
                                KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, cannot fire sound");
                            }
                        }
                        partToAttach.SendMessage("OnAttach", SendMessageOptions.DontRequireReceiver);
                    }
                }

                if (pointerMode == PointerMode.CopyAndAttach)
                {
                    // Not tested !
                    Part newPart = KAS_Shared.CreatePart(partToAttach.partInfo, pointer.transform.position, pointer.transform.rotation, partToAttach);
                    if (msgOnly)
                    {
                        if (hitPart)
                        {
                            StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation, hitPart));
                        }
                        else
                        {
                            StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation));
                        }
                    }
                    else
                    {
                        if (!hitPart && !hitEva)
                        {
                            StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation, hitPart));
                        }
                        else
                        {
                            StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation));
                        }
                    }
                }
                running = false;
            }
        }
Пример #3
0
        public void Grab(Vessel kerbalEvaVessel)
        {
            KAS_Shared.DebugLog("Grab - Grabbing part :" + this.part.partInfo.name);

            if (!AlignEvaPosition(kerbalEvaVessel))
            {
                return;
            }

            //Grab winch connected head if any
            KASModuleWinch moduleWinch = KAS_Shared.GetConnectedWinch(this.part);

            if (moduleWinch)
            {
                KASModulePort modulePort = this.part.GetComponent <KASModulePort>();
                moduleWinch.UnplugHead(false);
                moduleWinch.GrabHead(kerbalEvaVessel, modulePort);
            }

            keepTriggers = new List <Collider>();

            List <Collider> allColliders = new List <Collider>(this.part.GetComponentsInChildren <Collider>() as Collider[]);

            foreach (Collider col in allColliders)
            {
                if (col.isTrigger)
                {
                    keepTriggers.Add(col);
                }

                col.isTrigger = true;
            }

            Detach();

            if (this.part.vessel != kerbalEvaVessel)
            {
                KAS_Shared.DecoupleFromAll(this.part);
                this.part.Couple(kerbalEvaVessel.rootPart);
            }
            else
            {
                if (this.part.parent != kerbalEvaVessel.rootPart)
                {
                    this.part.setParent(null);
                    this.part.setParent(kerbalEvaVessel.rootPart);
                }
                this.part.PromoteToPhysicalPart();
            }

            //Destroy joint to avoid buggy eva move
            Destroy(this.part.attachJoint);

            this.part.rigidbody.velocity = kerbalEvaVessel.rootPart.rigidbody.velocity;

            if (physicJoint)
            {
                if (evaJoint)
                {
                    Destroy(evaJoint);
                }
                evaJoint = this.part.gameObject.AddComponent <FixedJoint>();
                evaJoint.connectedBody = evaCollider.attachedRigidbody;
                evaJoint.breakForce    = 5;
                evaJoint.breakTorque   = 5;
                KAS_Shared.ResetCollisionEnhancer(this.part);
            }
            else
            {
                this.part.physicalSignificance  = Part.PhysicalSignificance.NONE;
                this.part.transform.parent      = evaNodeTransform;
                this.part.rigidbody.isKinematic = true;
                KAS_Shared.ResetCollisionEnhancer(this.part, false);
            }

            //Add grabbed part mass to eva
            if (addPartMass && !physicJoint)
            {
                orgKerbalMass = kerbalEvaVessel.rootPart.mass;
                kerbalEvaVessel.rootPart.mass += this.part.mass;
            }

            GameEvents.onCrewBoardVessel.Add(new EventData <GameEvents.FromToAction <Part, Part> > .OnEvent(this.OnCrewBoardVessel));

            evaHolderVesselName = kerbalEvaVessel.vesselName;
            evaHolderPart       = kerbalEvaVessel.rootPart;
            grabbed             = true;
            grab_pending        = false;

            RefreshContextMenu();

            //Play grab sound
            fxSndGrab.audio.Play();
            base.SendMessage("OnPartGrabbed", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver);
        }
Пример #4
0
        public void Drop()
        {
            if (grabbed)
            {
                KAS_Shared.DebugLog("Drop - Dropping part :" + this.part.partInfo.name);
                base.SendMessage("OnPartDrop", SendMessageOptions.DontRequireReceiver);

                if (this.part.rigidbody)
                {
                    this.part.transform.parent     = null;
                    this.part.physicalSignificance = Part.PhysicalSignificance.FULL;
                }

                if (this.part.vessel.isEVA || grab_pending)
                {
                    this.part.decouple();
                }

                if (grab_pending)
                {
                    Destroy(evaNodeTransform.gameObject);
                    evaNodeTransform    = null;
                    evaHolderVesselName = null;
                    evaHolderPart       = null;
                    grabbed             = grab_pending = false;
                    RefreshContextMenu();
                    return;
                }

                //Remove created joints between eva and part if exist
                KAS_Shared.RemoveFixedJointBetween(this.part, evaHolderPart);
                KAS_Shared.RemoveHingeJointBetween(this.part, evaHolderPart);

                List <Collider> allColliders = new List <Collider>(this.part.GetComponentsInChildren <Collider>() as Collider[]);
                foreach (Collider col in allColliders)
                {
                    col.isTrigger = (keepTriggers != null && keepTriggers.Contains(col));
                }

                if (customGroundPos && evaHolderPart.checkLanded())
                {
                    KAS_Shared.MoveRelatedTo(this.part.transform, evaCollider.transform, dropPartPos, dropPartRot);
                }
                else
                {
                    KAS_Shared.MoveAlign(this.part.transform, partNode.nodeTransform, evaNodeTransform);
                }

                if (evaNodeTransform)
                {
                    Destroy(evaNodeTransform.gameObject);
                }
                if (evaJoint)
                {
                    Destroy(evaJoint);
                }

                this.part.transform.parent          = null;
                this.part.rigidbody.isKinematic     = false;
                this.part.physicalSignificance      = Part.PhysicalSignificance.FULL;
                this.part.rigidbody.velocity        = evaHolderPart.rigidbody.velocity;
                this.part.rigidbody.angularVelocity = evaHolderPart.rigidbody.angularVelocity;

                KAS_Shared.ResetCollisionEnhancer(this.part);

                if (addPartMass & !physicJoint)
                {
                    evaHolderPart.mass = orgKerbalMass;
                }

                KASModuleWinch grabbedWinchHead = KAS_Shared.GetWinchModuleGrabbed(evaHolderPart.vessel);
                if (grabbedWinchHead)
                {
                    if (grabbedWinchHead.grabbedPortModule)
                    {
                        KAS_Shared.DebugLog("Drop - Grabbed part have a port connected");
                        grabbedWinchHead.PlugHead(grabbedWinchHead.grabbedPortModule, KASModuleWinch.PlugState.PlugDocked, fireSound: false);
                    }
                }

                GameEvents.onCrewBoardVessel.Remove(new EventData <GameEvents.FromToAction <Part, Part> > .OnEvent(this.OnCrewBoardVessel));

                keepTriggers        = null;
                evaJoint            = null;
                evaNodeTransform    = null;
                evaHolderVesselName = null;
                evaHolderPart       = null;
                grabbed             = grab_pending = false;

                RefreshContextMenu();

                //Send drop message to all child objects
                base.SendMessage("OnPartDropped", SendMessageOptions.DontRequireReceiver);
            }
            else
            {
                KAS_Shared.DebugWarning("Drop - Part not grabbed, ignoring drop...");
            }
        }
Пример #5
0
        private IEnumerator GrabCoroutine(Vessel kerbalEvaVessel)
        {
            KAS_Shared.DebugLog("Grab - Grabbing part :" + this.part.partInfo.name);
            //Send message to other modules
            base.SendMessage("OnPartGrab", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver);

            //Set attach node on EVA
            SetEvaNode(kerbalEvaVessel);

            //Drop grabbed eva part if any
            KASModuleGrab grabbbedPartModule = KAS_Shared.GetGrabbedPartModule(kerbalEvaVessel);

            if (grabbbedPartModule)
            {
                grabbbedPartModule.Drop();
            }

            //Unplug winch connected head if any
            KASModuleWinch moduleWinch = KAS_Shared.GetConnectedWinch(this.part);

            if (moduleWinch)
            {
                moduleWinch.UnplugHead(false);
            }

            //Disable all colliders on part
            DisableColliders();

            //Detach if needed
            Detach();

            //Decouple part (if not already done, in case of loading after a save with the part grabbed)
            if (this.part.vessel != kerbalEvaVessel)
            {
                KAS_Shared.DecoupleFromAll(this.part);
            }

            //Wait decouple action (x64 fix)
            yield return(new WaitForFixedUpdate());

            //Move part to eva node
            KAS_Shared.MoveAlign(this.part.transform, partNode.nodeTransform, evaNodeTransform);

            //Grab winch connected head if any
            if (moduleWinch)
            {
                KASModulePort modulePort = this.part.GetComponent <KASModulePort>();
                moduleWinch.GrabHead(kerbalEvaVessel, modulePort);
            }

            //Couple part to eva (if not already done, in case of loading after a save with the part grabbed)
            if (this.part.vessel != kerbalEvaVessel)
            {
                this.part.Couple(kerbalEvaVessel.rootPart);
            }

            //Destroy joint to avoid buggy eva move
            if (this.part.attachJoint)
            {
                this.part.attachJoint.DestroyJoint();
            }

            //Set part to physic join or kinematic with parent
            if (physicJoint)
            {
                if (evaJoint)
                {
                    Destroy(evaJoint);
                }
                evaJoint = this.part.gameObject.AddComponent <FixedJoint>();
                evaJoint.connectedBody = evaCollider.attachedRigidbody;
                evaJoint.breakForce    = 5;
                evaJoint.breakTorque   = 5;
                KAS_Shared.ResetCollisionEnhancer(this.part);
            }
            else
            {
                syncGrab = true;
                KAS_Shared.ResetCollisionEnhancer(this.part, false);
            }

            //Add grabbed part mass to eva
            if (addPartMass && !physicJoint)
            {
                orgKerbalMass = kerbalEvaVessel.rootPart.mass;
                kerbalEvaVessel.rootPart.mass += this.part.mass;
            }

            //Add event
            GameEvents.onCrewBoardVessel.Add(new EventData <GameEvents.FromToAction <Part, Part> > .OnEvent(this.OnCrewBoardVessel));

            //Set variables
            evaHolderVesselName = kerbalEvaVessel.vesselName;
            evaHolderPart       = kerbalEvaVessel.rootPart;
            grabbed             = true;
            grab_pending        = false;

            //Refresh Context Menu
            RefreshContextMenu();

            //Play grab sound
            fxSndGrab.audio.Play();

            //Send message to other modules
            base.SendMessage("OnPartGrabbed", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver);
        }