コード例 #1
0
        private void Equip(Type itemType)
        {
            var equipInput = "";

            Console.Clear();
            if (itemType == typeof(Weapon) && Items.GetListOfItems(Hero.Bag, typeof(Weapon)).Any())
            {
                UI.DefaultBoxes.DrawManageInventory(Hero, UI.Grid.Left, Items.GetListOfItems(Hero.Bag, typeof(Weapon)));
                UI.DefaultBoxes.DrawOptions(new List <string>
                {
                    "Choose an item number to equip..",
                    "Press 0 to return to main menu."
                }, UI.Grid.Center);
                TakeInput();
            }
            else if (itemType == typeof(Armor) && Items.GetListOfItems(Hero.Bag, typeof(Armor)).Any())
            {
                UI.DefaultBoxes.DrawManageInventory(Hero, UI.Grid.Left, Items.GetListOfItems(Hero.Bag, typeof(Armor)));
                UI.DefaultBoxes.DrawOptions(new List <string>
                {
                    "Choose an item number to equip..",
                    "Press 0 to return to main menu."
                }, UI.Grid.Center);
                TakeInput();
            }
            else if (itemType == typeof(Shield) && Items.GetListOfItems(Hero.Bag, typeof(Shield)).Any())
            {
                UI.DefaultBoxes.DrawManageInventory(Hero, UI.Grid.Left, Items.GetListOfItems(Hero.Bag, typeof(Shield)));
                UI.DefaultBoxes.DrawOptions(new List <string>
                {
                    "Choose an item number to equip..",
                    "Press 0 to return to main menu."
                }, UI.Grid.Center);
                TakeInput();
            }
            else
            {
                UI.DefaultBoxes.DrawInventory(Hero, UI.Grid.Left);
                UI.Draw.PrintToOutput("There's no item of that type in your bag.");
                UI.DefaultBoxes.DrawOptions(new List <string>
                {
                    "Press any key to return to main menu."
                }, UI.Grid.Center);
                Console.ReadLine();
            }

            void TakeInput()
            {
                UI.Draw.UpdateInputCursor();
                equipInput = Console.ReadLine();

                while (!int.TryParse(equipInput, out int _x))
                {
                    UI.Draw.PrintToOutput("That is not a number, please choose again.");
                    equipInput = Console.ReadLine();
                }

                // If the input can be parsed as a number
                if (int.TryParse(equipInput, out int itemIndex) && itemIndex != 0)
                {
                    Console.Clear();
                    UI.DefaultBoxes.DrawInventory(Hero, UI.Grid.Left);
                    // If the number is an index of armour bag
                    if ((itemIndex -= 1) <= Items.GetListOfItems(Hero.Bag, itemType).Count - 1)
                    {
                        // Then Equip that Item
                        Hero.Equip(itemIndex, itemType);
                        UI.Draw.PrintToOutput("Item equipped!");
                    }
                    else
                    {
                        UI.Draw.PrintToOutput("There's no item with that number...");
                    }
                    UI.DefaultBoxes.DrawOptions(new List <string> {
                        "Press any key to return to Inventory."
                    }, UI.Grid.Center);
                    UI.Draw.UpdateInputCursor();
                    Console.ReadKey();
                }
            }
        }