Esempio n. 1
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;
        }
Esempio n. 2
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);
        }