Пример #1
0
        public void MoveItemToCell(int sourceCellId, int destCellId)
        {
            _currentSort = SortType.Unsorted;

            InventoryCell source = _cells[sourceCellId];
            InventoryCell dest   = _cells[destCellId];

            if (dest.ItemsCount > 0)
            {
                if (source.ItemId == dest.ItemId) // If we have same items in this cells - we exchange numbers until stack is full
                {
                    InventoryItem item = DummySettings.GetItemById(source.ItemId);
                    int           free = item.MaxStackSize - dest.ItemsCount;
                    dest.ItemsCount  += free;
                    source.ItemsCount = source.ItemsCount > free ? source.ItemsCount - free : 0;
                }
                else
                {
                    int itemId     = dest.ItemId;
                    int itemsCount = dest.ItemsCount;
                    dest.ItemId       = source.ItemId;
                    dest.ItemsCount   = source.ItemsCount;
                    source.ItemId     = itemId;
                    source.ItemsCount = itemsCount;
                }
            }
            else
            {
                dest.ItemId       = source.ItemId;
                dest.ItemsCount   = source.ItemsCount;
                source.ItemId     = -1;
                source.ItemsCount = 0;
            }
        }
Пример #2
0
        void SortByCondition(InventoryCell[] cells, SortCondition condition)
        {
            // I'm using Bubble sort here because it's simple and our data set isn't large
            bool swapped = false;

            for (int i = 0; i < cells.Length - 1; i++)
            {
                for (int j = 0; j < cells.Length - 1 - i; j++)
                {
                    if (condition(cells[j], cells[j + 1]))
                    {
                        InventoryCell temp = cells[j];
                        cells[j]     = cells[j + 1];
                        cells[j + 1] = temp;

                        swapped = true;
                    }
                }

                if (!swapped)
                {
                    break;
                }
            }
        }
Пример #3
0
        public Dictionary <InventoryItemActions, string> GetContextMenuActions(int cellId)
        {
            InventoryCell cell = _cells[cellId];

            if (cell.ItemsCount > 0)
            {
                InventoryItem item = DummySettings.GetItemById(cell.ItemId);
                List <InventoryItemActions> actions = item.GetItemActions();
                Dictionary <InventoryItemActions, string> actionDescs = new Dictionary <InventoryItemActions, string>();

                foreach (InventoryItemActions action in actions)
                {
                    switch (action)
                    {
                    case InventoryItemActions.Use:
                        actionDescs[action] = "Use " + item.Name;
                        break;

                    case InventoryItemActions.Sell:
                        int    price      = item.Price * cell.ItemsCount;
                        string amountInfo = cell.ItemsCount > 1 ? "(" + cell.ItemsCount.ToString() + ")" : "";
                        actionDescs[action] = "Sell " + item.Name + amountInfo + " for " + price.ToString();
                        break;

                    case InventoryItemActions.Drop:
                        actionDescs[action] = "Drop " + item.Name;
                        break;
                    }
                }

                return(actionDescs);
            }

            return(null);
        }
Пример #4
0
        public InventoryCell[] GetCellsContent()
        {
            InventoryCell[] cellsCopy = new InventoryCell[_cells.Length];
            Array.Copy(_cells, cellsCopy, _cells.Length);

            return(cellsCopy);
        }
Пример #5
0
        void AddButton_Click(object sender, EventArgs e)
        {
            InventoryCell cell = _inventory.GetCellsContent()[_selectedId];

            if (cell.ItemsCount > 0)
            {
                _inventory.AddItem(cell.ItemId, 1);
                UpdateInventoryGrid();
            }
        }
Пример #6
0
        public void RemoveItemsByCell(int cellId, int amount)
        {
            InventoryCell cell = _cells[cellId];

            cell.ItemsCount = (cell.ItemsCount > amount) ? cell.ItemsCount - amount : 0;
            if (cell.ItemsCount < 1)
            {
                cell.ItemId = -1;
                UpdateCurrentSorting();
            }
            UpdateEquipment();
        }
