//Called when the part is instantiated
        //Initializes the joints and attachments when in flight mode
        public override void OnStart(StartState state)
        {
            if (!initialized)
            {
                jointSpringValue  = jointSpring;
                jointDampingValue = jointDamping;
                initialized       = true;
            }
            //Debug.Log("[LYNX] OnStart");

            string[] names = visibleTransformNames.Split(',');

            if (names.Length == 2)
            {
                if (names[0].Replace(" ", string.Empty) != "NULL")
                {
                    VisibleTransforms[0] = KSPUtil.FindInPartModel(transform, names[0].Replace(" ", string.Empty));
                }
                if (names[1].Replace(" ", string.Empty) != "NULL")
                {
                    VisibleTransforms[1] = KSPUtil.FindInPartModel(transform, names[1].Replace(" ", string.Empty));
                }
            }

            if (HighLogic.LoadedSceneIsFlight)
            {
                //Initialize the reference transform
                InitReferences(false);
            }

            //update the interface
            UpdateUI();
        }
Пример #2
0
        //==================================================
        //Life Cycle
        //==================================================

        //Initialize the meshes and so on
        public override void OnStart(StartState state)
        {
            base.OnStart(state);

            targetTransform = KSPUtil.FindInPartModel(transform, toTransform.Replace(" ", string.Empty));
            sourceTransform = KSPUtil.FindInPartModel(transform, fromTransform.Replace(" ", string.Empty));

            valid = (targetTransform != null) & (sourceTransform != null);
        }
