コード例 #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;
            }
        }