private void OnUnstackedItemPly(ItemCollectionBase fromCollection, uint startSlot, ItemCollectionBase toCollection, uint endSlot, uint amount)
 {
     if (eventHandler != null)
     {
         eventHandler.CollectionOnUnstackedItem(startSlot, endSlot, amount);
     }
 }
Example #2
0
        /// <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)
        {
            Assert.IsNotNull(collection, "Added inventory collection to manager that was NULL!");
            Assert.IsTrue(priority >= 0, "Priority has to be higher than 0");

            _lootToCollections.Add(new ItemCollectionPriority <ItemCollectionBase>(collection, priority));
        }
 private void OnSwappedItemsPly(ItemCollectionBase fromCollection, uint fromSlot, ItemCollectionBase toCollection, uint toSlot)
 {
     if (eventHandler != null)
     {
         eventHandler.CollectionOnSwappedItems(fromCollection, fromSlot, toCollection, toSlot);
     }
 }
        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?
                }
                bool canMove = bag.CanUnEquip(toCollection, toSlot);
                if (canMove == false)
                {
                    return(false);
                }

                if (toSlot >= toCollection.items.Length - bag.extendBySlots)
                {
                    return(false);
                }
            }

            return(base.MoveItem(item, fromSlot, toCollection, toSlot, clearOld, doRepaint));
        }
Example #5
0
        public static void RemoveInventoryCollection(ItemCollectionBase collection)
        {
            _lootToCollections.RemoveAll(o => o.collection = collection);
            //var found = lootToCollections.FirstOrDefault(o => o.collection == collection);
            //if (found != null)
            //lootToCollections.Remove(found);

            //lootToCollections.Remove(new InventoryCollectionLookup(collection, priority));
        }
Example #6
0
        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;
            }
        }
Example #7
0
        public virtual void ToCollection(ItemCollectionBase collection)
        {
            collection.Resize((uint)items.Length);
            if (collection.useReferences)
            {
                for (int i = 0; i < items.Length; i++)
                {
                    var item = items[i];
                    var c    = ItemCollectionBase.FindByName(item.collectionName);
                    if (item.amount > 0 && item.itemID >= 0)
                    {
                        collection[i].item = c.Find((uint)item.itemID);
                        collection[i].Repaint();
                    }
                }
            }
            else
            {
                var deserializedItems = items.Select(o => o.ToItem()).ToArray();
                for (int i = 0; i < deserializedItems.Length; i++)
                {
                    collection[i].item = deserializedItems[i];
                    collection[i].Repaint();

                    if (deserializedItems[i] != null)
                    {
                        deserializedItems[i].gameObject.SetActive(false);
                        deserializedItems[i].transform.SetParent(collection.container);
                    }
                }
            }

            // Handle equippable items; Make sure the reference to the equippable collection is set.
            foreach (var item in collection)
            {
                var eq             = item.item as EquippableInventoryItem;
                var charCollection = collection as ICharacterCollection;
                if (eq != null && charCollection != null)
                {
                    eq.equippedToCollection = charCollection;
                    eq.NotifyItemEquipped(charCollection.equippableSlots[eq.index], eq.currentStackSize);
                }
            }

            collection.UnRegisterCurrencyEvents();
            collection.currenciesGroup = new CurrencyDecoratorCollection(false);

            var deserializedCurrencies = currencies.Select(o => o.ToCurrencyDecorator());

            foreach (var c in deserializedCurrencies)
            {
                collection.currenciesGroup.AddCurrency(c);
            }

            collection.RegisterCurrencyEvents();
        }
        /// <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));
        }
Example #9
0
        public override bool CanMergeSlots(uint slot1, ItemCollectionBase collection2, uint slot2)
        {
            bool can = base.CanMergeSlots(slot1, collection2, slot2);

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

            return(useReferences == false);
        }
Example #10
0
        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;
            }
        }
        public override bool SwapOrMerge(uint slot1, ItemCollectionBase handler2, uint slot2, bool repaint = true)
        {
            if (this == handler2)
            {
                return(false);
            }

            if (handler2[slot2].item == null)
            {
                return(base.SwapOrMerge(slot1, handler2, slot2, repaint));
            }

            return(false);
        }
        public T FindElement <T>(string collectionName, bool warnWhenNotFound) where T : ItemCollectionBase
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                return(null);
            }

            var a = ItemCollectionBase.FindByName <T>(collectionName);

            if (a == null && warnWhenNotFound)
            {
                Debug.LogWarning("Player instantiation :: Collection with name (" + collectionName + ") not found!");
            }

            return(a);
        }
