Ejemplo n.º 1
0
            /// <summary>
            /// Notifies this object when an item has been removed from an <see cref="EquipmentSlot"/>.
            /// </summary>
            /// <param name="slot">The <see cref="EquipmentSlot"/> the item was removed from.</param>
            public void NotifyRemoved(EquipmentSlot slot)
            {
                // Get the ID of the slot
                var slotID = slot.GetValue();

                // Check if there was a paper-doll item in the slot
                if (_bodies[slotID] == null)
                {
                    return;
                }

                // There was a paper-doll item in the slot, so remove it and synchronize
                _bodies[slotID] = null;
                SynchronizeBodyLayers();
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Notifies this object when an item has been added into an <see cref="EquipmentSlot"/>.
            /// </summary>
            /// <param name="slot">The <see cref="EquipmentSlot"/> the item was added to.</param>
            /// <param name="item">The item.</param>
            public void NotifyAdded(EquipmentSlot slot, IItemTable item)
            {
                // Get the ID of the slot
                var slotID = slot.GetValue();

                // Check if there is a paper-doll item in the slot already. If so, remove it.
                if (_bodies[slotID] != null)
                {
                    NotifyRemoved(slot);
                }

                // Check that the new item has a paper-doll value
                if (string.IsNullOrEmpty(item.EquippedBody))
                {
                    return;
                }

                // The new item has a paper-doll value, so add it and synchronize
                _bodies[slotID] = item.EquippedBody;
                SynchronizeBodyLayers();
            }
Ejemplo n.º 3
0
 public T this[EquipmentSlot slot]
 {
     get { return(this[slot.GetValue()]); }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Tries to set a given <paramref name="slot"/> to a new <paramref name="item"/>.
        /// </summary>
        /// <param name="slot">Slot to set the item in.</param>
        /// <param name="item">Item to set the slot to.</param>
        /// <param name="checkIfCanEquip">If true, the specified <paramref name="item"/> will
        /// be checked if it can be euqipped with <see cref="CanEquip"/>. If false, this additional
        /// check will be completely bypassed.</param>
        /// <returns>True if the <paramref name="item"/> was successfully added to the specified
        /// <paramref name="slot"/>, else false. When false, it is guarenteed the equipment will
        /// not have been modified.</returns>
        protected bool TrySetSlot(EquipmentSlot slot, T item, bool checkIfCanEquip = true)
        {
            // Check for a valid EquipmentSlot
            if (!EnumHelper <EquipmentSlot> .IsDefined(slot))
            {
                const string errmsg = "Invalid EquipmentSlot `{0}` specified.";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, slot);
                }
                Debug.Fail(string.Format(errmsg, slot));
                return(false);
            }

            // Get the array index for the slot
            var index = slot.GetValue();

            // If the slot is equal to the value we are trying to set it, we never want
            // to do anything extra, so just abort
            var currentItem = this[index];

            if (currentItem == item)
            {
                return(false);
            }

            if (item != null)
            {
                // Add item

                // Ensure the item can be equipped
                if (checkIfCanEquip)
                {
                    if (!CanEquip(item))
                    {
                        return(false);
                    }
                }

                // If the slot is already in use, remove the item, aborting if removal failed
                if (currentItem != null)
                {
                    if (!RemoveAt(slot))
                    {
                        return(false);
                    }
                }

                // Attach the listener for the OnDispose event
                item.Disposed -= ItemDisposeHandler;
                item.Disposed += ItemDisposeHandler;

                // Set the item into the slot
                _equipped[index] = item;

                OnEquipped(item, slot);

                if (Equipped != null)
                {
                    Equipped.Raise(this, new EquippedEventArgs <T>(item, slot));
                }
            }
            else
            {
                // Remove item (set to null)

                // Check if the item can be removed
                if (!CanRemove(slot))
                {
                    return(false);
                }

                var oldItem = _equipped[index];

                // Remove the listener for the OnDispose event
                oldItem.Disposed -= ItemDisposeHandler;

                // Remove the item
                _equipped[index] = null;

                OnUnequipped(oldItem, slot);

                if (Unequipped != null)
                {
                    Unequipped.Raise(this, new EquippedEventArgs <T>(oldItem, slot));
                }
            }

            // Slot setting was successful (since we always aborted early with false if it wasn't)
            return(true);
        }