Beispiel #1
0
 public void ApplyTweak(GameObject modelObj)
 {
     if (avPart == null)
     {
         return;
     }
     evaTransform = KISAddonConfig.FindEquipBone(modelObj.transform, itemModule.equipBoneName);
     if (evaTransform != null)
     {
         var partModel = Hierarchy.GetPartModelTransform(avPart.partPrefab);
         equippedGameObj = UnityEngine.Object.Instantiate(partModel.gameObject);
         Hierarchy.MoveToParent(equippedGameObj.transform, evaTransform);
         DebugEx.Info("Equipped part on kerbal model in main screen: {0}", avPart.name);
     }
     else
     {
         DebugEx.Error("Failed finding model transforms for part {0}", avPart.name);
     }
 }
Beispiel #2
0
        public void Equip(ActorType actorType = ActorType.API)
        {
            // Only equip EVA kerbals.
            if (!prefabModule || inventory.invType != ModuleKISInventory.InventoryType.Eva)
            {
                DebugEx.Warning("Cannot equip item from inventory type: {0}", inventory.invType);
                return;
            }
            if (quantity > 1)
            {
                ScreenMessaging.ShowPriorityScreenMessage(CannotEquipItemStackedMsg);
                UISounds.PlayBipWrong();
                return;
            }
            DebugEx.Info("Equip item: partName={0}, mode={1}", availablePart.title, equipMode);

            // Check if the skill is needed. Skip the check in the sandbox modes.
            if (HighLogic.CurrentGame.Mode != Game.Modes.SANDBOX &&
                HighLogic.CurrentGame.Mode != Game.Modes.SCIENCE_SANDBOX &&
                !String.IsNullOrEmpty(prefabModule.equipSkill))
            {
                bool skillFound = false;
                List <ProtoCrewMember> protoCrewMembers = inventory.vessel.GetVesselCrew();
                foreach (var expEffect in protoCrewMembers[0].experienceTrait.Effects)
                {
                    if (expEffect.ToString().Replace("Experience.Effects.", "") == prefabModule.equipSkill)
                    {
                        skillFound = true;
                        break;
                    }
                }
                if (!skillFound)
                {
                    if (actorType == ActorType.Player)
                    {
                        ScreenMessaging.ShowPriorityScreenMessage(
                            CannotEquipRestrictedToSkillMsg.Format(prefabModule.equipSkill));
                        UISounds.PlayBipWrong();
                    }
                    return;
                }
            }

            // Check if slot is already occupied.
            if (equipSlot != null)
            {
                KIS_Item equippedItem = inventory.GetEquipedItem(equipSlot);
                if (equippedItem != null && equippedItem != this)
                {
                    if (equippedItem.carriable && actorType == ActorType.Player)
                    {
                        ScreenMessaging.ShowPriorityScreenMessage(
                            CannotEquipAlreadyCarryingMsg.Format(equipSlot, equippedItem.availablePart.title));
                        UISounds.PlayBipWrong();
                        return;
                    }
                    equippedItem.Unequip();
                }
            }

            // Find the bone for this item to follow.
            evaTransform =
                KISAddonConfig.FindEquipBone(inventory.part.transform, prefabModule.equipBoneName);
            if (evaTransform == null)
            {
                return; // Cannot equip!
            }
            if (equipMode == EquipMode.Model)
            {
                var modelGo = availablePart.partPrefab.FindModelTransform("model").gameObject;
                equippedGameObj = UnityEngine.Object.Instantiate(modelGo);
                equippedGameObj.transform.parent = inventory.part.transform;
                foreach (Collider col in equippedGameObj.GetComponentsInChildren <Collider>())
                {
                    UnityEngine.Object.DestroyImmediate(col);
                }
            }
            else
            {
                var alreadyEquippedPart = inventory.part.FindChildPart(availablePart.name);
                if (alreadyEquippedPart)
                {
                    DebugEx.Info("Part {0} already found on eva, use it as the item", availablePart.name);
                    equippedPart = alreadyEquippedPart;
                    // This magic is copied from the KervalEVA.OnVesselGoOffRails() method.
                    // There must be at least 3 fixed frames delay before updating the colliders.
                    AsyncCall.WaitForPhysics(
                        equippedPart, 3, () => false,
                        failure: () => OnEquippedPartReady(equippedPart));
                    if (equipMode == EquipMode.Part)
                    {
                        // Ensure the part doesn't have rigidbody and is not affected by physics.
                        // The part may not like it.
                        equippedPart.PhysicsSignificance = 1; // Disable physics on the part.
                        UnityEngine.Object.Destroy(equippedPart.rb);
                    }
                }
                else
                {
                    Vector3    equipPos = evaTransform.TransformPoint(prefabModule.equipPos);
                    Quaternion equipRot = evaTransform.rotation * Quaternion.Euler(prefabModule.equipDir);
                    equippedPart = KIS_Shared.CreatePart(
                        partNode, equipPos, equipRot, inventory.part,
                        coupleToPart: inventory.part,
                        srcAttachNodeId: "srfAttach",
                        onPartReady: OnEquippedPartReady,
                        createPhysicsless: equipMode != EquipMode.Physic);
                }
                if (equipMode == EquipMode.Part)
                {
                    equippedGameObj = equippedPart.gameObject;
                }
            }

            // Hide the stock meshes if the custom helmet is equipped.
            if (equipSlot == HelmetSlotName)
            {
                var kerbalModule = inventory.part.FindModuleImplementing <KerbalEVA>();
                if (kerbalModule.helmetTransform != null)
                {
                    for (var i = 0; i < kerbalModule.helmetTransform.childCount; i++)
                    {
                        kerbalModule.helmetTransform.GetChild(i).gameObject.SetActive(false);
                    }
                    if (equippedGameObj != null)
                    {
                        equippedGameObj.transform.parent = kerbalModule.helmetTransform;
                    }
                }
                else
                {
                    DebugEx.Warning("Kerbal model doesn't have helmet transform: {0}", inventory);
                }
            }

            if (actorType == ActorType.Player)
            {
                UISoundPlayer.instance.Play(prefabModule.moveSndPath);
            }
            equipped = true;
            prefabModule.OnEquip(this);
            inventory.StartCoroutine(AlignEquippedPart());
        }