Example #13
0
        protected override bool SwapSlots(uint fromSlot, ItemCollectionBase toCollection, uint toSlot, bool repaint = true, bool fireEvents = true)
        {
            var swapped = base.SwapSlots(fromSlot, toCollection, toSlot, repaint, fireEvents);

            if (swapped)
            {
                if (this != toCollection)
                {
                    return(true);
                }

                _character.equipmentHandler.SwapItems(this, fromSlot, toSlot);
            }

            return(swapped);
        }
        // <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.settings.useUnstackDialog)
                {
                    var d = InventoryManager.langDatabase.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);
                    }
                }
            }
        }
        public virtual void UseWithTrigger(CraftingCategory category, CraftingProgressContainer progressContainer, ICraftingActionValidator validator, ItemCollectionBase removeItemsFromCollection, ItemCollectionBase storeRewardItemsInCollection)
        {
            Assert.IsNotNull(category);
            Assert.IsNotNull(progressContainer);

            overrideRemoveItemsFromCollection    = removeItemsFromCollection;
            overrideStoreRewardItemsInCollection = storeRewardItemsInCollection;

            if (_forceSingleInstance)
            {
                this.progressContainer = new CraftingProgressContainer(this, GetInstanceID(), GetComponent <AudioSource>());
            }
            else
            {
                this.progressContainer           = progressContainer;
                this.progressContainer.validator = validator;
            }

            SetCraftingCategory(category);
            window.Show();
        }
        public virtual InventoryItemBase ToItem()
        {
            if (itemID < 0 || itemID > ItemManager.database.items.Length - 1)
            {
//                DevdogLogger.LogWarning("ItemID is out of range, trying to deserialize item " + itemID);
                return(null);
            }

            var item = ItemManager.database.items[itemID];
            var inst = UnityEngine.Object.Instantiate <InventoryItemBase>(item);
            var s    = this.stats.Select(o => o.ToStat()).ToArray();

            inst.currentStackSize = amount;
            inst.stats            = s;
            if (string.IsNullOrEmpty(collectionName) == false)
            {
                inst.itemCollection = ItemCollectionBase.FindByName(collectionName);
            }

            return(inst);
        }
        public bool CanUnEquip(ItemCollectionBase toCollection, uint toIndex)
        {
            var extenderCollection = GetExtenderCollection();

            if (extenderCollection == null)
            {
                DevdogLogger.LogWarning("Can't unequip bag, no inventory found with extender collection");
                return(false);
            }

            // If the item is placed inside the slots it's supposed to rmove it should fail...
            var clearSlots = extendBySlots + layoutSize;
            var c          = extenderCollection.extendingCollection;

            if (toIndex > toCollection.items.Length - clearSlots)
            {
                return(false);
            }

            return(c.CanRemoveSlots(clearSlots));
        }
        /// <summary>
        /// Fill this data model with a collection reference.
        /// Gets the collection data from the collection and stores it in this serializable model.
        /// </summary>
        /// <param name="collection"></param>
        public void FromCollection(ItemCollectionBase collection)
        {
            currencies = collection.currenciesGroup.lookups.Select(o => new CurrencyDecoratorSerializationModel(o)).ToArray();
            //            items = collection.items.Select(o => new InventoryItemSerializationModel(o.item)).ToArray();


            // Serialize based on inventory item serialization model.
            items = new InventoryItemSerializationModel[collection.items.Length];
            for (int i = 0; i < collection.items.Length; i++)
            {
                var item = collection.items[i];
                InventoryItemSerializationModel inst = null;
                if (item.item != null)
                {
                    var  classes            = ReflectionUtility.GetAllClassesWithAttribute(typeof(SerializationModelAttribute), true);
                    Type serializationModel = typeof(InventoryItemSerializationModel);

                    foreach (var c in classes)
                    {
                        var attrib = (SerializationModelAttribute)c.GetCustomAttributes(typeof(SerializationModelAttribute), true).First();
                        if (c == item.item.GetType())
                        {
                            DevdogLogger.LogVerbose("Using custom serialization model for " + item.item.GetType().Name + " - " + attrib.type.Name);
                            serializationModel = attrib.type;
                        }
                    }

                    inst = (InventoryItemSerializationModel)Activator.CreateInstance(serializationModel);
                    inst.FromItem(item.item);
                }
                else
                {
                    inst = new InventoryItemSerializationModel(item.item);
                }

                items[i] = inst;
            }
        }
Example #19
0
        protected void Start()
        {
            if (targetCollection == null)
            {
                targetCollection = GetComponent <ItemCollectionBase>();
            }
            if (targetCollection == null)
            {
                DevdogLogger.LogError("CollectionPopulator can only be used on a collection.", transform);
                return;
            }

            if (useForceSet)
            {
                for (uint i = 0; i < _items.Length; i++)
                {
                    targetCollection.SetItem(i, _items[i], true);
                }
            }
            else
            {
                targetCollection.AddItems(_items);
            }
        }
Example #20
0
 public CollectionPriority(Tuple[] collection, int priority, ItemCollectionBase collectionRef)
     : base(collection, priority)
 {
     this.collectionRef = collectionRef;
 }
