Пример #1
0
        public Item Equip(Item equippable, EquipmentSlot slot)
        {
            if (equippable.SlotKind != slot.Kind || !Slots.Contains(slot))
            {
                return(null);
            }

            Item previousItem = null;

            if (SlotsInUse.Contains(slot))
            {
                previousItem = _equipped[slot];
            }


            _equipped[slot] = equippable;
            SlotsInUse.Add(slot);

            OnEquipped?.Invoke(slot, equippable);

            if (previousItem != null)
            {
                OnUnequipped?.Invoke(slot, previousItem);
            }

            return(previousItem);
        }
Пример #2
0
 /// <summary>
 /// Gives the slot a reference to a weapon. Can't equip a weapon if already holding one.
 /// </summary>
 /// <param name="weapon"></param>
 /// <returns>Returns whether the contents of the slot changed.</returns>
 public bool Equip(IWeapon weapon)
 {
     if (!IsEmpty || weapon == null)
     {
         return(false);
     }
     Weapon = weapon;
     OnEquipped?.Invoke(weapon);
     return(true);
 }
Пример #3
0
    //equipment
    public void SetEquipment(Equipable equipable)
    {
        if (equipable == null)
        {
            Debug.LogWarning("Equipping null equipable!\n\n" + new NullReferenceException().StackTrace);
            return;
        }

        if (this.GetEquipment(equipable.slot) != null)
        {
            Unequip(equipable.slot);
        }

        if (equipable is Holdable holdable)
        {
            //if we're equipping something that requires both hands, unequip other hand
            if (holdable.requiresBothHands)
            {
                Unequip(equipable.slot == EquipSlot.LeftHandItem ? EquipSlot.RightHandItem : EquipSlot.LeftHandItem);
            }
            //if we're equipping something that doesn't require both hands, check if we already have something
            //in the other hand that requires both hands, and unequip it
            else
            {
                Holdable otherHand = (Holdable)GetEquipment(equipable.slot == EquipSlot.LeftHandItem ? EquipSlot.RightHandItem : EquipSlot.LeftHandItem);

                //if true
                if (otherHand != null && otherHand.requiresBothHands)
                {
                    Unequip(otherHand.slot);
                }
            }
        }

        _equipment[(int)equipable.slot] = equipable;
        _equipment[(int)equipable.slot]?.OnEquip();

        OnEquipped?.Invoke(equipable);
    }