Example #1
0
    public Item Equip(AttachmentSlotId slotId, InventoryItem inventoryItem)
    {
        if (_slotLocks[slotId])
        {
            return(null);
        }

        if (!Inventory.Contains(inventoryItem))
        {
            return(null);
        }

        AttachableContext attachable = (AttachableContext)inventoryItem.context;

        Debug.AssertFormat(attachable, string.Format("Failed to equip: item '{0}' is not attachable", inventoryItem.context.name));

        AttachmentSlot slot = _slots.FirstOrDefault(x => x.SlotId == slotId);

        Debug.AssertFormat(slot != null, string.Format("No slot found with slot id '{0}'", slotId));

        if (_equippedItems[slotId] != null)
        {
            if (!_equippedItems[slotId].GetStateInfo().Depleted)
            {
                // Unequip currently equipped and re-add to the inventory
                AddToInventory(new InventoryItem(
                                   _equippedItems[slotId].GetContext(),
                                   _equippedItems[slotId].GetStateInfo()));
            }
            GameObject.Destroy(_equippedItems[slotId].gameObject);
        }

        // Spawn and equip item from inventory
        Item spawnedItem = GameObject.Instantiate(inventoryItem.context.Prefab);

        _equippedItems[slotId] = spawnedItem;
        if (inventoryItem.stateInfo != null)
        {
            _equippedItems[slotId].InitStateInfo(inventoryItem.stateInfo);
        }

        spawnedItem.transform.SetPositionAndRotation(
            slot.BoneTransform.position,
            slot.BoneTransform.rotation);
        spawnedItem.transform.SetParent(slot.BoneTransform);
        spawnedItem.transform.localPosition = attachable.AttachmentPositionOffset;
        spawnedItem.transform.localRotation = Quaternion.Euler(attachable.AttachmentRotationOffset);

        // Remove inventory item of equiped item
        RemoveFromInventory(inventoryItem);

        OnEquipmentChanged.Invoke(inventoryItem.context);
        return(spawnedItem);
    }
Example #2
0
    public void UnEquip(AttachmentSlotId slotId)
    {
        Item equipped = _equippedItems[slotId];

        if (equipped != null)
        {
            _equippedItems[slotId] = null;
            equipped.transform.SetParent(null);
            OnEquipmentChanged.Invoke(null);
        }
    }
Example #3
0
 public Item GetEquipmentInSlot(AttachmentSlotId slotId)
 {
     return(_equippedItems[slotId]);
 }
Example #4
0
 public void UnlockSlot(AttachmentSlotId slotId)
 {
     _slotLocks[slotId] = false;
 }
Example #5
0
 public void LockSlot(AttachmentSlotId slotId)
 {
     _slotLocks[slotId] = true;
 }