Exemplo n.º 1
0
        /**
         * <summary>Inserts an inventory item instance into a specific index in the collection.  If the item exists in another collection, it will be removed from there automatically.</summary>
         * <param name="addInstance">The inventory item instance to add</param>
         * <param name="index">The index to insert the item at</param>
         * <param name="occupiedSlotBehaviour">How to react if the intended index is already occupied by another item instance.</param>
         * <returns>The new instance of the added item</returns>
         */
        public InvInstance Insert(InvInstance addInstance, int index, OccupiedSlotBehaviour occupiedSlotBehaviour = OccupiedSlotBehaviour.ShiftItems)
        {
            // Adds to a specific index, or the end/first empty slot if -1
            if (!CanAccept(addInstance, index, occupiedSlotBehaviour))
            {
                if (InvInstance.IsValid(addInstance))
                {
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                    }
                }
                return(null);
            }

            InvInstance addedInstance = null;

            if (Contains(addInstance))
            {
                if (!CanReorder())
                {
                    return(addInstance);
                }

                if (MaxSlots > 0 && index >= MaxSlots)
                {
                    return(addInstance);
                }

                occupiedSlotBehaviour = OccupiedSlotBehaviour.SwapItems;
            }

            int numAdded = -1;

            InvCollection fromCollection = (Contains(addInstance)) ? this : addInstance.GetSource();

            if (index >= 0 && index < invInstances.Count)
            {
                // Inside
                InvInstance existingInstance = invInstances[index];

                if (!InvInstance.IsValid(existingInstance))
                {
                    // Empty slot
                    addedInstance = addInstance.CreateTransferInstance();

                    if (InvInstance.IsValid(addedInstance))
                    {
                        numAdded            = addedInstance.Count;
                        invInstances[index] = addedInstance;
                    }
                }
                else
                {
                    if (existingInstance == addInstance)
                    {
                        // Same
                        return(existingInstance);
                    }
                    else if (existingInstance.InvItem == addInstance.InvItem && addInstance.InvItem.canCarryMultiple && existingInstance.Capacity > 0)
                    {
                        // Merge
                        if (addInstance.TransferCount > existingInstance.Capacity)
                        {
                            addInstance.TransferCount = existingInstance.Capacity;
                        }
                        numAdded = Mathf.Min(addInstance.CreateTransferInstance().Count, existingInstance.Capacity);
                        existingInstance.Count += numAdded;
                        addedInstance           = existingInstance;
                    }
                    else
                    {
                        switch (occupiedSlotBehaviour)
                        {
                        case OccupiedSlotBehaviour.ShiftItems:
                            invInstances.Insert(index, addInstance.CreateTransferInstance());
                            addedInstance = invInstances[index];
                            break;

                        case OccupiedSlotBehaviour.FailTransfer:
                            if (InvInstance.IsValid(addInstance))
                            {
                                if (KickStarter.eventManager)
                                {
                                    KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                                }
                                return(null);
                            }
                            break;

                        case OccupiedSlotBehaviour.SwapItems:
                            if (fromCollection != null)
                            {
                                if (addInstance.IsPartialTransform())
                                {
                                    if (KickStarter.eventManager)
                                    {
                                        KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                                    }
                                    return(null);
                                }

                                fromCollection.invInstances[fromCollection.IndexOf(addInstance)] = existingInstance;
                                invInstances[index] = addInstance;
                                addedInstance       = invInstances[index];

                                if (KickStarter.runtimeInventory.SelectedInstance == addInstance)
                                {
                                    KickStarter.runtimeInventory.SelectItem(existingInstance);
                                }
                            }
                            break;

                        case OccupiedSlotBehaviour.Overwrite:
                            if (KickStarter.eventManager)
                            {
                                KickStarter.eventManager.Call_OnChangeInventory(this, existingInstance, InventoryEventType.Remove);
                            }
                            invInstances[index] = addInstance.CreateTransferInstance();
                            addedInstance       = invInstances[index];
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            else
            {
                // Add to first empty slot, or end
                bool addedInside = false;

                if (index < 0)
                {
                    // Find first empty slot
                    for (int i = 0; i < invInstances.Count; i++)
                    {
                        if (!InvInstance.IsValid(invInstances[i]))
                        {
                            invInstances[i] = addInstance.CreateTransferInstance();
                            addedInstance   = invInstances[i];
                            index           = i;
                            addedInside     = true;
                            break;
                        }
                        else if (invInstances[i] == addInstance)
                        {
                            return(addInstance);
                        }
                    }
                }

                if (!addedInside)
                {
                    if (maxSlots > 0 && invInstances.Count >= maxSlots)
                    {
                        return(null);
                    }

                    if (index > 0 && CanReorder())
                    {
                        while (invInstances.Count < index)
                        {
                            invInstances.Add(null);
                        }
                    }

                    invInstances.Add(addInstance.CreateTransferInstance());
                    addedInstance = invInstances[invInstances.Count - 1];
                }
            }

            if (fromCollection != null && fromCollection != this)
            {
                fromCollection.Clean();
            }

            Clean();
            PlayerMenus.ResetInventoryBoxes();

            if (KickStarter.eventManager)
            {
                if (numAdded >= 0)
                {
                    KickStarter.eventManager.Call_OnChangeInventory(this, addedInstance, InventoryEventType.Add, numAdded);
                }
                else
                {
                    KickStarter.eventManager.Call_OnChangeInventory(this, addedInstance, InventoryEventType.Add);
                }
            }
            return(addedInstance);
        }
Exemplo n.º 2
0
        /**
         * <summary>Adds an inventory item instance to the collection.  If the item exists in another collection, it will be removed from there automatically.</summary>
         * <param name="addInstance">The inventory item instance to add</param>
         */
        public void Add(InvInstance addInstance)
        {
            // Add to the first-available slot, or a filled slot if the same item

            if (!CanAccept(addInstance))
            {
                if (InvInstance.IsValid(addInstance))
                {
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                    }
                }
                return;
            }

            InvCollection fromCollection = addInstance.GetSource();

            bool added = false;

            for (int i = 0; i < invInstances.Count; i++)
            {
                // Inside

                if (!InvInstance.IsValid(invInstances[i]))
                {
                    // Empty slot
                    invInstances[i] = addInstance.CreateTransferInstance();
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnChangeInventory(this, invInstances[i], InventoryEventType.Add);
                    }
                    added = true;
                    break;
                }
                else if (invInstances[i] == addInstance)
                {
                    // Same
                }
                else if (invInstances[i].InvItem == addInstance.InvItem && addInstance.InvItem.canCarryMultiple && invInstances[i].Capacity > 0)
                {
                    // Merge
                    if (addInstance.TransferCount > invInstances[i].Capacity)
                    {
                        addInstance.TransferCount = invInstances[i].Capacity;
                    }
                    int numAdded = Mathf.Min(addInstance.CreateTransferInstance().Count, invInstances[i].Capacity);
                    invInstances[i].Count += numAdded;
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnChangeInventory(this, invInstances[i], InventoryEventType.Add, numAdded);
                    }
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                AddToEnd(addInstance);
                return;
            }

            if (fromCollection != null)
            {
                fromCollection.Clean();
            }
            Clean();
            PlayerMenus.ResetInventoryBoxes();
        }