Ejemplo n.º 1
0
        private static IEnumerator WaitAndCouple(Part newPart, Part tgtPart = null, string srcAttachNodeID = null, AttachNode tgtAttachNode = null, OnPartCoupled onPartCoupled = null)
        {
            // Get relative position & rotation
            Vector3 toPartLocalPos = Vector3.zero;
            Quaternion toPartLocalRot = Quaternion.identity;
            if (tgtPart)
            {
                if (tgtAttachNode == null)
                {
                    // Local position & rotation from part
                    toPartLocalPos = tgtPart.transform.InverseTransformPoint(newPart.transform.position);
                    toPartLocalRot = Quaternion.Inverse(tgtPart.transform.rotation) * newPart.transform.rotation;
                }
                else
                {
                    // Local position & rotation from node (KAS winch connector)
                    toPartLocalPos = tgtAttachNode.nodeTransform.InverseTransformPoint(newPart.transform.position);
                    toPartLocalRot = Quaternion.Inverse(tgtAttachNode.nodeTransform.rotation) * newPart.transform.rotation;
                }
            }

            // Wait part to initialize
            while (!newPart.started && newPart.State != PartStates.DEAD)
            {
                KIS_Shared.DebugLog("CreatePart - Waiting initialization of the part...");
                if (tgtPart)
                {
                    // Part stay in position
                    if (tgtAttachNode == null)
                    {
                        newPart.transform.position = tgtPart.transform.TransformPoint(toPartLocalPos);
                        newPart.transform.rotation = tgtPart.transform.rotation * toPartLocalRot;
                    }
                    else
                    {
                        newPart.transform.position = tgtAttachNode.nodeTransform.TransformPoint(toPartLocalPos);
                        newPart.transform.rotation = tgtAttachNode.nodeTransform.rotation * toPartLocalRot;
                    }
                }
                yield return null;
            }
            // Part stay in position
            if (tgtAttachNode == null)
            {
                newPart.transform.position = tgtPart.transform.TransformPoint(toPartLocalPos);
                newPart.transform.rotation = tgtPart.transform.rotation * toPartLocalRot;
            }
            else
            {
                newPart.transform.position = tgtAttachNode.nodeTransform.TransformPoint(toPartLocalPos);
                newPart.transform.rotation = tgtAttachNode.nodeTransform.rotation * toPartLocalRot;
            }
            KIS_Shared.DebugLog("CreatePart - Coupling part...");
            CouplePart(newPart, tgtPart, srcAttachNodeID, tgtAttachNode);

            if (onPartCoupled != null)
            {
                onPartCoupled(newPart, tgtPart, tgtAttachNode);
            }
        }
Ejemplo n.º 2
0
        public static Part CreatePart(ConfigNode partConfig, Vector3 position, Quaternion rotation, Part fromPart, Part coupleToPart = null, string srcAttachNodeID = null, AttachNode tgtAttachNode = null, OnPartCoupled onPartCoupled = null)
        {
            ConfigNode node_copy = new ConfigNode();
            partConfig.CopyTo(node_copy);
            ProtoPartSnapshot snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame);

            if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0)
            {
                snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
            }
            snapshot.parentIdx = 0;
            snapshot.position = position;
            snapshot.rotation = rotation;
            snapshot.stageIndex = 0;
            snapshot.defaultInverseStage = 0;
            snapshot.seqOverride = -1;
            snapshot.inStageIndex = -1;
            snapshot.attachMode = (int)AttachModes.SRF_ATTACH;
            snapshot.attached = true;
            snapshot.connected = true;
            snapshot.flagURL = fromPart.flagURL;

            Part newPart = snapshot.Load(fromPart.vessel, false);

            newPart.transform.position = position;
            newPart.transform.rotation = rotation;
            newPart.missionID = fromPart.missionID;

            fromPart.vessel.Parts.Add(newPart);

            newPart.physicalSignificance = Part.PhysicalSignificance.NONE;
            newPart.PromoteToPhysicalPart();
            newPart.Unpack();
            newPart.InitializeModules();

            if (coupleToPart)
            {
                newPart.rigidbody.velocity = coupleToPart.rigidbody.velocity;
                newPart.rigidbody.angularVelocity = coupleToPart.rigidbody.angularVelocity;
            }
            else
            {
                if (fromPart.rigidbody)
                {
                    newPart.rigidbody.velocity = fromPart.rigidbody.velocity;
                    newPart.rigidbody.angularVelocity = fromPart.rigidbody.angularVelocity;
                }
                else
                {
                    // If fromPart is a carried container
                    newPart.rigidbody.velocity = fromPart.vessel.rootPart.rigidbody.velocity;
                    newPart.rigidbody.angularVelocity = fromPart.vessel.rootPart.rigidbody.angularVelocity;
                }
            }

            newPart.decouple();

            if (coupleToPart)
            {
                newPart.StartCoroutine(WaitAndCouple(newPart, coupleToPart, srcAttachNodeID, tgtAttachNode, onPartCoupled));
            }
            else
            {
                newPart.vessel.vesselType = VesselType.Unknown;
                //name container
                ModuleKISInventory inv = newPart.GetComponent<ModuleKISInventory>();
                if (inv)
                {
                    if (inv.invName != "")
                    {
                        newPart.vessel.vesselName = inv.part.partInfo.title + " | " + inv.invName;
                    }
                    else
                    {
                        newPart.vessel.vesselName = inv.part.partInfo.title;
                    }
                }
            }
            return newPart;
        }
