Beispiel #1
0
        /// <summary>
        /// Use the item group. Function is responsible for deleting the item if used up etc. Return true if item was used successfully and time should be advanced.
        /// </summary>
        /// <param name="selectedGroup"></param>
        internal bool UseItem(InventoryListing selectedGroup)
        {
            //For now, we use the first item in any stack only
            int  itemIndex = selectedGroup.ItemIndex[0];
            Item itemToUse = Inventory.Items[itemIndex];

            //Check if this is a useable item
            IUseableItem useableItem = itemToUse as IUseableItem;

            if (useableItem == null)
            {
                Game.MessageQueue.AddMessage("Cannot use this type of item!");
                LogFile.Log.LogEntry("Tried to use non-useable item: " + itemToUse.SingleItemDescription);
                return(false);
            }

            bool usedSuccessfully = useableItem.Use(this);

            if (useableItem.UsedUp)
            {
                //Remove item from inventory and don't drop on floor
                Inventory.RemoveItem(itemToUse);
                Game.Dungeon.RemoveItem(itemToUse);
            }

            return(usedSuccessfully);
        }
Beispiel #2
0
 /// <summary>
 /// Callback from the inventory when the item is changed.
 /// </summary>
 /// <param name="item">The new item.</param>
 private void PrimaryItemChange(Item item)
 {
     // Do not listen for the ready event when the inventory switches items.
     if (m_ItemUsePending != null)
     {
         EventHandler.UnregisterEvent(m_GameObject, "OnItemReadyForUse", ReadyForUse);
         m_ItemUsePending = null;
     }
 }
        private static void ProcessUseCommand(string[] parts)
        {
            if (parts.Length == 1)
            {
                PrintLineWarning("Please specify which item.");
                return;
            }

            if (parts.Length == 2)
            {
                CurrentArea.GetItem(parts[1]);
                IUseableItem itemToUse = CurrentArea.GetItem(parts[1]) as IUseableItem;
                if (itemToUse != null)
                {
                    try
                    {
                        ((IUseableItem)itemToUse).Use();
                        PrintLinePositive("Neat thing!");
                    }
                    catch (ItemDepletedException ide)
                    {
                        PrintLineSpecial(ide.Message);
                    }
                    catch (WorldException we)
                    {
                        PrintLineDanger(we.Message);
                    }
                }
                else
                {
                    PrintLineWarning("This item cannot be used...");
                    return;
                }
            }

            string targetName = parts[3];
            object target;

            if (CurrentArea.HasItem(targetName))
            {
                target = CurrentArea.GetItem(targetName);
            }

            else if (CurrentArea.CreatureExists(targetName))
            {
                target = CurrentArea.GetCreature(targetName);
            }
            else
            {
                PrintLineWarning("I don't see any of that around here...");
                return;
            }
        }
Beispiel #4
0
        /// <summary>
        /// The primary item isn't always ready when the user wants to use it. For example, the primary item may be a weapon and that weapon needs to aim
        /// before it can fire. ReadyForUse will be called when the item is ready to be used.
        /// </summary>
        private void ReadyForUse()
        {
            // No longer need to listen to the event.
            EventHandler.UnregisterEvent(m_GameObject, "OnItemReadyForUse", ReadyForUse);
            // Try to use the item.
            if (m_ItemUsePending != null)
            {
                m_ItemUsePending.TryUse();

                // The item may have been stopped in the time that it took for the item to be ready. Let the item be used once and then stop the use.
                if (m_StopUse)
                {
                    m_ItemUsePending.TryStopUse();
                }
            }

            m_UseType        = UseType.None;
            m_ItemUsePending = null;
            m_StopUse        = false;
        }
Beispiel #5
0
 public ItemDepletedException(string message, IUseableItem item) : base(message, item)
 {
 }
Beispiel #6
0
        /// <summary>
        /// Tries to use the specified item. The item may not be able to be used if it isn't equipped or is in use.
        /// </summary>
        /// <param name="useType">Specifies the type of item that should be used.</param>
        /// <param name="extensionIndex">Specifies the index of the extension that should be used.</param>
        /// <returns>True if the item was used.</returns>
        private bool TryUseItem(UseType useType, int extensionIndex)
        {
            // Return early if the item cannot be interacted with or used.
            if (!m_CanInteractItem.Invoke() || !m_CanUseItem.Invoke())
            {
                return(false);
            }
            IUseableItem useableItem = null;
            var          primaryItem = true;

            if (((int)useType & (int)UseType.Primary) == (int)UseType.Primary)
            {
                useableItem = m_CurrentPrimaryItem.Get() as IUseableItem;
            }
            else if (((int)useType & (int)UseType.DualWield) == (int)UseType.DualWield)
            {
                useableItem = m_CurrentDualWieldItem.Get() as IUseableItem;
            }
            else if (((int)useType & (int)UseType.Secondary) == (int)UseType.Secondary)
            {
                useableItem = m_CurrentSecondaryItem.Get() as IUseableItem;
                primaryItem = false;
            }
            if (useableItem != null && useableItem.CanUse())
            {
                // If the extension index isn't -1 then use the extension item.
                if (extensionIndex != -1)
                {
                    useableItem = (useableItem as Item).ItemExtensions[extensionIndex] as IUseableItem;
                    if (useableItem == null || !useableItem.CanUse())
                    {
                        return(false);
                    }
                }
                if (primaryItem)
                {
                    // The UseType should always be updated.
                    m_UseType |= useType;
                    if (m_ItemUsePending == null)
                    {
                        // The SharedMethod TryUseItem will return failure if the item cannot be used for any reason, such as a weapon not being aimed. If this happens
                        // register for the event which will let us know when the item is ready to be used.
                        m_ItemUsePending = useableItem;
                        var item = useableItem as Item;
                        if (item == null)
                        {
                            item = (useableItem as ItemExtension).ParentItem;
                        }
                        if (m_TryUseItem.Invoke(item))
                        {
                            ReadyForUse();
                        }
                        else
                        {
                            EventHandler.RegisterEvent(m_GameObject, "OnItemReadyForUse", ReadyForUse);
                        }
                    }
                    return(true);
                }
                else
                {
                    if (useableItem.TryUse())
                    {
                        // After the item is used the character may no longer be alive so don't execuate the events.
                        if (enabled || m_IndependentLook.Invoke())
                        {
                            EventHandler.ExecuteEvent <bool>(m_GameObject, "OnUpdateAnimator", false);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }