Пример #1
0
        public void UnequipModularPart(Item_ModularBodyPart modularBodyPart)
        {
            // ===============================================================================================
            // Get default part
            // ===============================================================================================
            var modularMetadata = ResourcesLoader.Instance.LoadModularBodyMetadata();
            var defaults        = modularMetadata.objs
                                  .Where(x => x.Gender == modularBodyPart.Gender &&
                                         x.IsDefault == true);

            Item_ModularBodyPart_Metadata?defaultPart = null;
            Item_Armor      armor      = null;
            Item_Attachment attachment = null;

            if (modularBodyPart is Item_Armor)
            {
                armor       = modularBodyPart as Item_Armor;
                defaultPart = defaults.FirstOrDefault(x => x.ArmorType == armor.Type);
            }
            else
            {
                attachment  = modularBodyPart as Item_Attachment;
                defaultPart = defaults.FirstOrDefault(x => x.AttachmentType == attachment.Type);
            }

            if (defaultPart == null || defaultPart.HasValue == false)
            {
                string type = armor == null?armor.Type.ToString() : attachment.Type.ToString();

                Debug.Log($"Faile to unequip. Default part was not found!! ItemType : {modularBodyPart.ItemType} | Type : {type}");
                return;
            }

            // ===============================================================================================
            // Split the path to the Transform part
            // ===============================================================================================
            var splitCurrentEquippedPart = modularBodyPart.PartLocation.Split('/').ToList();
            var splitDefaultPart         = string.IsNullOrEmpty(defaultPart.Value.PartLocation) ?
                                           null : defaultPart.Value.PartLocation.Split('/').ToList();

            splitCurrentEquippedPart.RemoveAt(0);
            splitDefaultPart?.RemoveAt(0);

            // ===============================================================================================
            // Get the equip part by searching it through child
            // ===============================================================================================
            var portraitPart = portraitTransform;
            var ownerPart    = UnitTransform;

            var defaultPortaitPart = portraitTransform;
            var defaultOwnerPart   = ownerPart;

            while (splitCurrentEquippedPart.Count > 0)
            {
                portraitPart = GetChildTransformWithName(portraitPart, splitCurrentEquippedPart[0]);
                ownerPart    = GetChildTransformWithName(ownerPart, splitCurrentEquippedPart[0]);
                splitCurrentEquippedPart.RemoveAt(0);

                if (splitDefaultPart != null)
                {
                    defaultPortaitPart = GetChildTransformWithName(defaultPortaitPart, splitDefaultPart[0]);
                    defaultOwnerPart   = GetChildTransformWithName(defaultOwnerPart, splitDefaultPart[0]);

                    splitDefaultPart.RemoveAt(0);
                }
            }

            // ===============================================================================================
            // Finally, activate the equip
            // ===============================================================================================
            ownerPart.gameObject.SetActive(false);
            portraitPart?.gameObject.SetActive(false);
            if (splitDefaultPart != null)
            {
                defaultPortaitPart?.gameObject.SetActive(true);
                defaultOwnerPart.gameObject.SetActive(true);
            }

            // ===============================================================================================
            // Register equip item to dictionary
            // ===============================================================================================
            Item_Equipment oldEquipment = null;

            if (modularBodyPart is Item_Attachment)
            {
                var itemAttachment = modularBodyPart as Item_Attachment;
                oldEquipment = itemAttachment;
                equippedAttachmentDic[itemAttachment.Type] = null;
            }
            else if (modularBodyPart is Item_Armor)
            {
                var itemArmor = modularBodyPart as Item_Armor;
                oldEquipment = itemArmor;
                equippedArmorDic[itemArmor.Type] = null;
            }

            // ===============================================================================================
            // Fire on unequipped event
            // ===============================================================================================
            OnUnequippedEvent.Invoke(oldEquipment);
        }
Пример #2
0
        public void EquipModularPart(Item_ModularBodyPart modularPart)
        {
            // ===============================================================================================
            // Split the path to the Transform part
            // ===============================================================================================
            var split = modularPart.PartLocation.Split('/').ToList();

            split.RemoveAt(0);

            // ===============================================================================================
            // Get the equip part by searching it through child
            // ===============================================================================================
            var portraitPart = portraitTransform;
            var ownerPart    = UnitTransform;

            while (split.Count > 0)
            {
                portraitPart = GetChildTransformWithName(portraitPart, split[0]);
                ownerPart    = GetChildTransformWithName(ownerPart, split[0]);
                split.RemoveAt(0);
            }

            // ===============================================================================================
            // Deactivate all parts with same type as the equip item
            // ===============================================================================================
            var portaitPartParent = portraitPart?.parent;
            var ownerPartParent   = ownerPart.parent;

            for (int i = 0; i < ownerPartParent.childCount; i++)
            {
                if (ownerPartParent.GetChild(i) == ownerPart)
                {
                    continue;
                }

                portaitPartParent?.GetChild(i).gameObject.SetActive(false);
                ownerPartParent.GetChild(i).gameObject.SetActive(false);
            }

            // ===============================================================================================
            // Finally, activate the equip
            // ===============================================================================================
            ownerPart.gameObject.SetActive(true);
            portraitPart?.gameObject.SetActive(true);

            // ===============================================================================================
            // Register equip item to dictionary
            // ===============================================================================================
            Item_Equipment oldEquipment = null;

            if (modularPart is Item_Armor)
            {
                var itemArmor = modularPart as Item_Armor;
                oldEquipment = equippedArmorDic[itemArmor.Type];
                equippedArmorDic[itemArmor.Type] = itemArmor;
            }
            else if (modularPart is Item_Attachment)
            {
                var itemAttachment = modularPart as Item_Attachment;
                oldEquipment = equippedAttachmentDic[itemAttachment.Type];
                equippedAttachmentDic[itemAttachment.Type] = itemAttachment;
            }

            // ===============================================================================================
            // Fire on equipped event (left hand is old, right hand is new)
            // ===============================================================================================
            OnEquippedEvent.Invoke(oldEquipment, modularPart);
        }