private void OnUnstackedItem(ItemCollectionBase fromColl, uint startslot, ItemCollectionBase toCollection, uint endslot, uint amount)
        {
            toArr[startslot] = fromColl[startslot].item;

            if (fromColl == toCollection)
                toArr[endslot] = fromColl[endslot].item;
        }
        public override bool MoveItem(InventoryItemBase item, uint fromSlot, ItemCollectionBase toCollection, uint toSlot, bool clearOld, bool doRepaint = true)
        {
            if (item == null)
                return true;

            if (this != toCollection)
            {
                // Moving to another collection       
                var bag = item as BagInventoryItem;
                if (bag == null)
                    return false;

                if (toCollection[toSlot].item != null)
                    return false; // Slot is not empty, swap should have been called?

                toCollection.SetItem(toSlot, item); // Temp set it
                bool canMove = extendingCollection.CanRemoveSlots(bag.extendInventoryBySlots); // Check if allowed
                toCollection.SetItem(toSlot, null); // Remove

                if (canMove == false)
                    return false;
            }
            
            return base.MoveItem(item, fromSlot, toCollection, toSlot, clearOld, doRepaint);
        }
        public override void TriggerDrop(bool useRaycast = true)
        {
            if (item == null || itemCollection.canDropFromCollection == false)
            {
                return;
            }

            if (item.isDroppable == false)
            {
                InventoryManager.instance.lang.itemCannotBeDropped.Show(item.name, item.description);
                return;
            }

            Vector3    dropPosition = InventorySettingsManager.instance.playerObject.transform.position;
            Ray        ray          = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, InventorySettingsManager.instance.maxDropDistance,
                                InventorySettingsManager.instance.layersWhenDropping))
            {
                dropPosition = hit.point;
            }
            else
            {
                return; // Couldn't drop item
            }

            var s = InventorySettingsManager.instance;

            if (useRaycast && s.showConfirmationDialogWhenDroppingItem && s.showConfirmationDialogMinRarity.ID <= item.rarity.ID)
            {
                // Not on a button, drop it
                var tempItem = item; // Capture list stuff
                var msg      = InventoryManager.instance.lang.confirmationDialogDrop;
                s.confirmationDialog.ShowDialog(msg.title, msg.message, s.defaultDialogPositiveButtonText, s.defaultDialogNegativeButtonText, item,
                                                (dialog) =>
                {
                    ItemCollectionBase startCollection = tempItem.itemCollection;
                    uint startIndex = tempItem.index;

                    var d = tempItem.Drop(dropPosition);
                    if (d != null)
                    {
                        startCollection[startIndex].Repaint();
                    }
                },
                                                (dialog) =>
                {
                    //Debug.Log("No clicked");
                });
            }
            else
            {
                var d = item.Drop(dropPosition);
                if (d != null)
                {
                    Repaint();
                }
            }
        }