Ejemplo n.º 3
0
        public static Part CreatePart(ConfigNode partConfig, Vector3 position, Quaternion rotation,
                                      Part fromPart, Part coupleToPart = null,
                                      string srcAttachNodeID           = null, AttachNode tgtAttachNode = null,
                                      OnPartCoupled onPartCoupled      = null)
        {
            var node_copy = new ConfigNode();

            partConfig.CopyTo(node_copy);
            var snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame);

            if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) ||
                snapshot.flightID == 0)
            {
                snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
            }
            snapshot.parentIdx           = 0;
            snapshot.position            = position;
            snapshot.rotation            = rotation;
            snapshot.stageIndex          = 0;
            snapshot.defaultInverseStage = 0;
            snapshot.seqOverride         = -1;
            snapshot.inStageIndex        = -1;
            snapshot.attachMode          = (int)AttachModes.SRF_ATTACH;
            snapshot.attached            = true;
            snapshot.connected           = true;
            snapshot.flagURL             = fromPart.flagURL;

            Part newPart = snapshot.Load(fromPart.vessel, false);

            newPart.transform.position = position;
            newPart.transform.rotation = rotation;
            newPart.missionID          = fromPart.missionID;

            fromPart.vessel.Parts.Add(newPart);

            newPart.physicalSignificance = Part.PhysicalSignificance.NONE;
            newPart.PromoteToPhysicalPart();
            newPart.Unpack();
            newPart.InitializeModules();

            if (coupleToPart)
            {
                newPart.Rigidbody.velocity        = coupleToPart.Rigidbody.velocity;
                newPart.Rigidbody.angularVelocity = coupleToPart.Rigidbody.angularVelocity;
            }
            else
            {
                if (fromPart.Rigidbody)
                {
                    newPart.Rigidbody.velocity        = fromPart.Rigidbody.velocity;
                    newPart.Rigidbody.angularVelocity = fromPart.Rigidbody.angularVelocity;
                }
                else
                {
                    // If fromPart is a carried container
                    newPart.Rigidbody.velocity        = fromPart.vessel.rootPart.Rigidbody.velocity;
                    newPart.Rigidbody.angularVelocity = fromPart.vessel.rootPart.Rigidbody.angularVelocity;
                }
            }

            // New part by default is coupled with the active vessel.
            newPart.decouple();

            if (coupleToPart)
            {
                newPart.StartCoroutine(WaitAndCouple(newPart, coupleToPart, srcAttachNodeID,
                                                     tgtAttachNode, onPartCoupled));
            }
            else
            {
                RenameAssemblyVessel(newPart);
            }
            return(newPart);
        }
