protected virtual void HandleSkinnedMeshes(EquippableInventoryItem copy, CharacterEquipmentTypeBinder binder)
        {
            if (binder.equipTransform == null)
            {
                return;
            }

            if (replaceSkinnedMeshBones)
            {
                var itemSkinned = copy.GetComponentInChildren <SkinnedMeshRenderer>();
                if (itemSkinned != null)
                {
                    // It's a skinned mesh, combine the bones of this mesh with the player's bones to sync animations.
                    var playerSkinned = binder.equipTransform.GetComponentInParent <SkinnedMeshRenderer>();
                    if (playerSkinned != null)
                    {
                        itemSkinned.rootBone = binder.rootBone;

                        if (forceReplaceAllBones)
                        {
                            itemSkinned.bones = playerSkinned.bones;
                            DevdogLogger.LogVerbose("Force copied " + itemSkinned.bones.Length + " bones to new skinned mesh", itemSkinned);
                        }
                        else
                        {
                            ReplaceBonesOnTarget(playerSkinned, itemSkinned);
                        }
                    }
                    else
                    {
                        DevdogLogger.LogWarning("Tried to equip skinned item to player, but no skinned mesh renderer found in equip position's parent.", binder.equipTransform);
                    }
                }
            }
        }
Beispiel #2
0
        public override EquippableInventoryItem Equip(EquippableInventoryItem item, CharacterEquipmentTypeBinder binder, bool createCopy)
        {
            EquippableInventoryItem copy = item;

            if (createCopy)
            {
                copy = CreateDefaultCopy(item);
            }

            copy.transform.SetParent(binder.equipTransform.parent); // Same level

            //GameObject p = GameObject.FindGameObjectWithTag("Player");
            //Debug.Log(p.name);
            //p.GetComponent<PlayerMovement>().UpdateEquippedGear();

            copy.transform.SetSiblingIndex(binder.equipTransform.GetSiblingIndex());
            binder.equipTransform.gameObject.SetActive(false); // Swap the item by disabling the original

            copy.transform.localPosition = copy.equipmentPosition;
            copy.transform.localRotation = copy.equipmentRotation;

            HandleSkinnedMeshes(copy, binder);
            HandleClothMeshes(copy, binder);

            return(copy);
        }