Example #4
0
 public static void Drag(InventoryUIItemWrapper toDrag, uint startSlot, ItemCollectionBase handler, PointerEventData eventData)
 {
     if (eventData.button == PointerEventData.InputButton.Left)
     {
         draggingItem.transform.position = new Vector3(eventData.position.x, eventData.position.y, 0.0f);
     }
 }
        public override bool MoveItem(InventoryItemBase item, uint fromSlot, ItemCollectionBase toCollection, uint toSlot, bool clearOld, bool doRepaint = true)
        {
            if (item == null)
                return true;

            // No moving inside own collection
            if (this == toCollection)
                return false;

            var bag = (BagInventoryItem)item;
            if (toCollection[toSlot].item == null)
            {
                bool set = toCollection.SetItem(toSlot, item);
                if (set && InventoryManager.instance.inventory.CanRemoveSlots(bag.extendInventoryBySlots) == false)
                {
                    toCollection.SetItem(toSlot, null); // And reset
                    return false;
                }
            }

            bool moved = base.MoveItem(item, fromSlot, toCollection, toSlot, clearOld, doRepaint);
            if (moved == false)
                return false;

            if (toCollection != this)
            {
                bool unequip = bag.Unequip();
                if (unequip)
                {
                    SetItem(fromSlot, null);
                }
            }

            return true;
        }
        public CollectionToArraySyncer(ItemCollectionBase fromCollection, InventoryItemBase[] toArr)
        {
            this.fromCollection = fromCollection;
            this.toArr = toArr;

            RegisterEvents();
        }
        public static InventoryUIDragLookup BeginDrag(InventoryUIItemWrapper toDrag, uint startIndex, ItemCollectionBase collection, PointerEventData eventData)
        {
            if (draggingItem != null)
            {
                Debug.LogWarning("Item still attached to cursor, can only drag one item at a time", draggingItem.gameObject);
                return null; // Can only drag one item at a time
            }

            if (eventData.button != PointerEventData.InputButton.Left)
                return null;

            draggingItem = toDrag;
            //draggingButtonCollection = collection;

            // Canvas group allows object to ignore raycasts.
            CanvasGroup group = draggingItem.gameObject.GetComponent<CanvasGroup>();
            if(group == null)
                group = draggingItem.gameObject.AddComponent<CanvasGroup>();

            group.blocksRaycasts = false; // Allows rays to go through so we can hover over the empty slots.
            group.interactable = false;

            var lookup = new InventoryUIDragLookup();
            lookup.startIndex = (int)startIndex;
            lookup.startItemCollection = collection;

            return lookup;
        }
        public void LoadItems(ItemCollectionBase collection, Uri loadLocation, Action<byte[]> callback)
        {
            if (PlayerPrefs.HasKey("InventorySystem_" + collection.collectionName.ToLower().Replace(" ", "_")) == false)
                return; // Don't handle the callback, no data found.

            string data = PlayerPrefs.GetString("InventorySystem_" + collection.collectionName.ToLower().Replace(" ", "_"));
            callback(System.Text.Encoding.UTF8.GetBytes(data));
        }
        public void Reset()
        {
            startIndex = -1;
            startItemCollection = null;

            endIndex = -1;
            endItemCollection = null;
        }
        private void OnSwappedItems(ItemCollectionBase from, uint fromSlot, ItemCollectionBase to, uint toSlot)
        {
            if (from == fromCollection)
                toArr[fromSlot] = fromCollection[fromSlot].item;

            if (to == fromCollection)
                toArr[toSlot] = fromCollection[toSlot].item;

        }
        // Use this for initialization
        public virtual void Awake()
        {
            window = GetComponent<UIWindow>();
            collection = GetComponent<ItemCollectionBase>();

            collection.OnAddedItem += (items, amount, fromCollection) => CollectionChanged();
            collection.OnRemovedItem += (item, id, slot, amount) => CollectionChanged();
            collection.OnUsedItem += (item, id, slot, amount) => CollectionChanged();

            window.OnShow += Repaint;
        }