Ejemplo n.º 4
0
        private static IEnumerator WaitAndCouple(Part newPart, Part tgtPart  = null,
                                                 string srcAttachNodeID      = null,
                                                 AttachNode tgtAttachNode    = null,
                                                 OnPartCoupled onPartCoupled = null)
        {
            // Get relative position & rotation
            Vector3    toPartLocalPos = Vector3.zero;
            Quaternion toPartLocalRot = Quaternion.identity;

            if (tgtPart)
            {
                if (tgtAttachNode == null)
                {
                    // Local position & rotation from part
                    toPartLocalPos = tgtPart.transform.InverseTransformPoint(newPart.transform.position);
                    toPartLocalRot =
                        Quaternion.Inverse(tgtPart.transform.rotation) * newPart.transform.rotation;
                }
                else
                {
                    // Local position & rotation from node (KAS winch connector)
                    toPartLocalPos =
                        tgtAttachNode.nodeTransform.InverseTransformPoint(newPart.transform.position);
                    toPartLocalRot =
                        Quaternion.Inverse(tgtAttachNode.nodeTransform.rotation) * newPart.transform.rotation;
                }
            }

            // Wait part to initialize
            while (!newPart.started && newPart.State != PartStates.DEAD)
            {
                Logger.logInfo("CreatePart - Waiting initialization of the part...");
                if (tgtPart)
                {
                    // Part stay in position
                    if (tgtAttachNode == null)
                    {
                        newPart.transform.position = tgtPart.transform.TransformPoint(toPartLocalPos);
                        newPart.transform.rotation = tgtPart.transform.rotation * toPartLocalRot;
                    }
                    else
                    {
                        newPart.transform.position = tgtAttachNode.nodeTransform.TransformPoint(toPartLocalPos);
                        newPart.transform.rotation = tgtAttachNode.nodeTransform.rotation * toPartLocalRot;
                    }
                }
                yield return(null);
            }
            // Part stay in position
            if (tgtAttachNode == null)
            {
                newPart.transform.position = tgtPart.transform.TransformPoint(toPartLocalPos);
                newPart.transform.rotation = tgtPart.transform.rotation * toPartLocalRot;
            }
            else
            {
                newPart.transform.position = tgtAttachNode.nodeTransform.TransformPoint(toPartLocalPos);
                newPart.transform.rotation = tgtAttachNode.nodeTransform.rotation * toPartLocalRot;
            }
            Logger.logInfo("CreatePart - Coupling part...");
            CouplePart(newPart, tgtPart, srcAttachNodeID, tgtAttachNode);

            if (onPartCoupled != null)
            {
                onPartCoupled(newPart, tgtPart, tgtAttachNode);
            }
        }
Ejemplo n.º 5
0
        public static Part CreatePart(ConfigNode partConfig, Vector3 position, Quaternion rotation, Part fromPart, Part coupleToPart = null, string srcAttachNodeID = null, AttachNode tgtAttachNode = null, OnPartCoupled onPartCoupled = null)
        {
            ConfigNode node_copy = new ConfigNode();

            partConfig.CopyTo(node_copy);
            ProtoPartSnapshot snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame);

            if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0)
            {
                snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
            }
            snapshot.parentIdx           = 0;
            snapshot.position            = position;
            snapshot.rotation            = rotation;
            snapshot.stageIndex          = 0;
            snapshot.defaultInverseStage = 0;
            snapshot.seqOverride         = -1;
            snapshot.inStageIndex        = -1;
            snapshot.attachMode          = (int)AttachModes.SRF_ATTACH;
            snapshot.attached            = true;
            snapshot.connected           = true;
            snapshot.flagURL             = fromPart.flagURL;

            Part newPart = snapshot.Load(fromPart.vessel, false);

            newPart.transform.position = position;
            newPart.transform.rotation = rotation;
            newPart.missionID          = fromPart.missionID;

            fromPart.vessel.Parts.Add(newPart);

            newPart.physicalSignificance = Part.PhysicalSignificance.NONE;
            newPart.PromoteToPhysicalPart();
            newPart.Unpack();
            newPart.InitializeModules();

            if (coupleToPart)
            {
                newPart.rigidbody.velocity        = coupleToPart.rigidbody.velocity;
                newPart.rigidbody.angularVelocity = coupleToPart.rigidbody.angularVelocity;
            }
            else
            {
                if (fromPart.rigidbody)
                {
                    newPart.rigidbody.velocity        = fromPart.rigidbody.velocity;
                    newPart.rigidbody.angularVelocity = fromPart.rigidbody.angularVelocity;
                }
                else
                {
                    // If fromPart is a carried container
                    newPart.rigidbody.velocity        = fromPart.vessel.rootPart.rigidbody.velocity;
                    newPart.rigidbody.angularVelocity = fromPart.vessel.rootPart.rigidbody.angularVelocity;
                }
            }

            newPart.decouple();

            if (coupleToPart)
            {
                newPart.StartCoroutine(WaitAndCouple(newPart, coupleToPart, srcAttachNodeID, tgtAttachNode, onPartCoupled));
            }
            else
            {
                newPart.vessel.vesselType = VesselType.Unknown;
                //name container
                ModuleKISInventory inv = newPart.GetComponent <ModuleKISInventory>();
                if (inv)
                {
                    if (inv.invName != "")
                    {
                        newPart.vessel.vesselName = inv.part.partInfo.title + " | " + inv.invName;
                    }
                    else
                    {
                        newPart.vessel.vesselName = inv.part.partInfo.title;
                    }
                }
            }
            return(newPart);
        }