Пример #7
0
        public void AddItem(int itemId, int amount)
        {
            InventoryItem item = DummySettings.GetItemById(itemId);

            if (item.Quest && item.MaxStackSize == 1 && Array.FindIndex(_cells, c => c.ItemId == itemId) > -1)
            {
                Console.WriteLine("Cannot add quest item " + item.Name + ": non-stackable");
                return;
            }

            InventoryCell[] cells = Array.FindAll(_cells, c => c.ItemId == itemId && c.ItemsCount < item.MaxStackSize);
            if (cells.Length > 0)
            {
                SortByCondition(cells, CompareCount);

                for (int i = 0; i < cells.Length; i++)
                {
                    InventoryCell cell = cells[i];
                    int           free = item.MaxStackSize - cell.ItemsCount;
                    cell.ItemsCount += amount <= free ? amount : free;
                    amount          -= free;
                    if (amount <= 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < _cells.Length; i++)
                {
                    InventoryCell cell = _cells[i];
                    if (cell.ItemsCount > 0)
                    {
                        continue;
                    }
                    else
                    {
                        cell.ItemId = itemId;
                    }

                    int free = item.MaxStackSize - cell.ItemsCount;
                    cell.ItemsCount += amount <= free ? amount : free;
                    amount          -= free;
                    if (amount <= 0)
                    {
                        break;
                    }
                }
            }
        }
Пример #8
0
 public Inventory()
 {
     _equippedItemId = -1;
     _currentSort    = SortType.Unsorted;
     _cells          = new InventoryCell[DummySettings.CellsMaxAmount];
     for (int i = 0; i < DummySettings.CellsMaxAmount; i++)
     {
         _cells[i] = new InventoryCell()
         {
             ItemId = -1, ItemsCount = 0
         }
     }
     ;
 }
Пример #9
0
        bool CompareType(InventoryCell a, InventoryCell b)
        {
            if (a.ItemsCount < 1 && b.ItemsCount > 0)
            {
                return(true);
            }
            else if (a.ItemsCount > 0 && b.ItemsCount < 1 || a.ItemsCount < 1 && b.ItemsCount < 1)
            {
                return(false);
            }

            InventoryItem itemA = DummySettings.GetItemById(a.ItemId);
            InventoryItem itemB = DummySettings.GetItemById(b.ItemId);

            return(itemA.Type > itemB.Type);
        }
Пример #10
0
 public void RemoveItemsById(int itemId, int amount)
 {
     InventoryCell[] cells = Array.FindAll(_cells, c => c.ItemId == itemId && c.ItemsCount > 0);
     SortByCondition(cells, CompareCount);
     for (int i = cells.Length - 1; i >= 0; i--)
     {
         InventoryCell cell  = cells[i];
         int           count = cell.ItemsCount;
         cell.ItemsCount = count > amount ? count - amount : 0;
         amount         -= count;
         if (amount <= 0)
         {
             break;
         }
     }
     UpdateCurrentSorting();
     UpdateEquipment();
 }
Пример #11
0
        void UseItemFromCell(int cellId)
        {
            InventoryCell cell = _cells[cellId];
            InventoryItem item = DummySettings.GetItemById(cell.ItemId);

            switch (item.Type)
            {
            case InventoryItemType.Consumable:
                // Todo: invoke event to inform the system about used consumable
                //..
                RemoveItemsByCell(cellId, 1);
                break;

            case InventoryItemType.Euipment:
                _equippedItemId = item.Id;
                // Todo: invoke event to inform the system about equipped item
                break;
            }
        }
Пример #12
0
        void UpdateInventoryGrid()
        {
            InventoryCell[] cells = _inventory.GetCellsContent();
            for (int i = 0; i < cells.Length; i++)
            {
                InventoryCell cell = cells[i];
                InventoryItem item = DummySettings.GetItemById(cell.ItemId);
                CellView      cw   = _cellViews[i];
                cw.slot.ContextMenu.MenuItems.Clear();

                if (item != null)
                {
                    cw.name.Text    = item.Name;
                    cw.icon.Image   = item.Icon;
                    cw.icon.Visible = true;
                    cw.count.Text   = cell.ItemsCount.ToString();

                    Dictionary <InventoryItemActions, string> actions = _inventory.GetContextMenuActions(i);
                    if (actions != null)
                    {
                        foreach (KeyValuePair <InventoryItemActions, string> action in actions)
                        {
                            MenuItem menuItem = new MenuItem(action.Value);
                            menuItem.Click += (s, e) =>
                            {
                                _inventory.ApplyActionToItemInCell(action.Key, cw.id);
                                UpdateInventoryGrid();
                                UpdateEquipmentSlot();
                            };
                            cw.slot.ContextMenu.MenuItems.Add(menuItem);
                        }
                    }
                }
                else
                {
                    cw.name.Text    = "";
                    cw.icon.Visible = false;
                    cw.count.Text   = "";
                }
            }
        }
Пример #13
0
 bool CompareCount(InventoryCell a, InventoryCell b)
 {
     return(a.ItemsCount < b.ItemsCount);
 }