Example #12
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>
        public virtual bool HandleLocks(InventoryEquippableField equipSlot, ItemCollectionBase usedFromCollection)
        {
            var toBeRemoved = new List <uint>(8);

            // Loop through things we want to block
            foreach (var blockType in equipType.blockTypes)
            {
                // Check every slot against this block type
                foreach (var field in InventoryManager.instance.character.equipSlotFields)
                {
                    var item = InventoryManager.instance.character[field.index].item;
                    if (item != null)
                    {
                        var eq = (EquippableInventoryItem)item;

                        if (eq.equipType.ID == blockType && field.index != equipSlot.index)
                        {
                            toBeRemoved.Add(field.index);
                            bool canAdd = InventoryManager.CanAddItem(eq);
                            if (canAdd == false)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            //// There was already an item in this slot, un-equip that one first
            //if (InventoryManager.instance.character[equipSlot.index].item != null)
            //{
            //    // TODO:  FIX THIS .. !
            //    toBeRemoved.Add(equipSlot.index);
            //}

            foreach (uint i in toBeRemoved)
            {
                var  item  = InventoryManager.instance.character[i].item as EquippableInventoryItem;
                bool added = InventoryManager.AddItemAndRemove(item);
                if (added == false)
                {
                    Debug.LogError("Item could not be saved, even after check, please report this bug + stacktrace.");
                    return(false);
                }

                item.NotifyItemUnEquipped();
                //InventoryManager.instance.character.SetItem(i, null);
                //InventoryManager.instance.character[i].Repaint();
            }

            return(true);
        }
        public virtual void Awake()
        {
            collection = GetComponent<ItemCollectionBase>();

            if (collection.useReferences)
            {
                StartCoroutine(WaitAndLoad());
            }
            else
            {
                collection.LoadEasySave2(fileName, additionalFields);
            }
        }
        public static InventoryItemSaveLookup[] GetCollectionLookups(ItemCollectionBase collection)
        {
            var l = new InventoryItemSaveLookup[collection.items.Length];
            for (int i = 0; i < collection.items.Length; i++)
            {
                if (collection.items[i].item == null)
                    l[i] = new InventoryItemSaveLookup(-1, 0);
                else
                    l[i] = new InventoryItemSaveLookup((int)collection.items[i].item.ID, collection.items[i].item.currentStackSize);                
            }

            return l;
        }
        public static InventoryItemReferenceSaveLookup[] GetCollectionReferenceLookups(ItemCollectionBase collection)
        {
            var l = new InventoryItemReferenceSaveLookup[collection.items.Length];
            for (int i = 0; i < collection.items.Length; i++)
            {
                if (collection.items[i].item == null)
                    l[i] = new InventoryItemReferenceSaveLookup(-1, 0, string.Empty);
                else
                    l[i] = new InventoryItemReferenceSaveLookup((int)collection.items[i].item.ID, collection.items[i].item.currentStackSize, collection.items[i].item.itemCollection.collectionName);
            }

            return l;
        }
        public override bool Equip(InventoryEquippableField equipSlot, ItemCollectionBase equipToCollection)
        {
            bool equipped = base.Equip(equipSlot, equipToCollection);
            if (equipped == false)
                return false;

            if (actor.actorClass.currLevel < requiredLevel)
            {
                InventoryManager.instance.lang.itemCannotBeUsedLevelToLow.Show(name, description, requiredLevel);
                return false;
            }

            SetPlyGameValues();

            return true;
        }
        public void TriggerAddCurrencyToCollection(ItemCollectionBase collection)
        {
            InventoryManager.instance.intValDialog.ShowDialog(transform, "Amount", "", 1, 9999, value =>
            {
                // Yes callback
                if (InventoryManager.instance.inventory.CanRemoveCurrency((float)value, currencyID, allowCurrencyConversions))
                {
                    InventoryManager.instance.inventory.RemoveCurrency(value, currencyID, allowCurrencyConversions);
                    toCollection.AddCurrency(value, currencyID);
                }

            }, value =>
            {
                // No callback

            }
            );
        }
Example #18
0
        public virtual bool Equip(InventoryEquippableField equipSlot, ItemCollectionBase equipToCollection)
        {
            bool handled = HandleLocks(equipSlot, itemCollection);

            if (handled == false)
            {
                return(false); // Other items cannot be unequipped
            }
            // There was already an item in this slot, un-equip that one first
            if (equipToCollection[equipSlot.index].item != null)
            {
                var  item      = equipToCollection[equipSlot.index].item as EquippableInventoryItem;
                bool addedItem = InventoryManager.AddItemAndRemove(item);
                if (addedItem == false)
                {
                    return(false); // Can't un-equip item to equip item..
                }
                item.NotifyItemUnEquipped();
            }

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

            // Equip the item -> Will swap as merge is not possible
            bool swapped = itemCollection.SwapOrMerge(index, equipToCollection, equipSlot.index);

            if (swapped)
            {
                NotifyItemUsed(1, false); // NOTE: Collection changed, collection - event no longer valid!
                fromCollection.NotifyItemUsed(ID, prevIndex, 1);

                if (fromCollection[prevIndex].item != null)
                {
                    ((EquippableInventoryItem)fromCollection[prevIndex].item).NotifyItemUnEquipped();
                }

                return(true);
            }

            return(false);
        }
        protected override void Awake()
        {
            base.Awake();

            currentClipCount = 0;
            OnEquipped += to =>
            {
                tempCollection = itemCollection;

                objectTriggererItemUfps.TryGiveToPlayer(InventoryPlayerManager.instance.currentPlayer.GetComponentInChildren<Collider>(), (int)currentClipCount);
                
                eventHandler.Register(this); // Enable UFPS events
            };
            OnUnEquipped += () =>
            {
                //eventHandler.RemoveItem.Try(new object[] { itemType });
                ufpsInventory.TryRemoveItem(itemType, 0);
                currentClipCount = (uint)eventHandler.CurrentWeaponAmmoCount.Get();

                tempCollection = itemCollection;
                eventHandler.Unregister(this); // Disable UFPS events            
            };
        }
        public virtual bool Equip(InventoryEquippableField equipSlot, ItemCollectionBase equipToCollection)
        {
            bool handled = HandleLocks(equipSlot, itemCollection);
            if (handled == false)
                return false; // Other items cannot be unequipped

            // There was already an item in this slot, un-equip that one first
            if (equipToCollection[equipSlot.index].item != null)
            {
                var item = equipToCollection[equipSlot.index].item as EquippableInventoryItem;
                bool addedItem = InventoryManager.AddItemAndRemove(item);
                if (addedItem == false)
                    return false; // Can't un-equip item to equip item..

                item.NotifyItemUnEquipped();
            }

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

            // Equip the item -> Will swap as merge is not possible
            bool swapped = itemCollection.SwapOrMerge(index, equipToCollection, equipSlot.index);
            if (swapped)
            {
                NotifyItemUsed(1, false); // NOTE: Collection changed, collection - event no longer valid!
                fromCollection.NotifyItemUsed(ID, prevIndex, 1);

                if (fromCollection[prevIndex].item != null)
                    ((EquippableInventoryItem)fromCollection[prevIndex].item).NotifyItemUnEquipped();

                return true;
            }

            return false;
        }
 public void LoadItems(ItemCollectionBase collection, Uri loadLocation, Action<byte[]> callback)
 {
     string data = PlayerPrefs.GetString("InventorySystem_" + collection.collectionName.ToLower().Replace(" ", "_"));
     callback(System.Text.Encoding.UTF8.GetBytes(data));
 }
 /// <summary>
 /// Check if a given collection is a loot to collection.
 /// </summary>
 /// <param name="collection"></param>
 /// <returns></returns>
 public static bool IsInventoryCollection(ItemCollectionBase collection)
 {
     return(lootToCollections.Any(col => col.collection == collection));
 }
 /// <summary>
 /// Add a collection that functions as an Inventory. Items will be looted to this collection.
 /// </summary>
 /// <param name="collection">The collection to add.</param>
 /// <param name="priority">
 /// How important is the collection, if you 2 collections can hold the item, which one should be chosen?
 /// Range of 0 to 100
 /// </param>
 public static void AddInventoryCollection(ItemCollectionBase collection, int priority)
 {
     lootToCollections.Add(new InventoryCollectionLookup(collection, priority));
 }
 public static void AddBankCollection(ItemCollectionBase collection)
 {
     bankCollections.Add(collection);
 }
Example #25
0
        public static InventoryUIDragLookup BeginDrag(InventoryUIItemWrapper toDrag, uint startIndex, ItemCollectionBase collection, PointerEventData eventData)
        {
            if (draggingItem != null)
            {
                Debug.LogWarning("Item still attached to cursor, can only drag one item at a time", draggingItem.gameObject);
                return(null); // Can only drag one item at a time
            }

            if (eventData.button != PointerEventData.InputButton.Left)
            {
                return(null);
            }


            draggingItem = toDrag;
            //draggingButtonCollection = collection;

            // Canvas group allows object to ignore raycasts.
            CanvasGroup group = draggingItem.gameObject.GetComponent <CanvasGroup>();

            if (group == null)
            {
                group = draggingItem.gameObject.AddComponent <CanvasGroup>();
            }

            group.blocksRaycasts = false; // Allows rays to go through so we can hover over the empty slots.
            group.interactable   = false;

            var lookup = new InventoryUIDragLookup();

            lookup.startIndex          = (int)startIndex;
            lookup.startItemCollection = collection;

            return(lookup);
        }
 private void OnSwappedItemsPly(ItemCollectionBase fromCollection, uint fromSlot, ItemCollectionBase toCollection, uint toSlot)
 {
     if (eventHandler != null)
         eventHandler.CollectionOnSwappedItems(fromCollection, fromSlot, toCollection, toSlot);
 }
        /// <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>
        public virtual bool HandleLocks(InventoryEquippableField equipSlot, ItemCollectionBase usedFromCollection, CharacterUI equipTo)
        {
            var toBeRemoved = new List<uint>(8);

            // Loop through things we want to block
            foreach (var blockType in equipType.blockTypes)
            {
                // Check every slot against this block type
                foreach (var field in equipTo.equipSlotFields)
                {
                    var item = equipTo[field.index].item;
                    if(item != null)
                    {
                        var eq = (EquippableInventoryItem)item;

                        if(eq.equipType.ID == blockType && field.index != equipSlot.index)
                        {
                            toBeRemoved.Add(field.index);
                            bool canAdd = InventoryManager.CanAddItem(eq);
                            if (canAdd == false)
                                return false;
                        }
                    }
                }
            }
            
            foreach (uint i in toBeRemoved)
            {
                var item = equipTo[i].item as EquippableInventoryItem;
                bool added = InventoryManager.AddItemAndRemove(item);
                if (added == false)
                {
                    Debug.LogError("Item could not be saved, even after check, please report this bug + stacktrace.");
                    return false;
                }
            }

            return true;
        }
        private void OnUnstackedItemPlayMaker(ItemCollectionBase fromCollection, uint startSlot, ItemCollectionBase toCollection, uint endSlot, uint amount)
        {

        }
        /// <summary>
        /// Unstack this item
        /// </summary>
        /// <param name="toCollection"></param>
        /// <param name="toSlot"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public virtual bool UnstackItem(ItemCollectionBase toCollection, uint toSlot, uint amount)
        {
            if (itemCollection == null)
            {
                Debug.LogWarning("Can't unstack an item that is not in a collection", transform);
                return false;
            }

            return itemCollection.UnstackSlot(index, toCollection, toSlot, amount);
        }
 public override bool SwapOrMerge(uint slot1, ItemCollectionBase handler2, uint slot2, bool repaint = true)
 {
     return(false);
 }
 /// <summary>
 /// Check if a given collection is a equip to collection.
 /// </summary>
 /// <param name="collection"></param>
 /// <returns></returns>
 public static bool IsEquipToCollection(ItemCollectionBase collection)
 {
     return(equipToCollections.Any(col => col.collection == collection));
 }
 /// <summary>
 /// Add a collection that functions as an Equippable collection. Items can be equipped to this collection.
 /// </summary>
 /// <param name="collection">The collection to add.</param>
 /// <param name="priority">
 /// How important is the collection, if you 2 collections can hold the item, which one should be chosen?
 /// Range of 0 to 100
 ///
 /// Note: This method is not used yet, it only registers the Equippable collection, that's it.
 /// </param>
 public static void AddEquipCollection(ItemCollectionBase collection, int priority)
 {
     equipToCollections.Add(new InventoryCollectionLookup(collection, priority));
 }
 public static void AddBankCollection(ItemCollectionBase collection)
 {
     bankCollections.Add(collection);
 }
Example #34
0
        public static InventoryUIDragLookup EndDrag(InventoryUIItemWrapper toDrag, uint startSlot, ItemCollectionBase handler, PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                var lookup = new InventoryUIDragLookup();
                lookup.startIndex          = (int)draggingItem.index;
                lookup.startItemCollection = draggingItem.itemCollection;

                if (hoveringItem != null)
                {
                    lookup.endIndex          = (int)hoveringItem.index;
                    lookup.endItemCollection = hoveringItem.itemCollection;
                }

                Object.Destroy(draggingItem.gameObject); // No longer need it

                draggingItem = null;
                //draggingButtonCollection = null;

                return(lookup);
            }

            return(null);
        }
        private void OnSwappedItemsPlayMaker(ItemCollectionBase fromCollection, uint fromSlot, ItemCollectionBase toCollection, uint toSlot)
        {

        }
 public void SaveItems(ItemCollectionBase collection, Uri saveLocation, byte[] serializedData, Action<bool> callback)
 {
     PlayerPrefs.SetString("InventorySystem_" + collection.collectionName.ToLower().Replace(" ", "_"), System.Text.Encoding.UTF8.GetString(serializedData));
     if (callback != null)
         callback(true); // All good
 }
 public override void OnReset()
 {
     item = null;
     amount = 1;
     collection = null;
 }
 /// <summary>
 /// Check if a given collection is a equip to collection.
 /// </summary>
 /// <param name="collection"></param>
 /// <returns></returns>
 public static bool IsEquipToCollection(ItemCollectionBase collection)
 {
     return equipToCollections.Any(col => col.collection == collection);
 }
 private void OnUnstackedItemPly(ItemCollectionBase fromCollection, uint startSlot, ItemCollectionBase toCollection, uint endSlot, uint amount)
 {
     if (eventHandler != null)
         eventHandler.CollectionOnUnstackedItem(startSlot, endSlot, amount);
 }
 /// <summary>
 /// Check if a given collection is a loot to collection.
 /// </summary>
 /// <param name="collection"></param>
 /// <returns></returns>
 public static bool IsInventoryCollection(ItemCollectionBase collection)
 {
     return lootToCollections.Any(col => col.collection == collection);
 }
        // <inheritdoc />
        public override void TriggerUnstack(ItemCollectionBase toCollection, int toIndex = -1)
        {
            if (item == null || itemCollection.useReferences || itemCollection.canUnstackItemsInCollection == false)
                return;

            if (item.currentStackSize > 1)
            {
                var m = InventorySettingsManager.instance;
                if(m.useUnstackDialog)
                {
                    var d = InventoryManager.instance.lang.unstackDialog;
                    InventoryManager.instance.unstackDialog.ShowDialog(itemCollection.transform, d.title, d.message, 1, (int)item.currentStackSize - 1, item,
                        (int val) =>
                        {
                            if (toIndex != -1)
                                itemCollection.UnstackSlot(index, toCollection, (uint) toIndex, (uint)val, true);
                            else
                                itemCollection.UnstackSlot(index, (uint)val, true);
                        },
                        (int val) =>
                        {
                            // Canceled


                        });
                }
                else
                {
                    if(toIndex != -1)
                        itemCollection.UnstackSlot(index, toCollection, (uint)toIndex, (uint)Mathf.Floor(item.currentStackSize / 2), true);                    
                    else
                        itemCollection.UnstackSlot(index, (uint)Mathf.Floor(item.currentStackSize / 2), true);
                }
            }
        }
Example #42
0
 /// <summary>
 /// When the cursor exits an item
 /// </summary>
 /// <param name="item"></param>
 /// <param name="slot">The slot is the IButtonHandler index not the inventory index.</param>
 /// <param name="handler"></param>
 /// <param name="eventData"></param>
 public static void ExitItem(InventoryUIItemWrapper item, uint slot, ItemCollectionBase handler, PointerEventData eventData)
 {
     hoveringItem = null;
     //hoveringItemCollection = null;
 }