Пример #3
0
        /// <summary>
        /// This method is called once when the part is loaded into the current scene, after OnLoad.
        /// </summary>
        /// <param name="state"></param>
        public override void OnStart(StartState state)
        {
            base.OnStart(state);

            if (GameSettings.VERBOSE_DEBUG_LOG)
            {
                Debug.Log("[USI Tools] Hitch.OnStart called.");
            }

            // Let's see if we can find our fixed mesh and movable mesh
            try
            {
                _fixedMesh   = KSPUtil.FindInPartModel(transform, FixedMeshName);
                _movableMesh = KSPUtil.FindInPartModel(transform, MovableMeshName);

                if (_fixedMesh != null && _movableMesh != null)
                {
                    if (GameSettings.VERBOSE_DEBUG_LOG)
                    {
                        Debug.Log("[USI Tools] Hitch.OnStart: Found fixed mesh!");
                        Debug.Log("[USI Tools] Hitch.OnStart: Found movable mesh!");
                    }

                    // Give our FixedMesh and MovableMesh their own Rigidbody, if they don't already have one
                    Rigidbody fixedRigidbody   = _fixedMesh.gameObject.GetComponent <Rigidbody>();
                    Rigidbody movableRigidbody = _movableMesh.gameObject.GetComponent <Rigidbody>();

                    if (fixedRigidbody == null)
                    {
                        fixedRigidbody = _fixedMesh.gameObject.AddComponent <Rigidbody>();
                    }

                    if (movableRigidbody == null)
                    {
                        movableRigidbody = _movableMesh.gameObject.AddComponent <Rigidbody>();
                    }

                    // Set the mass of the Rigidbodies from .cfg
                    fixedRigidbody.mass   = FixedMeshMass;
                    movableRigidbody.mass = MovableMeshMass;
                }
                else
                {
                    throw new Exception("Part must contain child GameObjects named " + FixedMeshName + " and " + MovableMeshName + ".");
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("[USI Tools] Hitch.OnStart encountered an error: " + ex.Message);
            }
        }
Пример #4
0
        private void InitializeStatic(float delta)
        {
            var t = KSPUtil.FindInPartModel(this.transform, meshStatic);

            if (!t)
            {
                return;
            }

            t.RotateAround(this.transform.TransformPoint(this.anchor), this.part.transform.TransformDirection(this.axis), delta);
            if (this.part.parent)
            {
                t.parent = this.part.parent.transform;
            }
        }
Пример #5
0
        //==================================================
        //Methods
        //==================================================

        //Called when the part is instantiated
        //Initializes the transforms
        public override void OnStart(StartState state)
        {
            fromTransform = KSPUtil.FindInPartModel(transform, fromRotation);
            toTransform   = KSPUtil.FindInPartModel(transform, toRotation);

            string[] rotationValues      = targetValues.Split(',');
            string[] rotationTargetNames = targets.Split(',');

            rotationTargets.Clear();

            //get all the rotation targets
            if (rotationValues.Length == rotationTargetNames.Length)
            {
                for (int i = 0; i < rotationValues.Length; i++)
                {
                    RotationTarget rotationTarget = new RotationTarget();
                    rotationTarget.target = KSPUtil.FindInPartModel(transform, rotationTargetNames[i].Replace(" ", string.Empty));

                    bool valid = true;
                    //try to get the value for the interpolation
                    try
                    {
                        rotationTarget.value = float.Parse(rotationValues[i].Replace(" ", string.Empty));
                    }
                    catch
                    {
                        valid = false;
                    }

                    //when we have
                    if ((rotationTarget.target != null) && (valid))
                    {
                        rotationTargets.Add(rotationTarget);
                    }
                }
            }

            numTargets = rotationTargets.Count;

            //valid when all transforms have been found
            isValid = (fromTransform) & (toTransform);
        }
Пример #6
0
        //Called when the part is instantiated
        //Initializes the joints and attachments when in flight mode
        public override void OnStart(StartState state)
        {
            //Localization
            Fields["status"].guiName            = Localizer.GetStringByTag("#LOC_KERBETROTTER.hitch.status");
            Fields["jointSpringValue"].guiName  = Localizer.GetStringByTag("#LOC_KERBETROTTER.hitch.spring");
            Fields["jointDampingValue"].guiName = Localizer.GetStringByTag("#LOC_KERBETROTTER.hitch.damping");

            if (!initialized)
            {
                jointSpringValue  = jointSpring;
                jointDampingValue = jointDamping;
                initialized       = true;
            }
            //Debug.Log("[LYNX] OnStart");

            string[] names = visibleTransformNames.Split(',');

            if (names.Length == 2)
            {
                if (names[0].Replace(" ", string.Empty) != "NULL")
                {
                    VisibleTransforms[0] = KSPUtil.FindInPartModel(transform, names[0].Replace(" ", string.Empty));
                }
                if (names[1].Replace(" ", string.Empty) != "NULL")
                {
                    VisibleTransforms[1] = KSPUtil.FindInPartModel(transform, names[1].Replace(" ", string.Empty));
                }
            }

            if (HighLogic.LoadedSceneIsFlight)
            {
                //Initialize the reference transform
                InitReferences(false);
            }

            //update the interface
            UpdateUI();
        }
        //Called when the part is instantiated
        //Initializes the joints and attachments when in flight mode
        public override void OnStart(StartState state)
        {
            if (!initialized)
            {
                jointSpringValue  = jointSpring;
                jointDampingValue = jointDamping;
                initialized       = true;
            }

            string[] names = visibleTransformNames.Split(',');

            if (names.Length == 2)
            {
                if (names[0].Replace(" ", string.Empty) != "NULL")
                {
                    VisibleTransforms[0] = KSPUtil.FindInPartModel(transform, names[0].Replace(" ", string.Empty));
                }
                if (names[1].Replace(" ", string.Empty) != "NULL")
                {
                    VisibleTransforms[1] = KSPUtil.FindInPartModel(transform, names[1].Replace(" ", string.Empty));
                }
            }

            //Initialize the joints when in flight mode
            if (HighLogic.LoadedSceneIsFlight)
            {
                isInitializing = true;
                StartCoroutine(WaitAndInitialize());

                //save the initial rotation of the part
                initialPartRotation = part.orgRot;
            }

            //update the interface
            UpdateUI();
        }
Пример #8
0
        /// <summary>
        /// This method is called once when the part is loaded into the current scene, after OnLoad.
        /// </summary>
        /// <param name="state"></param>
        public override void OnStart(StartState state)
        {
            base.OnStart(state);

            if (GameSettings.VERBOSE_DEBUG_LOG)
            {
                Debug.Log("[USI Tools] Rotator.OnStart called.");
            }

            // Make sure the text for gui events is set correctly
            DirectionToggle(IsInverted);
            StartStopToggle(IsRunning);

            // Let's see if we can find our fixed mesh and movable mesh
            try
            {
                _fixedMesh   = KSPUtil.FindInPartModel(transform, FixedMeshName);
                _movableMesh = KSPUtil.FindInPartModel(transform, MovableMeshName);

                if (_fixedMesh != null && _movableMesh != null)
                {
                    if (GameSettings.VERBOSE_DEBUG_LOG)
                    {
                        Debug.Log("[USI Tools] Rotator.OnStart: Found fixed mesh!");
                        Debug.Log("[USI Tools] Rotator.OnStart: Found movable mesh!");
                    }

                    // Give our FixedMesh its own Rigidbody
                    Rigidbody fixedMeshRigidbody = _fixedMesh.gameObject.AddComponent <Rigidbody>();

                    // Give our MovableMesh its own Rigidbody
                    _movableMesh.gameObject.AddComponent <Rigidbody>();

                    // Setup a Joint for our MovableMesh and cache a reference to the motor for later
                    _rotatorJoint      = _movableMesh.gameObject.AddComponent <HingeJoint>();
                    _rotatorJointMotor = _rotatorJoint.motor;

                    // Mate the joint to the FixedMesh
                    _rotatorJoint.connectedBody = fixedMeshRigidbody;

                    // Configure other Joint options
                    _rotatorJoint.anchor = Vector3.zero;
                    _rotatorJoint.axis   = new Vector3(RotationAxisX, RotationAxisY, RotationAxisZ);
                    _rotatorJoint.autoConfigureConnectedAnchor = true;
                    _rotatorJoint.useMotor    = true;
                    _rotatorJoint.breakForce  = float.PositiveInfinity;
                    _rotatorJoint.breakTorque = float.PositiveInfinity;

                    // Setup the motor on the joint
                    UpdateMotor();
                }
                else
                {
                    throw new Exception("Part must contain child GameObjects named " + FixedMeshName + " and " + MovableMeshName + ".");
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("[USI Tools] Rotator.OnStart encountered an error: " + ex.Message);
            }
        }
        /// <summary>
        /// Attach the reference trasform the the parent if the situation is valid
        /// </summary>
        private void InitReferences(bool reInitialize)
        {
            Debug.Log("[LYNX] InitReferences");
            //do not init in the editor
            if (!HighLogic.LoadedSceneIsFlight)
            {
                return;
            }

            string[] names = referenceTransformNames.Split(',');

            if (names.Length == 2)
            {
                //get the first transform
                if (ReferenceTransforms[0] == null)
                {
                    //ReferenceTransforms[0] = Instantiate(part.transform);
                    ReferenceTransforms[0] = Instantiate(KSPUtil.FindInPartModel(transform, names[0].Replace(" ", string.Empty)));
                }

                //get the second transform
                if (ReferenceTransforms[1] == null)
                {
                    //ReferenceTransforms[1] = Instantiate(part.transform);
                    ReferenceTransforms[1] = Instantiate(KSPUtil.FindInPartModel(transform, names[1].Replace(" ", string.Empty)));
                }
            }


            if (part.parent == null)
            {
                if (!isLockEngaged)
                {
                    SetJointLock(true, true);
                }

                if (ReferenceTransforms[0] != null)
                {
                    ReferenceTransforms[0].parent = part.transform;
                    ReferenceTransforms[0]        = null;
                }

                if (ReferenceTransforms[1] != null)
                {
                    ReferenceTransforms[1].parent = part.transform;
                    ReferenceTransforms[1]        = null;
                }

                Debug.Log("[LYNX] ERR InitReferences: Part has no parent");
                hasParent         = false;
                isValidAttachment = false;
                currentParent     = null;
            }
            else
            {
                if (currentParent != part.parent)
                {
                    //Debug.Log("[LYNX] InitReferences: Parent has changed");
                    if (joint != null)
                    {
                        DestroyImmediate(joint);
                        LockJoint(false);
                        jointUnlocked = false;
                    }
                    currentParent = part.parent;
                }

                //Debug.Log("[LYNX] InitReferences: Part has parent: " + part.parent.partName);
                if ((ReferenceTransforms[0] != null) && (ReferenceTransforms[1] != null))
                {
                    string[] nodeNames = referenceNodeNames.Split(',');

                    //when we have a valid number of nodess
                    if (nodeNames.Length == 2)
                    {
                        //set the first transform
                        if (!reInitialize)
                        {
                            ReferenceTransforms[0].position = part.transform.position;
                            ReferenceTransforms[0].rotation = part.transform.rotation * Conjugate(rotation1);
                        }

                        AttachNode node1 = GetNode(nodeNames[0].Replace(" ", string.Empty));
                        if ((node1 != null) && (node1.attachedPart != null))
                        {
                            ReferenceTransforms[0].parent = node1.attachedPart.transform;
                        }
                        else
                        {
                            ReferenceTransforms[0].parent = part.transform;
                        }

                        //set the second transform
                        if (!reInitialize)
                        {
                            ReferenceTransforms[1].position = part.transform.position;
                            ReferenceTransforms[1].rotation = part.transform.rotation * Conjugate(rotation2);
                        }

                        AttachNode node2 = GetNode(nodeNames[1].Replace(" ", string.Empty));
                        if ((node2 != null) && (node2.attachedPart != null))
                        {
                            ReferenceTransforms[1].parent = node2.attachedPart.transform;
                        }
                        else
                        {
                            ReferenceTransforms[1].parent = part.transform;
                        }

                        //find out which is the active reference
                        if (node1.attachedPart == part.parent)
                        {
                            activeReference = 0;
                        }
                        else if (node2.attachedPart == part.parent)
                        {
                            activeReference = 1;
                        }
                        else
                        {
                            activeReference = -1;
                        }
                        //Debug.Log("[LYNX] InitReferences: activeReference: " + activeReference);
                    }
                    isValidAttachment = activeReference != -1;
                    if (activeReference != -1)
                    {
                        Debug.Log("[LYNX] ERR InitReferences: No active reference");
                    }
                }
                else
                {
                    isValidAttachment = false;
                    Debug.Log("[LYNX] ERR InitReferences: One of the references is null: " + (ReferenceTransforms[0] == null) + ", " + (ReferenceTransforms[1] == null));
                }
                hasParent = true;
            }
        }