Beispiel #3
0
        /// <summary>
        /// Some item's require multiple slots, for example a 2 handed item forces the left handed item to be empty.
        /// </summary>
        /// <returns>true if items were removed, false if items were not removed.</returns>
        protected virtual bool HandleLocks(EquippableSlot equipSlot, EquippableInventoryItem equippable)
        {
            var toBeRemoved = new List <uint>(8);

            // Loop through things we want to block
            foreach (var blockType in equippable.equipmentType.blockTypes)
            {
                // Check every slot against this block type
                foreach (var field in equippableSlots)
                {
                    var item = items[field.index].item;
                    if (item != null)
                    {
                        var eq = (EquippableInventoryItem)item;
                        if (eq.equipmentType == blockType && field.index != equipSlot.index)
                        {
                            toBeRemoved.Add(field.index);
                            bool canAdd = InventoryManager.CanAddItem(eq);
                            if (canAdd == false)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            foreach (uint i in toBeRemoved)
            {
                bool added = InventoryManager.AddItemAndRemove(items[i].item);
                Assert.IsTrue(added, "Item could not be saved, even after check, please report this bug + stacktrace.");
            }

            return(true);
        }
 public override void UnEquipItemVisually(EquippableInventoryItem item)
 {
     if (item.visuallyEquippedToBinder != null)
     {
         UnEquipItemVisually(item, item.visuallyEquippedToBinder);
     }
 }
 protected virtual void UnEquipItemVisually(EquippableInventoryItem item, CharacterEquipmentTypeBinder binder)
 {
     if (binder != null && binder.currentItem != null)
     {
         var t = GetEquippableTypeFromItem(binder, item);
         t.equipmentHandler.UnEquip(binder, true);
         NotifyItemUnEquippedVisually(binder, item);
     }
 }
        private void NotifyItemEquippedVisually(CharacterEquipmentTypeBinder binder, EquippableInventoryItem item)
        {
            Assert.IsNotNull(item);
            Assert.IsNotNull(binder);

            if (OnEquippedVisually != null)
            {
                OnEquippedVisually(binder, item);
            }

            item.NotifyItemEquippedVisually(binder);
        }
Beispiel #7
0
        /// <summary>
        /// Get all slots where this item can be equipped.
        /// </summary>
        /// <param name="item"></param>
        /// <returns>Returns indices where the item can be equipped. collection[index] ... </returns>
        public EquippableSlot[] GetEquippableSlots(EquippableInventoryItem item)
        {
            if (item.equipmentType == null)
            {
                Debug.LogWarning("The item " + item.name + " you're trying to equip has no equipment type set. Item cannot be equipped and will be ingored.", item.gameObject);
                return(new EquippableSlot[0]);
            }

            if (this.equippableSlots.Length == 0)
            {
                Debug.LogWarning("This characterUI has no equipSlotFields, need to define some??", gameObject);
                return(new EquippableSlot[0]);
            }

            var equipSlots = new List <EquippableSlot>(4);

            foreach (var field in this.equippableSlots)
            {
                // Disabled fields are ignored.
                if (field.gameObject.activeSelf == false || field.enabled == false)
                {
                    continue;
                }

                foreach (var type in field.equipmentTypes)
                {
                    if (item.equipmentType == type)
                    {
                        bool canAdd = true;
                        // Can the item be equipped considering the usage requirement properties?
                        foreach (var prop in item.usageRequirement)
                        {
                            canAdd = prop.CanUse(character);
                            if (canAdd == false)
                            {
                                break;
                            }
                        }

                        if (canAdd)
                        {
                            equipSlots.Add(field);
                        }
                    }
                }
            }

            return(equipSlots.ToArray());
        }
Beispiel #8
0
        public bool EquipItem(EquippableSlot equipSlot, EquippableInventoryItem item)
        {
            Assert.IsNotNull(item);

            bool handled = HandleLocks(equipSlot, item);

            if (handled == false)
            {
                return(false);
            }

            // The values before the collection / slot changed.
            uint fromIndex      = item.index;
            var  fromCollection = item.itemCollection;

            // There was already an item in this slot, un-equip that one first
            if (items[equipSlot.index].item != null)
            {
                bool unEquipped = UnEquipItem((EquippableInventoryItem)items[equipSlot.index].item, true);
                if (unEquipped == false)
                {
                    return(false);
                }
            }

            // EquippedItem the item -> Will swap as merge is not possible
            bool canSet = CanSetItem(equipSlot.index, item);

            if (canSet)
            {
                bool set = SetItem(equipSlot.index, item);
                if (set && fromCollection != null)
                {
                    if (useReferences == false)
                    {
                        fromCollection.SetItem(fromIndex, null);
                        fromCollection.NotifyItemRemoved(item, item.ID, fromIndex, item.currentStackSize);
                        fromCollection[fromIndex].Repaint();
                    }
                }

                NotifyItemAdded(item, item.currentStackSize, true);
                items[equipSlot.index].Repaint();

                return(true);
            }

            return(true);
        }
        public override EquippableSlot FindEquippableSlotForItem(EquippableInventoryItem equippable)
        {
            if (characterCollection.useReferences)
            {
                foreach (var slot in characterCollection.equippableSlots)
                {
                    if (slot.slot.item == equippable)
                    {
                        return(characterCollection.equippableSlots[slot.index]);
                    }
                }
            }

            return(characterCollection.equippableSlots.FirstOrDefault(o => o.equipmentTypes.Contains(equippable.equipmentType)));
        }
//        [SerializeField]
//        protected bool copySphereColliders;

        protected EquippableInventoryItem CreateDefaultCopy(EquippableInventoryItem item)
        {
            var copy = Instantiate <EquippableInventoryItem>(item);

            // Remove the default components, to prevent the user from looting an equipped item.
            Destroy(copy.GetComponent <ITriggerInputHandler>() as UnityEngine.Component);
            Destroy(copy.GetComponent <TriggerBase>());
            Destroy(copy.GetComponent <InventoryItemBase>());

            var rigid = copy.gameObject.GetComponent <Rigidbody>();

            if (rigid != null)
            {
                rigid.isKinematic = true;
            }

            var rigid2D = copy.gameObject.GetComponent <Rigidbody2D>();

            if (rigid2D != null)
            {
                rigid2D.isKinematic = true;
            }

            var cols = copy.gameObject.GetComponentsInChildren <Collider>(true);

            foreach (var col in cols)
            {
                col.isTrigger = true;
            }

            var cols2D = copy.gameObject.GetComponentsInChildren <Collider2D>(true);

            foreach (var col2D in cols2D)
            {
                col2D.isTrigger = true;
            }

            copy.gameObject.SetActive(true);
            InventoryUtility.SetLayerRecursive(copy.gameObject, InventorySettingsManager.instance.settings.equipmentLayer);

            if (resizeItemsToScale1)
            {
                copy.transform.localScale = Vector3.one;
            }

            return(copy);
        }
        public override EquippableInventoryItem Equip(EquippableInventoryItem item, CharacterEquipmentTypeBinder binder, bool createCopy)
        {
            EquippableInventoryItem copy = item;

            if (createCopy)
            {
                copy = CreateDefaultCopy(item);
            }

            copy.transform.SetParent(binder.equipTransform);
            copy.transform.localPosition = copy.equipmentPosition;
            copy.transform.localRotation = copy.equipmentRotation;

            HandleSkinnedMeshes(copy, binder);
            HandleClothMeshes(copy, binder);

            return(copy);
        }
        protected virtual void HandleClothMeshes(EquippableInventoryItem copy, CharacterEquipmentTypeBinder binder)
        {
            if (binder.equipTransform == null)
            {
                return;
            }

            var cloth = copy.GetComponent <Cloth>();

            if (cloth != null)
            {
                if (copyCapsuleColliders)
                {
                    cloth.capsuleColliders = binder.equipTransform.GetComponentsInParent <CapsuleCollider>();
                }

                cloth.ClearTransformMotion();
            }
        }
        public override void EquipItemVisually(EquippableInventoryItem item, EquippableSlot slot)
        {
            if (item.equipVisually == false)
            {
                return;
            }

            var binder = FindEquipmentLocation(slot);

            if (binder != null)
            {
                var t = GetEquippableTypeFromItem(binder, item);
                item.visuallyEquippedToBinder = binder;
                var copy = t.equipmentHandler.Equip(item, binder, true);

                binder.currentItem = copy.gameObject;
                NotifyItemEquippedVisually(binder, copy);
            }
        }
        public override EquippableInventoryItem Equip(EquippableInventoryItem item, CharacterEquipmentTypeBinder binder, bool createCopy)
        {
            EquippableInventoryItem copy = item;
            if (createCopy)
            {
                copy = CreateDefaultCopy(item);
            }

            copy.transform.SetParent(binder.equipTransform.parent); // Same level
            copy.transform.SetSiblingIndex(binder.equipTransform.GetSiblingIndex());
            binder.equipTransform.gameObject.SetActive(false); // Swap the item by disabling the original

            copy.transform.localPosition = copy.equipmentPosition;
            copy.transform.localRotation = copy.equipmentRotation;

            HandleSkinnedMeshes(copy, binder);
            HandleClothMeshes(copy, binder);

            return copy;
        }
Beispiel #15
0
        public bool UnEquipItem(EquippableInventoryItem item, bool addToInventory)
        {
            Assert.IsNotNull(item);
            Assert.IsTrue(item.isEquipped, "Trying to unEquip item, but item wasn't equipped in the first place!");
            Assert.IsTrue(item.itemCollection == this || useReferences, "Trying to un-equip an item that wasn't equipped in this collection!");

            if (item.CanUnEquip(addToInventory) == false)
            {
                return(false);
            }

            if (useReferences == false)
            {
                if (addToInventory)
                {
                    bool added = InventoryManager.AddItemAndRemove(item);
                    Assert.IsTrue(added);
                    return(added);
                }

                SetItem(item.index, null);
                NotifyItemRemoved(item, item.ID, item.index, item.currentStackSize);
            }
            else
            {
                foreach (var wrapper in items)
                {
                    if (wrapper.item == item)
                    {
                        SetItem(wrapper.index, null, true);
                        NotifyItemRemoved(item, item.ID, wrapper.index, item.currentStackSize);
                    }
                }
            }

            return(true);
        }
 public abstract void EquipItemVisually(EquippableInventoryItem item, EquippableSlot specificSlot);
 public abstract void UnEquipItemVisually(EquippableInventoryItem item);
 public abstract EquippableInventoryItem Equip(EquippableInventoryItem item, CharacterEquipmentTypeBinder binder, bool createCopy);
        public override void EquipItemVisually(EquippableInventoryItem item)
        {
            var slot = FindEquippableSlotForItem(item);

            EquipItemVisually(item, slot);
        }
 private EquipmentType GetEquippableTypeFromItem(CharacterEquipmentTypeBinder binder, EquippableInventoryItem item)
 {
     return(binder.equippableSlot.equipmentTypes.FirstOrDefault(o => o == item.equipmentType));
 }
 public abstract EquippableSlot FindEquippableSlotForItem(EquippableInventoryItem equippable);