Exemplo n.º 1
0
        private void dgvMyItems_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            // The "Sell 1" button
            if (e.ColumnIndex == 4)
            {
                var  itemID        = dgvMyItems.Rows[e.RowIndex].Cells[0].Value;
                Item itemBeingSold = World.GetItem(Convert.ToInt32(itemID));

                if (itemBeingSold.Untradable)
                {
                    MessageBox.Show("You cannot sell the " + itemBeingSold.Name);
                }
                else
                {
                    _player.RemoveItemFromInventory(itemBeingSold.ID);
                    _player.Gold += itemBeingSold.Price;
                    _vendor.AddItemToInventory(itemBeingSold);
                }
            }
        }
        private void dgvMyItems_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //The first column of a dataagrid view has a ColumnIndex = 0
            //This is known as "zero-based" array/collection/list
            //If the player clicked the button column, we will sell an item from that row

            if (e.ColumnIndex == 4)
            {
                //This gets the ID value of the item, from the hidden 1st column
                var itemID = dgvMyItems.Rows[e.RowIndex].Cells[0].Value;

                //Get the item object for the selected row
                Item itemBeingSold = World.ItemByID(Convert.ToInt32(itemID));

                var num = Convert.ToInt32(dgvMyItems.Rows[e.RowIndex].Cells[2].Value);

                if (itemBeingSold.Price == World.UNSELLABLE_ITEM_PRICE)
                {
                    MessageBox.Show($"Unable to sell {itemBeingSold.Name}.");
                }

                else if (itemBeingSold is Weapon && currentPlayer.Weapons.Count == 1 && num == 1)
                {
                    //MessageBox.Show($"Unable to sell your only weapon.");
                    MessageBox.Show("YOU CAN'T SELL YOUR ONLY WEAPON!");
                }

                else
                {
                    //Remove one of these items from the inventory
                    currentPlayer.RemoveItemFromInventory(itemBeingSold);

                    currentVendor.AddItemToInventory(itemBeingSold, 1);

                    //Give the player gold
                    currentPlayer.Gold += itemBeingSold.Price;
                }
            }
        }