Interactable items - slotting working correctly and calling the dispatchers each time

This commit is contained in:
Michal Pikulski
2025-09-12 12:26:44 +02:00
parent ef96d80d51
commit 445e36975d
5 changed files with 67 additions and 107 deletions

View File

@@ -18,7 +18,6 @@ namespace Interactions
private PickupItemData _currentlySlottedItemData;
public SpriteRenderer slottedItemRenderer;
private GameObject _currentlySlottedItemObject = null;
public GameObject GetSlottedObject()
{
@@ -38,107 +37,41 @@ namespace Interactions
{
var heldItemData = FollowerController.CurrentlyHeldItemData;
var heldItemObj = FollowerController.GetHeldPickupObject();
var pickup = GetComponent<Pickup>();
var slotItem = pickup != null ? pickup.itemData : null;
var config = GameManager.Instance.GetSlotItemConfig(slotItem);
var allowed = config?.allowedItems ?? new List<PickupItemData>();
var config = GameManager.Instance.GetSlotItemConfig(itemData);
var forbidden = config?.forbiddenItems ?? new List<PickupItemData>();
if (heldItemData != null && _currentlySlottedItemObject != null)
{
// Remove the currently slotted item
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData);
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
onItemSlotRemoved?.Invoke();
UpdateSlottedSprite();
// Now slot the held item and check correctness
if (forbidden.Contains(heldItemData))
{
DebugUIMessage.Show("Can't place that here.");
onForbiddenItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
return;
}
SlotItem(heldItemObj, heldItemData);
if (allowed.Contains(heldItemData))
{
onCorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(true);
return;
}
else
{
DebugUIMessage.Show("I'm not sure this works.");
onIncorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
return;
}
}
else if (heldItemData == null && _currentlySlottedItemObject != null)
{
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData);
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
onItemSlotRemoved?.Invoke();
UpdateSlottedSprite();
return;
}
// // CASE 1: No held item, slot has item -> pick up slotted item
// if (heldItemData == null && _cachedSlottedObject != null)
// {
// InteractionOrchestrator.Instance.PickupItem(FollowerController, _cachedSlottedObject);
// _cachedSlottedObject = null;
// currentlySlottedItem = null;
// UpdateSlottedSprite();
// Interactable.BroadcastInteractionComplete(false);
// return;
// }
// // CASE 2: Held item, slot has item -> swap
// if
// {
// InteractionOrchestrator.Instance.SwapItems(FollowerController, this);
// currentlySlottedItem = heldItemData;
// UpdateSlottedSprite();
// return;
// }
// CASE 3: Held item, slot empty -> slot the held item
// Held item, slot empty -> try to slot item
if (heldItemData != null && _currentlySlottedItemObject == null)
{
// First check for forbidden items at the very start so we don't continue unnecessarily
if (forbidden.Contains(heldItemData))
{
DebugUIMessage.Show("Can't place that here.");
DebugUIMessage.Show("Can't place that here.", Color.red);
onForbiddenItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
return;
}
SlotItem(heldItemObj, heldItemData);
if (allowed.Contains(heldItemData))
{
onCorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(true);
return;
}
else
{
DebugUIMessage.Show("I'm not sure this works.");
onIncorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
return;
}
}
// CASE 4: No held item, slot empty -> show warning
if (heldItemData == null && _currentlySlottedItemObject == null)
{
DebugUIMessage.Show("This requires an item.");
SlotItem(heldItemObj, heldItemData, true);
return;
}
// Either pickup or swap items
if ((heldItemData == null && _currentlySlottedItemObject != null)
|| (heldItemData != null && _currentlySlottedItemObject != null))
{
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData, false);
onItemSlotRemoved?.Invoke();
SlotItem(heldItemObj, heldItemData, _currentlySlottedItemObject == null);
return;
}
// No held item, slot empty -> show warning
if (heldItemData == null && _currentlySlottedItemObject == null)
{
DebugUIMessage.Show("This requires an item.", Color.red);
return;
}
return;
}
/// <summary>
@@ -170,18 +103,43 @@ namespace Interactions
}
}
public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData)
public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData, bool clearFollowerHeldItem = true)
{
if (itemToSlot == null)
return;
if (itemToSlot == null)
{
_currentlySlottedItemData = null;
_currentlySlottedItemData = null;
}
else
{
itemToSlot.SetActive(false);
itemToSlot.transform.SetParent(null);
SetSlottedObject(itemToSlot);
itemToSlot.SetActive(false);
itemToSlot.transform.SetParent(null);
SetSlottedObject(itemToSlot);
_currentlySlottedItemData = itemToSlotData;
_currentlySlottedItemData = itemToSlotData;
if (clearFollowerHeldItem)
{
FollowerController.ClearHeldItem();
}
}
UpdateSlottedSprite();
FollowerController.ClearHeldItem();
// Once an item is slotted, we know it is not forbidden, so we can skip that check, but now check if it was
// the correct item we're looking for
var config = GameManager.Instance.GetSlotItemConfig(itemData);
var allowed = config?.allowedItems ?? new List<PickupItemData>();
if (allowed.Contains(itemToSlotData))
{
DebugUIMessage.Show("You correctly slotted " + itemToSlotData.itemName + " into: " + itemData.itemName, Color.green);
onCorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(true);
}
else
{
DebugUIMessage.Show("I'm not sure this works.", Color.yellow);
onIncorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
}
}
}
}