Пример #1
0
        /// <summary>Swaps a whole item or stack.</summary>
        /// <returns>True if swap occurs, else false.</returns>
        private bool SwapItem(InventoryItem srcInvItem, int fromSlotId, InventoryItem destInvItem, int toSlotId, bool fireEvent)
        {
            // Check that the source item is able to go to the dest slot and that the destination item non-existant or can be swapped
            if (ValidateSlot(srcInvItem, toSlotId) && (destInvItem == null || ValidateSlot(destInvItem, fromSlotId))) {
                if (fromSlotId == (int)InventorySlot.Cursor)
                    _cursorQueue.Dequeue(); // If we're moving something from the cursor, dequeue it

                if (srcInvItem.Item.ItemClass == Item.ITEM_CLASS_CONTAINER) { // Are we moving a container from the source slot?
                    // Clear items from any possible containers
                    ClearBagContents(toSlotId);     // We can assume the items are both containers as we've validated by this point
                    ClearBagContents(fromSlotId);

                    // Move source container's items to the destination container slots
                    int idx = 0;
                    foreach (InventoryItem invItem in srcInvItem.SubItems()) {
                        int toContSlotID = GetSlotIdWithinContainer(toSlotId, idx);
                        int fromContSlotID = GetSlotIdWithinContainer(fromSlotId, idx);
                        //_log.DebugFormat("Moving item from within a container @ slot {0} to slot {1}", fromContSlotID, toContSlotID);

                        this[toContSlotID] = invItem;
                        idx++;
                    }
                }

                if (destInvItem != null && destInvItem.Item.ItemClass == Item.ITEM_CLASS_CONTAINER) {
                    // Move destination container's items to the source container slots
                    int idx = 0;
                    int fromContSlotID = GetSlotIdWithinContainer(fromSlotId, idx);
                    foreach (InventoryItem invItem in destInvItem.SubItems())
                        this[fromContSlotID + idx++] = invItem;
                }

                // Swap the source and destination items
                this[toSlotId] = srcInvItem;
                this[fromSlotId] = destInvItem;

                if (fireEvent) {
                    OnItemMoved(new ItemMoveEventArgs((uint)fromSlotId, (uint)toSlotId, 0, false));
                    if (destInvItem != null)
                        OnItemMoved(new ItemMoveEventArgs((uint)toSlotId, (uint)fromSlotId, 0, false));
                }

                return true;
            }
            else
                return false;
        }