Example #21
0
 public CollectionPriority FindLookup(ItemCollectionBase collection)
 {
     return(_collections.FirstOrDefault(o => o.collectionRef == collection));
 }
        public virtual object SerializeCollection(ItemCollectionBase collection)
        {
            var serializationModel = new ItemCollectionSerializationModel(collection);

            return(JsonSerializer.Serialize(serializationModel, null));
        }
 /// <summary>
 /// Trigger the unstacking of this wrapper.
 /// </summary>
 /// <param name="toCollection">The collection to unstack the item into.</param>
 /// <param name="toIndex">The index the new item should be unstacked to. <b>-1 for the first empty slot available.</b></param>
 public abstract void TriggerUnstack(ItemCollectionBase toCollection, int toIndex = -1);
 // <inheritdoc />
 public void TriggerUnstack(ItemCollectionBase toCollection, int toIndex = -1)
 {
     DevdogLogger.LogWarning("Trigger actions can't be used on data wrapper.");
 }
 public ItemCollectionSerializationModel(ItemCollectionBase itemCollection)
 {
     FromCollection(itemCollection);
 }
Example #26
0
 public static void RemoveBankCollection(ItemCollectionBase collection)
 {
     _bankCollections.Remove(collection);
 }
Example #27
0
 public override bool CanMergeSlots(uint slot1, ItemCollectionBase collection2, uint slot2)
 {
     return(false);
 }
Example #28
0
        /// <summary>
        /// Tries to find a blueprint based on the current layout / items inside the UI item wrappers (items).
        /// </summary>
        /// <returns>Returns blueprint if found one, null if not.</returns>
        public virtual CraftingBlueprint GetBlueprintFromCurrentLayout(ItemCollectionBase collection, CraftingCategory category)
        {
            if (collection.items.Length != category.cols * category.rows)
            {
                Debug.LogWarning("Updating blueprint but blueprint layout cols/rows don't match the collection");
            }

            int totalItemCountInLayout = 0; // Nr of items inside the UI wrappers.

            foreach (var item in collection.items)
            {
                if (item.item != null)
                {
                    totalItemCountInLayout++;
                }
            }

            foreach (var b in GetBlueprints(category))
            {
                foreach (var a in b.blueprintLayouts)
                {
                    if (a.enabled)
                    {
                        var hasItems = new Dictionary <uint, uint>(); // ItemID, amount
                        //var requiredItems = new Dictionary<uint, uint>(); // ItemID, amount
                        currentBlueprintItemsDict.Clear();

                        int  counter         = 0; // Item index counter
                        int  shouldHaveCount = 0; // Amount we should have..
                        int  hasCount        = 0; // How many slots in our layout
                        bool matchFailed     = false;
                        foreach (var r in a.rows)
                        {
                            if (matchFailed)
                            {
                                break;
                            }

                            foreach (var c in r.columns)
                            {
                                if (c.item != null && c.amount > 0)
                                {
                                    if (currentBlueprintItemsDict.ContainsKey(c.item.ID) == false)
                                    {
                                        currentBlueprintItemsDict.Add(c.item.ID, 0);
                                    }

                                    currentBlueprintItemsDict[c.item.ID] += (uint)c.amount;
                                    shouldHaveCount++;

                                    if (collection.items[counter].item != null)
                                    {
                                        if (collection.items[counter].item.ID != c.item.ID)
                                        {
                                            matchFailed = true;
                                            break; // Item in the wrong place...
                                        }

                                        if (hasItems.ContainsKey(c.item.ID) == false)
                                        {
                                            uint itemCount = 0;
                                            if (collection.useReferences)
                                            {
                                                itemCount = InventoryManager.GetItemCount(c.item.ID, category.alsoScanBankForRequiredItems);
                                            }
                                            else
                                            {
                                                //                                                itemCount = items[counter].item.currentStackSize;
                                                itemCount = collection.GetItemCount(c.item.ID);
                                            }

                                            hasItems.Add(c.item.ID, itemCount);
                                        }

                                        hasCount++;
                                    }
                                    else if (collection.items[counter].item == null && c != null)
                                    {
                                        matchFailed = true;
                                        break;
                                    }
                                }

                                counter++;
                            }
                        }

                        if (matchFailed)
                        {
                            continue;
                        }

                        // Filled slots test
                        if (totalItemCountInLayout != hasCount || shouldHaveCount != hasCount)
                        {
                            continue;
                        }

                        // Check count
                        foreach (var item in currentBlueprintItemsDict)
                        {
                            if (hasItems.ContainsKey(item.Key) == false || hasItems[item.Key] < item.Value)
                            {
                                matchFailed = true;
                            }
                        }

                        if (matchFailed == false)
                        {
                            return(b);
                        }
                    }
                }
            }

            return(null); // Nothing found
        }
Example #29
0
 public override bool SwapOrMerge(uint slot1, ItemCollectionBase handler2, uint slot2, bool repaint = true)
 {
     return(false);
 }
 public override bool SwapOrMerge(uint slot1, ItemCollectionBase handler2, uint slot2, bool repaint = true)
 {
     return(SwapSlots(slot1, handler2, slot2, repaint));
 }