Exemplo n.º 1
0
 // Checks inventory use.
 private void CheckInventoryUse()
 {
     if (KeyPress.InventorySlotId.HasValue)
     {
         ActionnedItem itemDropped = Player.Inventory.UseItem();
         if (itemDropped != null)
         {
             CurrentScreen.AddDroppedItem(itemDropped);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Uses an item of the inventory.
        /// </summary>
        /// <returns><see cref="ActionnedItem"/>; <c>Null</c> if item dropped.</returns>
        internal ActionnedItem UseItem()
        {
            if (!Engine.Default.KeyPress.InventorySlotId.HasValue)
            {
                return(null);
            }

            int inventorySlotId = Engine.Default.KeyPress.InventorySlotId.Value - 1;

            if (inventorySlotId >= DisplayableItems.Count)
            {
                return(null);
            }

            InventoryItem item = DisplayableItems.ElementAt(inventorySlotId);

            Item ammoItemBase = Item.GetAmmoItem(item.BaseItem.Type);

            InventoryItem ammoItem = _items.FirstOrDefault(it => ammoItemBase == it.BaseItem);

            if (!ItemCanBeUseInContext(item.BaseItem.Type) || (ammoItemBase != null ? (ammoItem == null || !ammoItem.TryPick()) : !item.TryPick()))
            {
                return(null);
            }

            // Note : items used as ammunitions are never removed from the list.
            if (item.Quantity == 0)
            {
                _items.Remove(item);
            }

            ActionnedItem droppedItem = null;

            switch (item.BaseItem.Type)
            {
            case ItemType.Bomb:
                droppedItem = new ActionnedBomb(ComputeDropCoordinates(Constants.Bomb.WIDTH, Constants.Bomb.HEIGHT));
                break;

            case ItemType.LifePotionSmall:
                Engine.Default.Player.DrinkLifePotion(Constants.Inventory.SMALL_LIFE_POTION_RECOVERY_LIFE_POINTS);
                break;

            case ItemType.LifePotionMedium:
                Engine.Default.Player.DrinkLifePotion(Constants.Inventory.MEDIUM_LIFE_POTION_RECOVERY_LIFE_POINTS);
                break;

            case ItemType.LifePotionLarge:
                Engine.Default.Player.DrinkLifePotion(Constants.Inventory.LARGE_LIFE_POTION_RECOVERY_LIFE_POINTS);
                break;

            case ItemType.Lamp:
                LampIsOn = !LampIsOn;
                break;

            case ItemType.Bow:
                droppedItem = new ActionnedArrow(ComputeDropCoordinates(Constants.Arrow.WIDTH, Constants.Arrow.HEIGHT),
                                                 Engine.Default.Player.Direction, Engine.Default.Player);
                break;
            }

            return(droppedItem);
        }