KAS.KASModuleWinch.OnStart C# (CSharp) Method

OnStart() public method

public OnStart ( StartState state ) : void
state StartState
return void
        public override void OnStart(StartState state)
        {
            base.OnStart(state);
            if (state == StartState.Editor || state == StartState.None) {
              return;
            }

            texCable = GameDatabase.Instance.GetTexture(cableTexPath, false);
            if (!texCable) {
              KAS_Shared.DebugError("cable texture loading error !");
              ScreenMessages.PostScreenMessage(
              string.Format(
              "Texture file : {0} as not been found, please check your KAS installation !",
              cableTexPath),
              10, ScreenMessageStyle.UPPER_CENTER);
            }
            KAS_Shared.createFXSound(part, fxSndMotorStart, motorStartSndPath, false);
            KAS_Shared.createFXSound(part, fxSndMotor, motorSndPath, true);
            KAS_Shared.createFXSound(part, fxSndMotorStop, motorStopSndPath, false);
            KAS_Shared.createFXSound(part, fxSndHeadLock, headLockSndPath, false);
            KAS_Shared.createFXSound(part, fxSndEject, ejectSndPath, false);
            KAS_Shared.createFXSound(part, fxSndHeadGrab, headGrabSndPath, false);

            // Get head transform
            headTransform = this.part.FindModelTransform(headTransformName);
            if (!headTransform) {
              KAS_Shared.DebugError(
              "OnStart(Winch) Head transform {0} not found in the model !", headTransformName);
              DisableWinch();
              return;
            }

            // get winch anchor node
            winchAnchorNode = this.part.FindModelTransform(anchorNodeName);
            if (!winchAnchorNode) {
              KAS_Shared.DebugError(
              "OnStart(Winch) Winch anchor tranform node {0} not found in the model !", anchorNodeName);
              DisableWinch();
              return;
            }

            // Get head port node
            headPortNode = this.part.FindModelTransform(headPortNodeName);
            if (!headPortNode) {
              KAS_Shared.DebugError(
              "OnStart(Winch) Head transform port node {0} not found in the model !", headPortNodeName);
              DisableWinch();
              return;
            }

            //Set connector node transform
            AttachNode an = this.part.FindAttachNode(connectedPortNodeName);
            an.nodeTransform = new GameObject("KASWinchConnectorAn").transform;
            an.nodeTransform.parent = this.part.transform;
            an.nodeTransform.localPosition = an.position;
            an.nodeTransform.localRotation = Quaternion.LookRotation(an.orientation);
            an.nodeTransform.parent = headTransform;

            // Set linked object module
            KAS_LinkedPart linkedPart = headTransform.gameObject.AddComponent<KAS_LinkedPart>();
            linkedPart.part = this.part;

            // Create head anchor node
            headAnchorNode = new GameObject("KASHeadAnchor").transform;
            headAnchorNode.position = winchAnchorNode.position;
            headAnchorNode.rotation = winchAnchorNode.rotation;
            headAnchorNode.parent = headTransform;
            headAnchorNode.rotation *= Quaternion.Euler(new Vector3(180f, 0f, 0f));

            // Get original head position and rotation
            headOrgLocalPos = KAS_Shared.GetLocalPosFrom(headTransform, this.part.transform);
            headOrgLocalRot = KAS_Shared.GetLocalRotFrom(headTransform, this.part.transform);

            if (headState == PlugState.PlugDocked) {
              if (nodeConnectedPort) {
            KAS_Shared.DebugLog(
            "OnStart(Winch) NodeConnectedPort is : {0}", nodeConnectedPort.part.partInfo.title);
              } else {
            KAS_Shared.DebugWarning("OnStart(Winch) No connected part found !");
              }
            }

            // Get saved port module if any
            if (headState == PlugState.PlugDocked || headState == PlugState.PlugUndocked) {
              StartCoroutine(WaitAndLoadConnection());
            }

            if (headState != PlugState.Locked) {
              KAS_Shared.SetPartLocalPosRotFrom(headTransform, this.part.transform,
                                        headCurrentLocalPos, headCurrentLocalRot);
              SetTubeRenderer(true);
            }

            motorSpeedSetting = motorMaxSpeed / 2;

            KAS_Shared.DebugLog("OnStart(Winch) HeadState : {0}", headState);
            GameEvents.onVesselGoOnRails.Add(OnVesselGoOnRails);
            GameEvents.onVesselGoOffRails.Add(OnVesselGoOffRails);
            GameEvents.onCrewBoardVessel.Add(OnCrewBoardVessel);
        }