/// <summary> /// Updates the slot indicator's visibility and position. /// </summary> /// <param name="inventorySlotBehaviour">If one is given, assume we're picking up an item. Otherwise, assume we're dropping an item.</param> public void UpdateSlotIndicator(InventorySlotBehaviour inventorySlotBehaviour = null) { if (inventorySlotBehaviour != null && !_inventorySlotIndicatorBehaviour.Visible) { // If the slot indicator is invisible, make it visible and move it to the new slot. _inventorySlotIndicatorBehaviour.MoveTo(inventorySlotBehaviour.transform.localPosition, true); _inventorySlotIndicatorBehaviour.Visible = true; } else { // If the current slot has an item, then don't attempt to find another slot. if (_slots[_currentSlotIndex].ItemBehaviour != null) { return; } int nextSlotIndex = -1; for (int i = 0; i < _slots.Length; i++) { if (_slots[i].ItemBehaviour != null) { nextSlotIndex = i; break; } } // If there's a slot with an item, set it as the current slot and move the indicator to it. // Otherwise hide the indicator. if (nextSlotIndex != -1) { _currentSlotIndex = nextSlotIndex; _inventorySlotIndicatorBehaviour.Visible = true; _inventorySlotIndicatorBehaviour.MoveTo(_slots[_currentSlotIndex].transform.localPosition); } else { _inventorySlotIndicatorBehaviour.Visible = false; } } }
/// <summary> /// Picks up an item if one is nearby. /// Called by the "pickup item" input action. /// </summary> /// <param name="context">The context of the input action.</param> public void OnPickupItem(InputAction.CallbackContext context) { if (!SceneManager.GetActiveScene().isLoaded) { return; } if (!context.performed) { return; } ItemBehaviour selectedItem = GetNearbyItem(); if (selectedItem == null) { return; } InventorySlotBehaviour nextAvailableSlot = _slots.FirstOrDefault(x => x.ItemBehaviour == null); if (nextAvailableSlot == null) { // All inventory slots are taken. // Swap the current item with the nearby item. OnDropItem(context, false); OnPickupItem(context); return; } // Pickup the item into the next available slot. nextAvailableSlot.PickupItem(selectedItem); selectedItem.gameObject.SetActive(false); UpdateSlotIndicator(nextAvailableSlot); }